1. 第1题

#include <iostream>using namespace std;int main()
{int n1 = 0;//第一个数int n2 = 0;//第二个数int total = 0;//和cout << "请输入2个整数:";cin >> n1 >> n2;for (int i = n1; i <= n2; i++){total = total + i;}cout << n1 << "~" << n2 << "之间的所有整数和为:" << total << endl;return 0;
}

这里要注意的是for循环的边界,因为是包含这两个整数的,所以从n1开始,到n2结束,因此要用i<=n2
结果图:

2. 第2题

#include<iostream>
#include<array>using namespace std;const int ArSize = 101;//0~100int main()
{array<long double, ArSize> a1;a1[1] = a1[0] = 1;for (int i = 2; i < ArSize; i++){a1[i] = a1[i - 1] * i;}for (int i = 0; i < ArSize; i++){cout << i << "! = " << a1[i] << endl;//逐个显示}return 0;
}

这里要注意的:

  1. 使用了array模板,所以要添加相应的头文件。
  2. 要计算100!,因为0!也要算进去,所以ArSize应该设置为101。

结果图:

3. 第3题

#include<iostream>using namespace std;int main()
{int num = 0;//输入的数字int total = 0;//和cout << "请输入数字:";cin >> num;while (num != 0){total = total + num;cout << "当前累计和为:" << total << endl;cout << "请输入数字:";cin >> num;}cout << "程序结束!" << endl;return 0;
}

注意点:

  1. while循环的测试条件,当输入0的时候跳出循环,还可用用别的循环。

结果图:

4. 第4题

#include <iostream>using namespace std;int main()
{//原始存款100美元double Daphne = 100;double Cleo = 100;int year = 0;//年数double RateofDaphne = 0.10;//Daphne的年利率double RateofCleo = 0.05;//Cleo的年利率while (Cleo <= Daphne){//第year年的投资价值Daphne = Daphne + 100 * RateofDaphne;Cleo = Cleo + Cleo * RateofCleo;year++;}cout << year << "年后:" << endl;cout << "Cleo的投资价值为:" << Cleo << "美元" << endl;cout << "Daphne的投资价值为:" << Daphne << "美元" << endl;cout << "Cleo的投资价值超过了Daphne的投资价值!" << endl;return 0;
}

注意的点:

  1. 选择哪一种循环方式,我这里选择的是while循环,因为我不知道要循环多少次,当然也可以用for循环。
  2. 测试条件应该是Cleo的投资价值>Daphne的投资价值时,跳出循环。

结果图:

5. 第5题

#include <iostream>
#include<string>using namespace std;int main()
{const string Month[12] = { "Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Nov","Oct","Dec" };//12个月int number[12];//每个月的销售额int total = 0;//总销售额cout << "请输入每个月的销售额:" << endl;for (int i = 0; i < 12; i++){cout << Month[i] << ":";cin >> number[i];}for (int i = 0; i < 12; i++){total = total + number[i];}cout << "这一年的销售总额为:" << total << endl;return 0;
}

注意的点:

  1. 月份字符串我采用的string,因此应该添加相应的头文件,之所以采用string而没有采用char*,是因为我觉得string比char更方便,不容易出错。

结果图:

6. 第6题

#include <iostream>
#include<string>using namespace std;int main()
{const string Month[12] = { "Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Nov","Oct","Dec" };//12个月int number[3][12];//每年每个月的销售额int TotalofYear[3] = { 0 };//每年的销售额int total = 0;//3年的总销售额for (int i = 0; i < 3; i++){cout << "请输入第" << i + 1 << "年各个月的销售额:" << endl;for (int j = 0; j < 12; j++){cout << Month[j] << ":";cin >> number[i][j];}}for (int i = 0; i < 3; i++){for (int j = 0; j < 12; j++){TotalofYear[i] = number[i][j] + TotalofYear[i];}       cout << "第" << i + 1 << "年的销售额为:" << TotalofYear[i] << endl;}total = TotalofYear[0] + TotalofYear[1] + TotalofYear[2];cout << "这3年的销售总额为:" << total << endl;system("pause");return 0;
}

这题相比于上一题代码要多一点,因为是三年的销售量,使用到二维数组,单单输入就要输3次12个月。其实二维数组就是多个一维数组组成的,很简单。

结果图:

7. 第7题

#include <iostream>
#include<string>using namespace std;struct car
{string make;//生产商int yearmade;//生产年份
};int main()
{int num;//车辆数cout << "How many cars do you wish to catalog?";cin >> num;cin.get();//上一行输入的是数字,下一行输入的是字符串,为了防止混合输入数字和面向行的字符串出现的问题car *arr = new car[num];//动态创建相应数量的car结构数组;for (int i = 0; i < num; i++){cout << "Car#" << i + 1 << ":" << endl;cout << "Please enter the make: ";getline(cin, (arr + i)->make);cout << "Please enter the year made: ";cin >> (arr + i)->yearmade;cin.get();//上一行输入的是数字,下一行输入的是字符串,为了防止混合输入数字和面向行的字符串出现的问题}cout << "Here is your collection:" << endl;for (int i = 0; i < num; i++){cout << (arr + i)->make << " " << (arr + i)->yearmade << endl;}delete[] arr;//释放内存return 0;
}

这一题看似简单,实质上到处都是坑!我就掉坑里了,尽管看过书,也知道容易犯错,还是栽了>-<

  1. 混合输入数字和面向行的字符串时,千万千万要注意!!!在二者之间加上cin.get(); !!!这是因为当cin读取数字后会将回车键生成的换行符留在输入队列中,导致后面读取字符串时看到的不是你输入的字符串,而是换行符!为了防止这种错误的出现,请同学们一定要加上cin.get();,我也求求我自己长长心吧,栽了好几次了。。。
  2. 使用new创建动态数组时,最后记得delete数组释放内存。
  3. 这里访问结构成员时,我使用的时=是指针,所以用箭头运算符访问。

结果图:

8. 第8题

#include <iostream>
#include<cstring>using namespace std;int main()
{char words[100];int count = 0;//计数cout << "Enter words (to stop, type the word done): " << endl;cin >> words;while (strcmp(words, "done") != 0){cin >> words;//不是done继续输入count++;}cout << "You entered a total of " << count << " words." << endl;return 0;
}

注意点

  1. 因为不知道要循环多少次,所以选择while循环
  2. 跳出循环的测试条件是用户输入done时。
  3. 当用户输入的不是done时,继续输入,并计数加一
  4. 这题和书上152页的5.16一个性质。

结果图:

9. 第9题

#include <iostream>
#include<string>using namespace std;int main()
{string words;int count = 0;//计数cout << "Enter words (to stop, type the word done): " << endl;cin >> words;while (words != "done"){cin >> words;//不是done继续输入count++;}cout << "You entered a total of " << count << " words." << endl;return 0;
}

这题和上一题不同之处是用string类,因此比较两个字符串是否一样时,直接用!=即可。

结果图:

10. 第10题

#include <iostream>
#include<string>using namespace std;int main()
{int num;cout << "Enter number of rows: ";cin >> num;for (int i = 1; i <= num; i++){//打印.for (int j = i; j < num; j++){cout << '.';}//打印*for (int k = i; k > 0; k--){cout << '*';}       cout << endl;//每行打印完换行}return 0;
}

注意点:

  1. 第一层循环是用来控制行,1~5行
  2. 第二层循环中用来控制每行中打印的字符:其中有两个循环,第一个循环是打印字符.,第二循环是打印*,主要是控制打印两种字符的个数。

结果图:

C++PrimePlus第5章编程练习答案及运行结果相关推荐

  1. 速学堂(java)第十一章编程题答案(自写)

    速学堂(java)第十一章编程题答案(自写) 1.设计一个多线程的程序如下:设计一个火车售票模拟程序.假如火车站要有100张火车票要卖出,现在有5个售票点同时售票,用5个线程模拟这5个售票点的售票情况 ...

  2. c语言第六版第四章答案,C primer plus 第六版 第6版 004章 第四章 编程 练习 答案 中文...

    第四章 编程练习 1.编写一个程序,提示用户输入名和姓,然后以『名,姓』的格式打印出来. char name[40]; char family[40]; printf("请输入名和姓:\n& ...

  3. Java语言程序设计基础篇原书第十版第二章编程练习题答案

    程序如有问题,及时联系博主哦~博主会贴出所有带有星号的题目,以及一些典型的例子,希望能够帮助到同学们,也希望同学们都学好java语言. 2.1将摄氏温度转换为华氏温度 package nameyu;i ...

  4. C++ Primer Plus(第六版)第4章 编程练习答案详解

    第四章 1. 编写一个C++程序,如下述输出示例所示的那样请求并显示信息: What is your first name? Betty Sue What is your last name? Yew ...

  5. C++ Primer Plus(第六版)第6章 编程练习答案详解

    1. 编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列). #include<iostream& ...

  6. C语言程序设计现代方法(第二版)十二章编程题答案

    作为一名C语言初学者,以下答案为随手练习笔记,均已通过编译器实现书上功能,如有错误之处欢迎指出. 1. (a) # include <cstdio> # include <iostr ...

  7. C Primer Plus(第六版)第十六章 编程练习答案

    距离上次隔了十二天,好像是有点慢,期间还看了下C++pp,看到句话,每章内容不是很多,建议一周内完成一章,虽然之后要看琢石成器,C++也要顺带看一下.--11.16 CH16 Code answer ...

  8. 《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 5 章 编程练习答案

    1到4题比较简单,这里省略 5题6题代码 def main():prompt = input("Enter the name: ").lower().split()P = &quo ...

  9. C Primer Plus(第6版)第二章编程练习答案

    第二章  C语言概述(P37-P38) 5 编写一个程序,生成以下输出: Brazil, Russia, India, China India, China, Brazil, Russia 除了mai ...

最新文章

  1. Android studio 自动导入(全部)包 import
  2. 7-17 BCD解密(10 分)
  3. 《Python 黑科技》代理ip奇技淫巧
  4. sp 导出unity哪个_GitHub上发现的一个导出Unity3D场景数据的工具
  5. RoboCode的Intellij环境搭建
  6. linux典型压缩包操作 tar打包、压缩与解压
  7. 微型计算机原理与接口技术 考题,微机原理与接口技术试题答案
  8. Elastic 7.9 版本发布,提供免费的 Workplace Search 和终端安全功能
  9. 2020版本kali安装
  10. python怎么输出变量加文字书名_python的交互模式怎么输出名文汉字
  11. 手机打车APP的机遇与挑战
  12. CF447A DZY Loves Hash 模拟
  13. 金蝶EAS绿色客户端配置
  14. Android多行文本折叠展开效果
  15. 非洲 喀麦隆 Paul Epete CAGNOB 超大订单 骗局
  16. Myth源码解析系列之八- myth-admin事务管理后台
  17. Java代码给csv文件加水印_闪电PDF虚拟打印机如何给文件加上水印?
  18. 机票预订系统活动图_机票预订系统详细设计
  19. 如何读取PSD文件(photoshop)的图层......
  20. 两栏布局与三栏布局(圣杯布局与双飞翼布局)

热门文章

  1. Android Studio如何查看自己创建的SQLite数据库
  2. Mobile TV Vs IPTV
  3. QT基础学习笔记 Demo01
  4. SPSS MODELER笔记1----数据基本处理和整体方法概念
  5. 数据库MySQL系统实操实验从安装系统到实际操作全过程,五万字系列五,这不直接学完去学校装杯?
  6. GD32F4(3): 在keil软件中使用GD-LINK下载调试
  7. JS根据函数名字符串调用函数
  8. 文字识别ORC与公式识别
  9. 挺带劲!这款开源数据库迁移工具超牛逼
  10. NVIDIA GeFprce GTX 1080 Ti NVIDIA图形驱动程序版本466.77下载和安装