《C++ Primer Plus》(第6版)第5章编程练习

  • 《C++ Primer Plus》(第6版)第5章编程练习
    • 1. 计算闭区间内的整数和
    • 2. 重新编写程序清单5.4
    • 3. 累加
    • 4. 投资价值
    • 5. 销售情况
    • 6. 销售情况2
    • 7. 汽车
    • 8. 统计单词数量
    • 9. 统计单词数量2
    • 10. 嵌套循环

《C++ Primer Plus》(第6版)第5章编程练习

1. 计算闭区间内的整数和

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

代码:

#include <iostream>
using namespace std;
int main()
{int a, b, sum = 0;cout << "Please give me two numbers a and b(a<b),\n";cout << " and I will calculate the sum of the integers in the interval [a,b].\n";cout << "a: ";cin >> a;cout << "b: ";cin >> b;for (int i = a; i <= b; i++)sum += i;cout << "result:" << sum << endl;system("pause");return 0;
}

运行结果:

2. 重新编写程序清单5.4

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

代码:

#include <iostream>
#include <array>
using namespace std;
const int ArrSize = 101;int main()
{array<long double, ArrSize> factorials;factorials[0] = factorials[1] = 1;for (int i = 2; i < ArrSize; i++)factorials[i] = i * factorials[i - 1];cout << "100!=" << factorials[100] << endl;system("pause");return 0;
}

运行结果:

3. 累加

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

代码:

#include <iostream>
using namespace std;int main()
{int num, sum = 0;cout << "Please enter the number,\n";cout << "and I will calculate the cumulative sum of the input.\n";cout << "(enter 0 to end the program)" << endl;while (cin >> num && num != 0){sum += num;cout << "total:" << sum << endl;}cout << "Done!\n";system("pause");return 0;
}

运行结果:

4. 投资价值

Daphne 以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:
利息=0.10×原始存款
而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息的5%,:
利息=0.05×当前存款
Cleo在第一年投资100美元的盈利是5%——得到了105美元。下一年的盈利是105美元的5%—–即5.25美元,依此类推。请编写一个程序,计算多少年后,Cleo 的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。

代码:

