C++ Primer章课后编程问题
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
#include<iostream>
int main()
{
using namespace std;
int num1;
int num2;
int total=0;
cout << "请输入開始数字\n";
cin >> num1;
cout << "请输入结束数字\n";
cin >> num2;
for (num1; num1<=num2; num1++)
total = num1 + total;
cout << num1 << " 和 " << num2 << "之间的整数和为 " << total <<endl;
return 0;
}
#include<iostream>
int main()
{
using namespace std;
double total = 0.0;
double in;
cout << "请输入数字:";
cin >> in;
while (in != 0)
{
total += in;
cout << "全部输入数的和为:" << total << "\n";
cout << "请输入下一个数字:";
cin >> in;
}
cout << "执行结束";
return 0;
}
#include<iostream>
int main()
{
using namespace std;
double daphne=100;
double cleo=100;
int year=1;
while (daphne >= cleo)
{
daphne += 100*0.1;
cleo += cleo*0.05;
cout << "第" << year << "年,daphne投资价值为 " << daphne << "cleo投资价值为 " << cleo <<endl;
year++;
}
cout << "第 " << year << "年,时cleo超过daphne的投资价值"<< endl;
return 0;
}
#include<iostream>
const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int main()
{
using namespace std;
int sales[12];
int total;
for (int i=1; i<=12; i++)
{
cout << "请输入" << months[i-1] << "销售数量:";
cin >> sales[i-1];
}
for (int j=0; j<12; j++)
{
total = total+sales[j];
}
cout << "总销售为:" << total <<endl;
return 0;
}
#include<iostream>
const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int main()
{
using namespace std;
int sales[3][12];
int total[3] = {0}; //一定要初始化。不然初始值不为0
int sum;
for (int a=1;a<=3;a++)
{
for (int i=1; i<=12; i++)
{
cout << "请输入第"<< a << "年" << months[i-1] << "销售数量:";
cin >> sales[a-1][i-1];
}
}
for (int b=0; b<3; b++)
{
for (int j=0; j<12; j++)
{
total[b] = total[b]+sales[b][j];
}
sum = sum + total[b];
cout << "第" << b+1 << "年总销量为" << total[b] <<endl;
}
cout << "总销售为:" << sum <<endl;
return 0;
}
<pre name="code" class="cpp">//一定要加while (cin.get() != '\n');
#include<iostream>
using namespace std;
const int LEN = 60;
struct Car
{
char brand[LEN];
int year;
};
int main()
{
int num;
cout << "How many cars do you wish to catalog?";
cin >> num;
while (cin.get() != '\n');
Car *ps = new Car[num];
for (int i=0;i<num;i++)
{
cout << "Car #" << (i+1) << ":\n";
cout << "Please enter the make:";
cin.getline(ps[i].brand, LEN);
cout << "Please enter the year made:";
cin >> ps[i].year;
while(cin.get() != '\n');
}
cout << "Here is your collection:\n";
for (int i=0; i<num; i++)
{
cout << ps[i].year << " " << ps[i].brand <<endl;
}
delete [] ps;
return 0;
}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
#include<iostream>
#include<cstring>
const int STR_LEN=60;
int main()
{
using namespace std;
char words[STR_LEN];
int count=0;
cout << "Enter words(to stop,type the word done):\n";
while (cin >> words && strcmp("done", words))
++count;
cout << "You entered a total of " << count << " words .\n" <<endl;
return 0;
}
#include<iostream>
#include<string>
int main()
{
using namespace std;
string words;
int count=0;
cout << "Enter words {to stop,type the word done}:\n";
while (cin >> words && words != "done")
++count;
cout << "You entered a total of " << count << " words .\n";
return 0;
}
#include<iostream>
int main()
{
using namespace std;
int row;
cout << "Enter number of row:";
cin >> row;
for (int i=0; i<row; i++)
{
for (int j=row-i; j>1; j--)
{
cout << ".";
}
for (int k=0; k<=i; k++)
{
cout << "*";
}
cout << "\n";
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
C++ Primer章课后编程问题的更多相关文章
- C++ Primer第九章课后编程问题
1. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0J ...
- java科学和艺术语言 第六章 课后编程
家转载,为保留作者成果.转载请注明出处,http://blog.csdn.net/netluoriver,有些文件在资源中也能够下载!假设你没有积分,能够联系我索要. 1. package Sixth ...
- Kenneth A.Lambert著的数据结构(用python语言描述)的第一章课后编程答案
第6题:工资部门将每个支付周期的雇员信息的列表保存到一个文本文件, 每一行的格式:<last name><hourly wage><hours worked> 编写 ...
- 《C++面向对象程序设计》第6章课后编程题2拓展
设计一个程序,其中有3个类CBank.BBank.GBank,分别为中国银行类,工商银行类和农业银行类.每个类都包含一个私有数据成员balance用于存放储户在该行的存款数,另有一个友元函数Total ...
- Java程序设计(2021春)——第一章课后题(选择题+编程题)答案与详解
Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 第一章选择题 1.1 Java与面向对象程 ...
- Java程序设计(2021春)——第二章课后题(选择题+编程题)答案与详解
Java程序设计(2021春)--第二章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第二章课后题(选择题+编程题)答案与详解 第二章选择题 2.1 面向对象方法的特性 ...
- C++第三章课后作业答案及解析---指针的使用
今天继续完成上周没有完成的习题---C++第三章课后作业,本章题涉及指针的使用,有指向对象的指针做函数参数,对象的引用以及友元类的使用方法等 它们具体的使用方法在下面的题目中会有具体的解析(解析标注在 ...
- 《深入浅出Node.js》第7章 网络编程
@by Ruth92(转载请注明出处) 第7章 网络编程 Node 只需要几行代码即可构建服务器,无需额外的容器. Node 提供了以下4个模块(适用于服务器端和客户端): net -> TCP ...
- 20145330《Java学习笔记》第一章课后练习8知识总结以及IDEA初次尝试
20145330<Java学习笔记>第一章课后练习8知识总结以及IDEA初次尝试 题目: 如果C:\workspace\Hello\src中有Main.java如下: package cc ...
随机推荐
- Rick's RoTs -- Rules of Thumb for MySQL--转载
原文地址:http://mysql.rjweb.org/doc.php/ricksrots Brought to you by Rick James Here are 160+ tips, trick ...
- centos7 分区满了,分析哪个目录或文件占用空间-小叶-51CTO博客
原文:centos7 分区满了,分析哪个目录或文件占用空间-小叶-51CTO博客 du -sh 例如: [root@zabbix ~]# du -sh /var/* 0 /var/adm 132M / ...
- 20160206.CCPP体系具体解释(0016天)
代码片段(01):.指针.c+02.间接赋值.c 内容概要:内存 ///01.指针 #include <stdio.h> #include <stdlib.h> //01.取地 ...
- MySQL字符编码问题,Incorrect string value
MySQL上插入汉字时报错例如以下.详细见后面分析. Incorrect string value: '\xD0\xC2\xC8A\xBEW' for column 'ctnr' at row 1 M ...
- 每天五个java相关面试题(3)
1. Servlet的生命周期是什么? 答: 第一次请求: 构造方法->init() -> service() -> doGet()/doPost() 以后的请求:service() ...
- WCF学习笔记——对象序列化
当试图通过Web服务.WCF这样的远程处理技术将一个对象复制到远端时,具有对类型序列化的能力很关键. 一 序列化基础 序列化描述了持久化或传输一个对象的状态到流的过程(.NET将对象序列化到流,流是字 ...
- BI并不是万能,中层业务管理报表要另辟蹊径
BI是商业智能的缩写,是能够帮助企业做出明智的业务经营决策的工具.其数据来源于各个业务系统.如ERP.CRM.SCM.进销存.HER.OA等. BI系统不同于传统的管理信息 ...
- [HTML] Change an HTML5 input's placeholder color with CSS
We will look at what CSS selectors to use to change an HTML5 inputs placeholder color. This can diff ...
- 20、RTC驱动程序
drivers\rtc\rtc-s3c.c s3c_rtc_init platform_driver_register s3c_rtc_probe rtc_device_register(" ...
- 随手可得的Application对象
在Android的开发其中,Application和Context对象应该是我们接触最多的对象了,特别是Context对象. 当我们在某个Activity或者Service其中时,因为它们本身就是Co ...