C++ Primer Plus 自学第五章结尾编程10题
第一题
要求:输入两个整数,计算输出两个整数之间的所有整数和。
//5.1
#include<iostream>
int main()
{using namespace std;cout << "Enter two int numbers :\n";int a, b;cin >> a;cin >> b;while (a > b){int i;i = a,a = b, b = i;}int m = 0;for (int i = a; i <= b; i++)m += i;cout << a << " to " << b << " sum is " << m;return 0;
}
第二题
要求:用array对象重写程序。
//5.2
#include<iostream>
#include<array>
int main()
{using namespace std;array<long double, 101>factorials;factorials[0] = factorials[1] = (long double) 1;for (int i = 2; i < 101; i++){factorials[i] = i * factorials[i - 1];}for (int i = 0; i < 101; i++)cout << i << "! = " << factorials[i] << endl;return 0;}
第三题
要求:输入整数,得到0至该整数的和,并不断相加,直至输入0,程序停止,输出总和。
//5.3
#include<iostream>
int main()
{using namespace std;int m=0;int a=1;while (a != 0){cout << "Enter number ,the ending is entering 0 :\n";cin >> a;for (int b = 0; b < a; b++)m += b;cout << "The sum is " << m<<endl;}return 0;
}
第四题
要求:比较投资价值,输出超越年数以及此时价值。
//5.4
#include<iostream>
const double start_m = 100.0;
int main()
{using namespace std;double Daphne_m = 100.0;double Cleo_m = 100.0;double Daphne_f = 0.1;double Cleo_f = 0.05;int n;double m,q;for (n = 1; Daphne_m >= Cleo_m; n++){q= start_m * Daphne_f ;Daphne_m += q;Cleo_m = Cleo_m * (1 + Cleo_f);}cout << n << " years ago, Cleo_money >Daphne_money.\n";cout << "Daphne: " << Daphne_m << endl<< "Cleo: " << Cleo_m << endl;return 0;
}
第五题
要求:创建数组(月份与对应销售量),并计算输出销售量总额。
//5.5
#include<iostream>
const int year_per_months = 12;int main()
{using namespace std;cout << "Enter 12 months' books :\n";int books[year_per_months];const char* months[year_per_months] ={ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };int m = 0;for (int i = 0; i < year_per_months; i++){cin >> books[i];m += books[i];};for (int i = 0; i < year_per_months; i++){cout << months[i] << ":\t" << books[i] << endl;};cout << "Year's Sum is " << m;return 0;
}
第六题
要求:创建二维数组,报告三年内每月销售量,并计算总和。
//5.6
#include<iostream>
const int y_per_m = 12;
int main()
{using namespace std;cout << "2019 -- 2021 the books' sales :\n";int book[3][y_per_m];const char* months[y_per_m] ={ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };int m = 0;for (int i = 0; i < 3; i++){for (int n = 0; n < y_per_m; n++){cin >> book[i][n];m += book[i][n];}cout << "\n";}int sum = m;const int year[3] = { 2019,2020,2021 };for (int n = 0; n < y_per_m; n++){cout << "\t";cout << months[n];};cout << endl;for (int i = 0; i < 3; i++){cout << year[i] << "\t";for (int n = 0; n < y_per_m; n++){cout << book[i][n];cout << "\t";}cout << "\n";}cout << "The sales is " << sum;return 0;
}
第七题
要求:设计结构,创建动态数组,通过输入汽车数,循环输入输出每个结构成员信息。
//5.7
#include<iostream>
#include<string>
using namespace std;struct car
{string c_type;int c_time;
};int main()
{cout << "How many cars do you with to catalog ?\n";int m;cin >> m;car* cars = new car[m];for (int i = 0; i < m; i++){ cin.get();//在使用getline()之前需要加一个cin.get();cout << "Car #" << (i + 1)<<endl;cout << "Please enter the make : ";getline(cin, cars[i].c_type);cout << "Please enter the year made : ";cin >> cars[i].c_time;cout << endl;}cout << "Here is your collection ;\n";for (int i = 0; i < m; i++)cout << cars[i].c_time << "\t" << cars[i].c_type << endl;delete[] cars;return 0;
}
第八题
要求:用char数组循环读取单词,并指出输入单词数量,终止单词为‘’done‘’。
//5.8
#include<iostream>
#include<cstring>
int main()
{using namespace std;int count = 0;cout << "Enter words (to stop, type the word done ): \n";char word[20];cin >> word;while (strcmp(word,"done")){count++;cin >> word;}cout << "you entered a total of " << count << " words.";return 0;}
第九题
要求·:使用string类,完成第八题。
//5.9
#include<iostream>
#include<string>
int main()
{using namespace std;int count = 0;cout << "Enter words (to stop, type the word done ):\n";string word;cin >> word;while (word != "done"){count++;cin >> word;}cout << "you entered a total of " << count << " words.\n";return 0;
}
第十题
要求:创建嵌套循环,得到符号方阵。
//5.10
#include<iostream>
int main()
{using namespace std;cout << "Enter number of rows: ";int rows;cin >> rows;for (int i = 1; i <= rows; i++){for (int m = rows - i; m > 0; m--){cout << '.';};for (int n = 0; n < i ; n++){cout << '*';}cout << "\n";}return 0;
}
C++ Primer Plus 自学第五章结尾编程10题相关推荐
- C++ Primer Plus 自学第六章结尾编程9题
第一题 要求:输入字符串到@字符为止,回显除数字以外输入,将大小写字母相互转换. //6.1 #include<iostream> #include<cctype> int m ...
- C Primer Plus 第五章 复习题编程练习 答案
第五章 复习题&编程练习 复习题 1. 假设所有变量的类型都是int,下列各项变量的值是多少: 2. 假设所有变量的类型都是int,下列各项变量的值是多少: 3. 对下列各表达式求值: 4. ...
- 第五章第二十九题(显示日历)(Display calendars)
第五章第二十九题(显示日历)(Display calendars) **5.29(显示日历)编写程序,提示用户输入年份和代表该年的第一天是星期几的数字,然后在控制台上显示该年的日历表.例如,如果用户输 ...
- 猴子第一天摘下若干个桃子,当时吃了一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个谭浩强c语言第五章第十二题
题目 本题是谭浩强<c语言程序设计>第五章第十二题 题目:猴子第一天摘下若干个桃子,当时吃了一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第10天早上再想吃时,就只剩一个 ...
- 《C语言程序设计》谭浩强 第五版 编程10题解答2
<C语言程序设计>谭浩强 第五版 编程10题解答2 11.素数计算 编写程序计算500-800区间内素数的个数cnt,并按所求素数的值从大到小的顺序,再计算其间隔减.加之和,即第1个素数- ...
- Web前端开发精品课HTML CSS JavaScript基础教程第五章课后编程题答案
编程题: 图5-15所示为一个问卷调查的网页,请制作出来.要求:大标题用h1标签:小题目用h3标签:前两个问题使用有序列表:最后一个问题使用无序列表. 用VS2013新建ASP.NET空网站,添加新项 ...
- 《C Primer Plus》第五章-运算符 表达式和语句(笔记、复习题、编程题,副作用和序列点,升降级)
文章目录 运算符 表达式和语句 本章内容 循环简介 基本运算符 赋值运算符:= 几个术语:数据对象.左值.右值和运算符 加法运算符:+ 减法运算符:- 5.2.4 符号运算符:-和+ 乘法运算符:* ...
- 【C++ Primer】第十五章 友元、异常和其他 --之一---友元和嵌套类
一,友元 1)可以将类作为友元,友元类的所有方法都可以访问原始类的私有成员和保护成员. 2)下面例子介绍了 电视类和遥控器类,其中遥控器类为电视类的友元类 3)注意:友元关系不具对称性.即 A 是 B ...
- 《C Primer Plus》第五章 编程习题
目录 5.11.1 5.11.2 5.11.3 5.11.4 5.11.5 5.11.6 5.11.7 5.11.8 5.11.9 5.11.1 编写一个程序,把分钟表示的时间转换成小时和分钟表示的时 ...
最新文章
- 七夕大礼包:26个AI学习资源送给你!
- 即使会溢出,也能得到正确的结果
- 算法基础课-搜索与图论-spfa-AcWing 852. spfa判断负环:spfa求负环板子
- 鸿蒙开发-实现页面跳转与页面返回
- 如何解决SAP Structure CMST_SI_ENQ的DDIC_TYPE_INCONSISTENCY问题
- oracle中$的用法,关于expdp 中query用法小结
- 一加神秘新机入网:搭载天玑8100 首发长寿版150W超级闪充
- PXE-preboot execute environment
- 代理模式-Java实现-静态代理、动态代理
- 为什么网易云音乐总能知道你喜欢听什么歌?背后的原理竟然如此简单!
- 基于php的小区物业管理系统
- java开发名言_java实现收藏名言语句台词的app
- 小程序推送代码到远程库
- UnityShader学习教程之<详解uv坐标,c#类似uv坐标的值以及贴图操作>
- 考研数学二-汤家凤中值定理
- Anaconda安装并配置
- 蓄电池内阻测量系统设计
- 毛笔字体设计 让经典字体焕然新生
- Windows10数字权利
- 【python】python内置函数——print()打印输出信息
热门文章
- 【高级人工智能】国科大《高级人工智能》联结主义 笔记 + 考试回忆
- 服务器删除登录日志文件,CentOS查看登录日志及其它安全日志 清空删除系统日志的方法...
- 字体库(阿里巴巴矢量图标库)使用两三坑
- Cisco Firepower NGFW FTD Software 7.1.0 下载 - 思科下一代防火墙
- VS code ssh 远程连接超算
- macos 查找文件_如何在Windows和macOS上查找和安装颜色配置文件以获取更准确的显示器...
- 中国铁塔成立6周年 | 致敬时光,致敬奋斗者!
- 从乙方到甲方,我用了六年的时间
- RFID车辆管理|RFID智能停车场管理系统应用
- VM虚拟机用Linux系统安装MySQL,文字总结版