#include <iostream>
using namespace std;
#define SIMPLE_RATE 0.10
#define COMPOUND_RATE 0.05
int main()
{double Daphne = 100, Cleo = 100;double principal = 100;unsigned year = 0;while (Cleo <= Daphne){Daphne += SIMPLE_RATE * principal;Cleo += COMPOUND_RATE * Cleo;year++;// cout << "year " << year << ":\n";// cout << "Daphne:" << Daphne << endl;// cout << "Cleo:" << Cleo << endl;}cout << year << " years later,";cout << " the investment value of Cleo exceeds the investment value of Daphne.\n";system("pause");return 0;
}

运行结果:

5. 销售情况

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

代码:

#include <iostream>
using namespace std;
#define MONTHS 12
const string months[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};int main()
{int sales[MONTHS];int sum = 0;for (int i = 0; i < MONTHS; i++){cout << "Please enter the sales volume for " << months[i] << ":";cin >> sales[i];}for (int i = 0; i < MONTHS; i++){cout << months[i] << " sales:" << sales[i] << endl;sum += sales[i];}cout << "Annual sales:" << sum << endl;system("pause");return 0;
}

运行结果:

6. 销售情况2

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

代码:

#include <iostream>
using namespace std;
#define YEAR 3
#define MONTH 12
const string months[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};int main()
{int sales[YEAR][MONTH] = {0};int annualSales[YEAR] = {0};int sum = 0;for (int i = 0; i < YEAR; i++){for (int j = 0; j < MONTH; j++){cout << "Please enter the sales volume for year " << i + 1 << " month " << months[j] << ":";cin >> sales[i][j];}}for (int i = 0; i < YEAR; i++){for (int j = 0; j < MONTH; j++){cout << "year " << i + 1 << " month " << months[i] << " sales:" << sales[i][j] << endl;annualSales[i] += sales[i][j];}}cout << "Annual sales:\n";for (int i = 0; i < YEAR; i++){cout << "year " << i + 1 << ":" << annualSales[i] << endl;sum += annualSales[i];}cout << "Total sales:" << sum << endl;system("pause");return 0;
}

7. 汽车

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

How many cars do you wish to catalog? 2car #1:
Please enter the make: Hudson  HornetPlease enter the year made: 1952
car #2:
please  efter themake: Kaiserplease enter  the year made:1951Here is your colrection :
1952 Hudson Hornet
1951 Kaiser

代码:

#include <iostream>
#include <string>
using namespace std;
struct Car
{string producer;int madeYear;
};
int main()
{int num = 0;cout << "How many cars do you wish to catalog? ";cin >> num;cin.get();Car *cars = new Car[num];for (int i = 0; i < num; i++){cout << "car #" << i + 1 << ":\n";cout << "Please enter the make: ";getline(cin, cars[i].producer);cout << "Please enter the year made: ";cin >> cars[i].madeYear;cin.get();}cout << "Here is your collection:\n";for (int i = 0; i < num; i++)cout << cars[i].madeYear << " " << cars[i].producer << endl;system("pause");return 0;
}

运行结果:

8. 统计单词数量

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

Enter words (to stop, type the word done):
anteater birthday category  dumpster
envy finagle geometry done for sure
You  entered a total of 7 words.

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

代码:

#include <iostream>
#include <cstring>
#define ArrSize 30
using namespace std;int main()
{char word[ArrSize];int count = 0;cout << "Enter words (to stop, type the word done):\n";while (cin >> word && strcmp("done", word))count++;cout << "You  entered a total of " << count << " words.\n";system("pause");return 0;
}

运行结果:

9. 统计单词数量2

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

代码:

#include <iostream>
#include <string>
using namespace std;int main()
{string word;int count = 0;cout << "Enter words (to stop, type the word done):\n";while (cin >> word && word != "done")count++;cout << "You  entered a total of " << count << " words.\n";system("pause");return 0;
}

运行结果:

10. 嵌套循环

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

Enter number of rows: 5
....*
...**
..***
.****
*****

代码:

#include <iostream>
using namespace std;int main()
{int num = 0;cout << "Enter number of rows: ";cin >> num;for (int i = 1; i <= num; i++){for (int j = 0; j < num - i; j++)cout << ".";for (int j = 0; j < i; j++)cout << "*";cout << endl;}system("pause");return 0;
}

运行结果:

《C++ Primer Plus》(第6版)第5章编程练习相关推荐

  1. C++ Primer Plus第六版第六章编程练习 第4题, 加入Benevolent Order of Programmer后,在BOP大会上

    /*************************************************************************************************** ...

  2. c++ primer plus第六版第六章编程练习

    编写程序读取键盘输入,回显除数字外字符,同时大写转小写,小写转大写,遇'@'就停止. //练习6.1 读取键盘输入,回显输出(除数字),另外大写字母和小写字母互转,遇"@"则退出程 ...

  3. C Primer Plus第六版第七章编程题目与参考答案⭐

    1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数.换行符数和所有其他字符的数量. #include <stdio.h> #define STOP '#' #define SP ...

  4. C++primer plus第六版第四章编程题代码

    1. #include "stdafx.h" #include <iostream> #include <string> #include <vect ...

  5. C Primer Plus第六版第四章编程题目与参考答案⭐

    1.编写一个程序,提示用户输入名和姓,然后以"名,姓"的格式打印出来. #include <stdio.h>int main() {char firstname[20] ...

  6. C++ Primer Plus 第六版 所有章节课后编程练习答案

    我的独立博客地址:www.blog4jimmy.com,欢迎大家关注 下面的是C++ Primer Plus 第六版所有章节的课后编程练习的答案,都是博主自己写的,有不对的地方请大家留言指出讨论讨论 ...

  7. C Primer Plus第六版第五章运算符,表达式,语句源码

    源码 //5.1#include<stdio.h> #define ADJUST 7.31 int main(void) {const double SCALE = 0.333;doubl ...

  8. C Primer Plus(第六版)第三章 数据和C

    笔记记录 1.float 类型可以储存带小数的数字. 2.printf()中使用%f来处理浮点值.%.2f中的.2用于精确控制输出,指定输出的浮点数只显示小数点后面两位. 3.scanf()函数用于读 ...

  9. c++primer plus 第六版 第六章重点内容总结 以及编程题答案

    1.if中的trick 当char型字符使用cout输出时,并且在使用运算符之后再cout的时候,一定要强制转化回char类型.否则打印出的是数字(字符的编码),if else中的两种操作如果需要多条 ...

  10. C Primer Plus (第六版) 第七章 7.11编程练习 参考答案

    11.ABC 邮购杂货店出售的洋蓟售价为 2.05 美元/磅,甜菜售价为 1.15美元/磅,胡萝卜售价为 1.09美元/磅.在添加运费之前,100美元的订单有5%的打折优惠.少于或等于5磅的订单收取6 ...

最新文章

  1. android录像增加时间记录(源码里修改)
  2. c语言ascii图形输出,C语言实例10——有关ASCII图形的输出
  3. android 控件描边取消重叠
  4. c语言三级上机题库,2006年9月全国等级考试三级c语言上机题库(三十四)
  5. eclipse中怎么安装spring插件_安装eclipse中容易遇到的问题
  6. PyTorch学习—19.模型的加载与保存(序列化与反序列化)
  7. JAVA 中文乱码 idea设置
  8. 刷新计算机dns缓存的命令,Windows系统刷新DNS缓存命令是什么?Win7系统清除DNS缓存方法...
  9. css3实现动画的三种方式
  10. opencv证件照变更任意底色
  11. AI 计算能力TOPS
  12. 蛮X搜神记的NetManager分析(1)
  13. 交出2021年进步答卷,挚文集团如何做到稳中求进?
  14. SAP部署SSL数字证书
  15. Stable Diffusion6
  16. 云平台是什么?知名云平台有哪些?
  17. layui框架KIT ADMIN后台管理系统模板
  18. (1366, Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...'for column 'VARIABLE_VALUE' at489
  19. 数据库的三级模式和两级映射--简单介绍
  20. 【颜色代码】代码中16进制表示颜色

热门文章

  1. 视觉SLAM⑪----回环检测
  2. 秒杀系统面临哪些技术难题
  3. 关于某网盘解除下载限速(亲测有效)
  4. comtrade文件C语言,生成的comtrade.dat文件有关问题,波形分析软件打开看点是乱的...
  5. 区块链编程初学者指南
  6. 微信多媒体团队访谈:音视频开发的学习、微信的音视频技术和挑战等
  7. 0x0000007b 电脑蓝屏的解决方法
  8. 网站加了外链会在前面自动加上自己网页的网址
  9. Windows10环境下同时下载了jdk8和jdk17的环境变量如何配置
  10. 【shell脚本】——归档文件脚本