第五章 循环和关系表达式

1、编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有整数的和为44。

   分析:简单的for循环

 1 #include<iostream>2 using namespace std;3 int main(){4     int min, max;5     cout << "Enter first number: ";6     cin >> min;7     cout << "Enter second number: "; 8 cin >> max; 9 int i, s=0; 10 for(i=min; i<=max; i++){ 11 s += i; 12  } 13 cout << s; 14 }

2、 使用array对象(而不是数组)和long double(而不是long long)重新编写程序清单5.4,并计算100!的值。

    分析:简单的for循环

 1 #include "iostream"2 #include "array"3 using namespace std;4 int main()5 {6     array<long double, 100> myArray;7     myArray[0] = 1;8     for (int i = 1; i < 100; i++) { 9 myArray[i] = (i + 1)*myArray[i - 1]; 10 cout << i + 1 << "! = " << myArray[i]; 11 cout << endl; 12  } 13 return 0; 14 }

3、编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有的输入累计和。当用户输入0时,程序结束

   分析:简单的while循环

 1 #include<iostream>2 using namespace std;3 int main(){4     int number;    //输入5     int total = 0;    //总和 6     cin >> number;7     while(number!=0){8          total += number;9          cout << "total: " << total << endl; 10 cin >> number; 11  } 12 }

4、 Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%(利息=0.10*原始存款)。而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%(利息=0.05*当前存款)。请计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两人的投资价值。

     分析:do...while的简单应用

 1 #include<iostream>2 using namespace std;3 int main(){4     double d=100, c=100;5     int count = 0;6     do{7         d += 10.0; 8 c *= 1.05; 9 count++; 10 }while(d>c); 11 cout << "after " << count << " years "; 12 cout << "Cleo pass Daphne" <<endl; 13 cout << "Cleo: " << c <<endl; 14 cout << "Daphne: " << d <<endl; 15 }

5、 假设要销售《C++ For Fools》一书,请编写一个程序,输入全年中每一年的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char * 数组(或string对象数组)逐月进行提示,并将输入的数据存储在一个int数组中。然后,程序计算数组中各元素的总和,并报告这一年的销售情况。

    分析:for循环的简单运用

 1 #include<iostream>2 #include<string>3 using namespace std;4 int main(){5     string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};6     int sales[12];7     int count = 0;8     cout << "Enter sales per month" << endl; 9 for(int i=0; i<12; i++){ 10 cout << months[i] << ": "; 11 cin >> sales[i]; 12  } 13 for(int i=0; i<12; i++) 14 count += sales[i]; 15 cout << "The total sales of this year is " << count; 16 }

6、 完成编程练习5,但这一次使用一个二维数组来存储输入——3年中每个月的销售量,程序将报告每年销售量以及三年的总销售量。

    分析:for循环的简单运用

 1 #include<iostream>2 #include<string>3 using namespace std;4 int main(){5     string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};6     int sales[3][12];7     int total1 = 0, total2 = 0, total3 = 0;        //每年总销量 8     cout << "Enter sales per month" << endl;9     for(int i=0; i<3; i++){ 10 for(int j=0; j<12; j++){ 11 cout << i+1 << " year " << months[j] << ": "; 12 cin >> sales[i][j]; 13 if(i==0) 14 total1 += sales[i][j]; 15 if(i==1) 16 total2 += sales[i][j]; 17 if(i==2) 18 total3 += sales[i][j]; 19  } 20 cout << endl; 21  } 22 cout << "first year: " << total1 << endl; 23 cout << "second year: " << total2 << endl; 24 cout << "third year: " << total3 << endl; 25 cout << "total: " << total1 + total2 + total3; 26 }

