《C++ primer plus》第5章练习题
1.输入两个整数,输出两个整数之间所有整数的和,包括两个整数。
#include<iostream>
using namespace std; int main()
{
int num1, num2,num_left,num_right,sum = 0; cout << "Input two integers:" << endl; cin >> num1 >> num2; //比较大小,从小的开始累加
num_left = num1 < num2 ? num1 : num2;
num_right = num1 > num2 ? num1 : num2; for (int i = 0; (num_left+i) <= num_right; i++)
{
sum += num_left + i;
} cout << "Sum of all integers between the two numbers:" << sum << endl; system("pause"); }
2.输入一个整数,计算它的阶乘,要能够计算100的阶乘(使用long double)。
#include<iostream>
using namespace std; int main()
{
double num;
long double res; cin >> num; res = num; for (int i = 1; num - i >0; i++)
{
res *= (num - i);
} cout << res << endl; system("pause"); }
3.每次输入一个数,输出:到目前为止,前面输入的所有数的和。输入0结束。
#include<iostream>
using namespace std; int main()
{
double input_number, sum = 0; cout << "Input a number to add(input 0 to quit):" << endl; cin >> input_number; while (input_number)
{
sum += input_number; cout << "Until now,the sum of all numbers before: " << sum << endl;
cout << "Input next number to add(input 0 to quit):" << endl; cin >> input_number;
} cout << "Final result:" << sum << endl;
cout << "done." << endl; system("pause"); }
4.Daphne进行单利投资,Cleo进行复利投资,两人都投资100美元。Daphne每年的利息(收益)是:原始存款×0.10,Cleo每年的利息(收益)是:当前存款×0.05。也就是说,Daphne每年固定盈利100×0.10=10美元;Cleo今年投资100美元,按5%盈利,下一年的盈利就是5美元,下下年的盈利就是105×5%=5.25美元。计算多少年后,Cleo的投资价值才能超过Daphne,并显示此时两人的投资价值。
#include<iostream>
using namespace std; const double ratio_D = 0.1, ratio_C = 0.05; int main()
{
double mon_D = 100, mon_C = 100;
double intst_D = mon_D * ratio_D; int y = 0;
do
{
++y;
mon_D = mon_D + intst_D;
mon_C = (1+ratio_C)*mon_C;
} while (mon_C < mon_D); cout << "After " << y << " years." << endl;
cout << "Daphne:$" << mon_D << "\tCleo:$" << mon_C << endl; system("pause"); }
5.假设销售一本书,用char数组(或string对象数组)提示用户输入一年中所有月份的销售量,将输入的销售量存储在一个int数组中,然后程序计算数组中元素的总和,报告一年的销售情况。
#include<iostream>
#include<string>
using namespace std; int main()
{
string prmt[] =
{ "January","February","March","April",
"May","June","July","August",
"September","October","November","December"
}; int booksales[12],sales_sum = 0; for (int i = 0; i < 12; i++)
{
cout << "Input books sales in " << prmt[i] << ":\n";
cin >> booksales[i];
sales_sum += booksales[i];
} cout << "The whole sales in this year is:" << sales_sum << endl; system("pause"); }
6.在第5题的基础上修改程序,使用二维数组来存储输入——3年中每个月的销售量,程序将报告每年的销售量和三年的总销售量。
#include<iostream>
#include<string>
using namespace std; const int years = 3; int main()
{
string prmt[] =
{ "January","February","March","April",
"May","June","July","August",
"September","October","November","December"
}; int booksales[years][12], sum_py[years] = {},sum_ay = 0; for ( int y = 0; y < years; y++)
{
cout << "\t|| Books sales for " << "YEAR " << y + 1 << " ||\n\n";
for (int m = 0; m < 12; m++)
{
cout << "Input books sales in " << prmt[m] << ":\n";
cin >> booksales[y][m];
sum_py[y] += booksales[y][m];
}
cout << "\nBooks sales in " << "YEAR " << y + 1 << " is:" << sum_py[y] << "\n\n";
} for (int i = 0; i < years; i++)
sum_ay += sum_py[i]; cout << "Books sales of " << years << " years:" << sum_ay << endl; system("pause"); }
7.设计一个car结构,存储汽车的生产商(字符数组或string对象)和生产年份(整数)。编写程序,向用户询问有多少辆车。随后,程序使用new创建一个由相应数量的car结构组成的动态数组。然后,程序依次提示用户输入生产商和年份。最后,输出所有存储的信息。
#include<iostream>
using namespace std; struct car_product
{
char producer[20];
int year;
}; int main()
{
int counts; cout << "How many cars do your wish catalog? ";
cin >> counts;
cin.get(); //清空缓冲区的换行符,防止后面cin.get()停止 car_product *ptr = new car_product[counts]; for (int i = 0; i < counts; i++)
{
cout << "Car #" << i+1 << ":\n";
cout << "Please enter the make: ";
cin.get(ptr[i].producer,20); //cin.get()会读取整行字符,包括空格,遇到换行符停止
cout << "Please enter the year made: ";
cin >> ptr[i].year;
cin.get(); //同上,清空缓冲区的换行符
} cout << "Here is your collection:" << endl;
for (int i = 0; i < counts; i++)
{
cout << ptr[i].year << " " << ptr[i].producer << endl;
} delete[]ptr; system("pause"); }
*要特别注意其中cin和cin.get()的用法。这里学习了输入流的概念,用户的输入都会预先存到缓冲区内,cin有关的输入会先从缓冲区内取数据。cin取数据时,遇到空格或换行符都会停止,取完数据后,会留下换行符。而cin.get()会把空格也读入,遇到换行符停止,同样也会把换行符留在缓冲区内。
*cin.get()不指定读取到哪个对象和长度,会默认读走一个字符,所以也可以起到清空缓冲区的作用。
8.用户输入一系列单词,中间用空格隔开。程序使用char数组存储所有单词,然后统计单词“done”之前有多少个单词。
#include<iostream>
using namespace std; const int MAXSIZE = 100; int main()
{
char store[MAXSIZE];
int counts = 0; cin.get(store,MAXSIZE); for (int i = 0; i<MAXSIZE ;i++)
{
if (store[i] == ' ')
counts++;
else if ((store[i] == 'd') && (store[i + 1] == 'o') && (store[i + 2] == 'n') && (store[i + 3] == 'e'))
break;
else {};
} cout << "You entered a total of "<<counts<<" words.\n"; system("pause"); }
9.编写循环嵌套程序,要求用户输入一个值,指出要显示多少行,然后,程序按下面的规律显示,假如输入的是5:
....*
...**
..***
.****
*****
第一行显示一个星号,其余用点补充,下面以此类推,直到第五行显示出五个星号。
#include<iostream>
using namespace std; int main()
{
int num; cout << "Enter number of rows: ";
cin >> num; for (int i = 0; i < num; i++)
{
for (int j = num-1; j >i; j--)
{
cout << ".";
} for (int k = 0; k <= i; k++)
{
cout << "*";
} cout << "\n";
} system("pause"); }
《C++ primer plus》第5章练习题的更多相关文章
- C Primer Plus_第6章_循环_编程练习
1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...
- C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- 《C++ primer plus》第3章练习题
注:有的题设条件自己改动了一下,比如英寸英尺改成米和厘米,部分题目自己加了点额外要求. 1.要求用户输入身高(m),用下划线提示用户输入,将身高转化成"米"加"厘米&qu ...
- C Primer Plus 第3章 数据和C 编程练习
1. /* 整数上溢 */ #include <stdio.h> int main(void) { ; unsigned ; /* 无符号整数j像一个汽车里程指示表(形容的太好了,可参考& ...
- C++ Primer 5th 第1章 开始
*****代码在Ubuntu g++ 5.31 / clang++ 3.8(C++11)下编写调试***** 每个C++程序必须有一个main( )函数,main( )函数的返回值也必须是int类型, ...
- C++ Primer 笔记 第三章
C++ Primer 第三章 标准库类型 3.1using声明 例: using namespace atd; using std::cin; 3.2string类型 初始化方式 string s1 ...
- C++primer拾遗(第二章:变量和基本类型)
这是我对c++primer第二章的一个整理总结,算是比较适用于我自己吧,一小部分感觉不用提及的就省略了,只提了一下平时不注意,或者不好记住的内容. 排版太费劲了,直接放了图片格式.从自己的oneNot ...
- python第一章练习题
本章总节 练习题 1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型:把源代码编译成机器语言的可执行文件,程序执行的时候执行可执行文件即可. 优点:程序执行不 ...
随机推荐
- day44:CSS选择器优先级&JS基础
目录 1.CSS选择器优先级 2.补充:margin是可以设置百分比的 3.JS 3.1 js代码的引入方式 3.2 变量 3.3 数据类型 3.4 数组(类似于python中的列表) 3.5 自 ...
- 高可用集群corosync+pacemaker之crmsh使用(二)
上一篇博客我们聊到了crmsh的安装以及配置一个资源到corosync+pacemaker高可用集群上的相关命令的用法,回顾请参考https://www.cnblogs.com/qiuhom-1874 ...
- Unity坐标系详解
1. World Space(世界坐标系): 我们在场景中添加的物体(如:Cube),他们都是以世界坐标显示在场景中.transform.position 获取的便是这个 坐标数值. 2. Scene ...
- 条件竞争(race condition)
条件竞争漏洞是一种服务器端的漏洞,由于服务器端在处理不同用户的请求时是并发进行的,因此,如果并发处理不当或相关操作逻辑顺序设计的不合理时,将会导致此类问题的发生. 参考了一些资料,发现一个比较能说明问 ...
- google protocol buffer——protobuf的问题及改进一
这一系列文章主要是对protocol buffer这种编码格式的使用方式.特点.使用技巧进行说明,并在原生protobuf的基础上进行扩展和优化,使得它能更好地为我们服务. 在上一篇文章中,我们完整了 ...
- npm 进阶命令知多少(一)
npm命令知多少(一) 前言 作为前端模块化扎展现形式的npm包,已经在前端开发中不可或缺,熟练掌握npm相关内容,也是前端开发者的一门必修课,那么除了npm publish这类常见内容之外,还有哪些 ...
- Python - 网易邮箱邮件阅读和删除辅助小脚本
摘要:[原创]转载请注明作者Johnthegreat和本文链接 简介:在Windows下的网易邮箱大师客户端中,阅读邮件时,可以使用快捷键Delete删除邮件,然后自动跳到下一封,如果再按一次Dele ...
- 3.AVPacket使用
1.使用注意 AVPacket需要用户通过av_packet_allc()创建好空间后.才能供给fimpeg进行获取解码前帧数据,由于解码前帧数据大小是不固定的(比如I帧数据量最大)所以ffmpeg会 ...
- @RequestBody使用说明
@RequestBody 使用 @RequestBody这个对于一般刚接触来说,确实有点陌生,但是现在前端,后端技术分的太细,越来越多的技术层出不穷,前端就分化出POST ,GET,PUT,DELET ...
- oracle之二redo日志
redo 日志 4.1 redo (重做) log 的功能:数据recovery4.2 redo log 特征: 1)记录数据库的变化(DML.DDL) 2)用于数据块的recover ...