7、设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组或string对象中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能有多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串。最后,程序将显示每个结构的内容。改程序的运行情况如下:

   How many cars do you wish to catalog? 2

   Car #1:

   Please enter the make: Hudson Hornet

   Please enter the year made: 1952

   Car #2:

    Please enter the make: Kaiser

    Please enter the year made: 1951

    Here is your collection:

    1952 Hudson Hornet

    1951 Kaiser

    分析:综合第四章知识

 1 #include<iostream>2 #include<string>3 using namespace std;4 struct car{5     string make;    //生产厂商 6     int year;        //生产年份 7 };8 int main(){9     int n;
10     cout << "How many cars do yuou wish to catalog? "; 11 cin >> n; 12 cin.ignore(); //防止使用getline()函数时出现无法输入的bug 13 car * cars = new car[n]; //动态创建结构体数组 14 for(int i=0; i<n; i++){ 15 cout << "Car #" << i+1 <<endl; 16 cout << "Please enter the make: "; 17  getline(cin, cars[i].make); 18 cout << "Please enter the year made: "; 19 cin >> cars[i].year; 20  cin.ignore(); 21  } 22 cout << "Here is your collection:" << endl; 23 for(int i=0; i<n; i++){ 24 cout << cars[i].year << " " << cars[i].make <<endl; 25  } 26 } 

    cin在getline(cin, str)之前使用时要在cin后加cin.ignore()来解决getline(cin, str)读取回车的问题

8*、编写一个程序,他使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少单词(不包括done在内)。下面是该程序的运行情况:

   Enter words (to stop, type the word done):

   anteater birthday category dumpster

   envy finagle geometry done for sure

   You enter a total of 7words.

   你应在程序中包含头文件cstring,并使用strcmp( )来进行比较测试。

 1 #include<iostream>2 #include<cstring>3 using namespace std;4 int main()5 {6     char word[20];7     int num = 0;8     cout << "Enter words (to stop, type the word done):" << endl; 9 cin >> word; 10 while(strcmp(word , "done") != 0) 11  { 12 if(bool(cin >> word) == true) 13  { 14 num++; 15  } 16  } 17 cout << "You entered a total of " << num << " words." << endl; 18 return 0; 19 } 

9*、编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。

 1 #include<iostream>2 #include<string>3 using namespace std;4 int main(){5     string word;6     int num = 0;7     cout << "Enter words (to stop, type the word done):" << endl;8     cin >> word; 9 while(word!="done") 10  { 11 if(bool(cin >> word) == true) 12  { 13 num++; 14  } 15  } 16 cout << "You entered a total of " << num << " words." << endl; 17 return 0; 18 }

10、编写一个使用循环嵌套的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包含一个星号,第二行包含两个星号,依次类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。

 1 #include<iostream>2 using namespace std;3 int main(){4     int n;5     cin >> n;6     for(int i=0; i<n; i++){7         for(int j=0; j<n-i-1; j++) 8 cout << "."; 9 for(int j=0; j<i+1; j++) 10 cout << "*"; 11 cout << endl; 12  } 13 }

转载于:https://www.cnblogs.com/SChenqi/p/9662982.html

C++ Primer Plus章节编程练习(第五章)相关推荐

  1. Java7并发编程指南——第五章:Fork Join框架

    Java7并发编程指南--第五章:Fork Join框架 @(并发和IO流) Java7并发编程指南第五章Fork Join框架 思维导图 项目代码 思维导图 项目代码 GitHub:Java7Con ...

  2. C++ Primer Plus课后编程练习第6章参考代码

    (C++ Primer Plus课后编程练习第6章参考代码) 声明: 作者入门小白,将学习过程中的代码做一些分享,仅供大家参考,欢迎大家交流指正.全部编译运行过,水平有限,不喜勿喷. 环境: Wind ...

  3. 浙大PTA-Python题库 编程题第五章(5-1~5-11)题解

    其他各章题解链接如下 浙大PTA-Python题库 编程题第一章(1-1~1-3)题解 https://blog.csdn.net/zimuzi2019/article/details/1070206 ...

  4. core组件进阶(Opencv3编程入门 第五章) 第四节 图像对比度亮度调整

    core组件进阶(Opencv3编程入门 第五章) 第四节 图像对比度亮度调整 #include <opencv2/core/core.hpp> #include <opencv2/ ...

  5. [转]Windows Shell编程 第十五章【来源:http://blog.csdn.net/wangqiulin123456/article/details/7988016】...

    第十五章 SHELL扩展 谈到Windows Shell编程,Shell扩展是最重要的科目之一,绝大多数商业应用的最酷特征的都是通过Shell扩展实现的,而且有许多显著的系统特征实际都是插入了扩展代码 ...

  6. Windows Shell 扩展编程 第十五章

    转自:http://blog.csdn.net/chchzh/article/details/4597866 第十五章 SHELL扩展 谈到Windows Shell编程,Shell扩展是最重要的科目 ...

  7. 【Linux命令行与Shell脚本编程】第五章 理解 Shell 父子关系 后台进程 协程

    Linux命令行与Shell脚本编程 第五章 理解 Shell 文章目录 Linux命令行与Shell脚本编程 五,理解 Shell 5.1,shell的类型 5.2,shell的父子关系 5.2.1 ...

  8. C Primer Plus编程题-第五章 运算符、表达式和语句

    第一题: 编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间.使用#define或const创建一个表示60的符号常量或const变量.通过while循环让用户重复输入值,直到用户输入小于或 ...

  9. C Primer Plus 第六版编程练习第五章答案

    1,编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间.使用#define或const创建一个表示60的符号常量或const变量.通过while循环让用户重复输入值,直到用户输入小于或等于0 ...

最新文章

  1. 理想ONE“偷袭”豪华品牌 李想强调不会收取金融服务费 | 2019上海车展
  2. 在Init之前究竟执行了什么?
  3. Windows CE(C#)嵌入式应用开发pdf
  4. mysql semi join_MySQL 5.6 Semi join优化之materialization strategy
  5. 小程序 获取当前用户地址及地图显示
  6. JavaScript HTML DOM
  7. leetcode 144 --- 二叉树前序遍历
  8. html 表单命名规范,最佳实践:按HTML ID或名称属性访问表单元素?
  9. IDEA导入项目笔记
  10. 【转】使用EBNF相对于BNF表示的优越性
  11. Java—读取多层嵌套Json文件的内容并根据索引返回JsonObject
  12. 三款免费好用的Gif制作神器
  13. linux cpan 参数配置,Linux下使用CPAN进行Perl模块的安装
  14. RabbitMQ报错ERROR: node with name rabbit already running on lhg1
  15. 服务器配置防火墙的地址伪装和端口转发实例
  16. Flutter syncfusion_flutter_charts 饼图PieSeries使用
  17. 那段记忆中的声音:单式评书再现江湖背后的AI技术
  18. html在线表单生成,一种基于html5的在线表单设计系统的制作方法
  19. 实习周报 | 3rd week | 2021.4.19 - 4.23 | 文本摘要,ES语义搜索,SOTA SA
  20. 压缩感知的常见稀疏基名称及离散傅里叶变换基

热门文章

  1. VS2019编写简单的C程序示例
  2. Haskell语言实现求解一个整数所有因子的代码及运行结果
  3. Elasticsearch之mapping映射入门
  4. python多线程读取文件夹下的文件_是否可以使用python多线程从文件夹数读取文件数,并处理这些文件以获得组合结果?...
  5. tensorflowgpu利用率为0_奥普特冲科:曾侵犯竞争对手商业机密,产能利用率或虚高...
  6. 学计算机趣图,我的世界:六张玩家自制趣图,最后一张,想起了“骗”父母买电脑...
  7. 【科普】为什么ip地址通常以192.168开头?
  8. Kubernetes 集群升级指南:从理论到实践
  9. SpringCloud 应用在 Kubernetes 上的最佳实践 — 线上发布(可回滚)
  10. html怎么显示直线,html怎么用鼠标画出一条直线,鼠标移动时候要能看到线条