c+primer中文版习题答案(CPrimerplus)
c+primer中文版习题答案(CPrimerplus)
2024-07-01 07:32:03  作者:远离碍  网址:https://m.xinb2b.cn/know/msn411196.html

c+primer中文版习题答案(CPrimerplus)(1)

第五章内容参考上一篇的技术点分析:

[C Primer plus 心得]5.循环关系表达式

1.编写一个要求用户输入两个整数的程序,该程序将计算并输出这两个整数之间(包括这两个整数)所有的整数的和。

这里假设先输入较小的整数,例如如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44.

答案:

#include <iostream>using namespace std;int main(void){ int first = 0; int second = 0; int sum = 0; cout << "Please Enter two integer number:\n"; cin >> first; cin >> second; for (int i = first; i <= second; i ) { sum = i; } cout << "The sum value is " << sum <<endl; return 0;}

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

答案:

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

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

答案:

#include <iostream>using namespace std;int main(){ int input = 0; int sum = 0; cout << "Please Enter integer number:\n"; cin >> input; while (input != 0) { sum = input; cout << "The total input value is " << sum <<endl; cin >> input; } return 0;}

4.Daphne以10%的单利投资了100美元,每一年利润都是投资额的10%,即每年10美元。

而Cleo以5%的复利投资了100美元,利息是当前存款(包括获得的利息)的5%,Cleo在第一年投资100美元的盈利是5%得到了105美元,下一年的盈利是105美元的5%,以此类推编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示两个人的投资价值。

答案:

#include <iostream>using namespace std;int main(){ double daphne_value = 100; double cleo_value = 100; int count = 0; while (cleo_value <= daphne_value) { daphne_value = 100*0.1; cleo_value = daphne_value * 0.05; count ; } cout << "count is "<< count <<endl; cout << "daphne's value is " << daphne_value <<endl; cout << "Cleo's value is " << cleo_value <<endl; return 0;}

5.假设要销售《c for fool》一书。请编写一个程序,输入全年中每个月的销售量(图书数量而不是销售额).

程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组) 逐月进行提示,并将输入的数据存储在一个int数组中,然后程序计算数组中各元素的总数,并报告这一年的销售情况。

答案:

#include <iostream>#include <array>using namespace std;int main(){ int arry_size = 12; int total = 0; int sales[arry_size] = {0}; string month[arry_size]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "}; for (int i =0; i< arry_size; i ) { cout<<"Please enter the sales of"<<month[i]<<"is: "; cin>>sales[i]; total = sales[i]; } for(int i = 0; i < arry_size; i ) { cout<<"The sales of "<<month[i]<<" is: "<<sales[i]<<endl; } cout<<"The sales of a year is "<<total<<endl; return 0;}

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

答案:

#include <iostream>using namespace std;int main(){ int arry_size = 12; int year_size = 3; int total[year_size] = {0}; int sales[year_size][arry_size] = {0}; string month[arry_size]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "}; for (int i = 0; i< year_size; i ) { for (int j = 0; j < arry_size; j ) { cout<<"Please enter the sales of"<<month[i]<<"in: " << (i 1) << " years is :"; cin>>sales[i][j]; total[i] = sales[i][j]; } } for(int i = 0; i < arry_size; i ) { cout<<"The sales of "<<(i 1)<<" year is: "<<total[i]<<endl; } cout<<"The sales of three year is "<<(total[0] total[1] total[2])<<endl; return 0;}

7.设计一个名为car的结构,用它存储下述有关汽车的信息: 生产商(存储在字符数组或string对象中的字符串)、 生产年份(整数)。

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

答案:

#include <iostream>using namespace std;struct car{ string manufacturer; int year;};int main(){ int count = 0; car *car_info =NULL; cout << "How many cars do you wish to catalog?\n"; cin >> count; car_info = new car[count]; for (int i = 0; i < count; i ) { cout << "Please enter the manufacturer of " << (i 1) << " car:"; cin >> car_info[i].manufacturer; cout << "Please enter the year of " << (i 1) << " car:"; cin >> car_info[i].year; } cout<<"Here is your collection:"<<endl; for (int i = 0; i < count; i ) { cout << "The "<< (i 1) << " car's manufacturer is "<<car_info[i].manufacturer; cout << " and the year is "<<car_info[i].year <<endl; } delete []car_info; 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 toal of 7 words.

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

答案:

#include <iostream>#include <cstring>using namespace std;int main(){ int count = 0; char words[50] = {0}; cout << "Enter words (to stop, type the word done)\n"; cin >> words; while(strcmp(words, "done") != 0) { count ; cin >> words; } cout <<"for sure You entered a toal of " << count << " words" << endl; return 0;}

9.编写一个满足前一个练习中描述的程序,但是用string对象而不是字符数组。

请在程序中包含头文件string,并使用关系运算符来进行比较测试。

答案:

#include <iostream>#include <string>using namespace std;int main(){ int count = 0; string words; cout << "Enter words (to stop, type the word done)\n"; cin >> words; while(words != "done") { count ; cin >> words; } cout <<"for sure You entered a toal of " << count << " words" << endl; return 0;}

10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行,

然后程序将显示相应的行数的星号,其中第一行包括一个星号,第二行包括两个星号,以此类推,每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。

输出:

Enter number of rows: 5

. . . . *

. . . * *

. . * * *

. * * * *

* * * * *

答案:

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

  • 茄子素馅水饺家常做法(茄子馅素水饺)
  • 2024-07-01茄子馅素水饺食材:圆茄子1个,青椒1个,鸡蛋2个,香菜5角钱,葱姜蒜①7两面和面②茄子切丁放点盐腌一下挤干水分备用③青椒切丁④炒鸡蛋碎(放点盐料酒)⑤香菜切碎,将所有食材放一起,放入五香粉,白胡椒粉,鸡粉,蚝油,。
  • 内存时序怎么还原(电脑内存时序基础知识)
  • 2024-07-01电脑内存时序基础知识有不少电脑装机用户在选择电脑内存的时候都只关注内存品牌、内存容量以及内存频率而已对于内存时序却关注的很少,其实内存时序也是内存的参数之一,但是对于内存时序的意思是什么?内存时序高低的好坏,估计没有几个。
  • 草鱼豆腐煲正宗做法(在家来一份豆腐草鱼煲)
  • 2024-07-01在家来一份豆腐草鱼煲“大家好,这里是壹号美食公馆,专注分享简单的家常菜和儿童美味辅食,用美食拓宽生活中的幸福之味砂锅煲在我们的日常生活中应该是再常见不过的美食了虽然对于很多家庭来说,用砂锅炖煮美食的次数并不是太多而且对于。
  • 学会简单的逻辑学(简单的逻辑学以后)
  • 2024-07-01简单的逻辑学以后(原创)文/数海潮1.电视剧《欢乐颂》里的安迪,发现有问题时爱说:这不合逻辑逻辑是什么?是课本上那些头疼的术语:逻辑非、逻辑与……想起就让人望而却步那些枯燥的内容,如今早已经还给老师不合逻辑的表现司空。
  • 锂离子电池正极研究现状(锂离子电池正极材料)
  • 2024-07-01锂离子电池正极材料锂离子电池正极材料在锂电池中的总成本占据40%以上的比例,并且其性能直接影响了锂电池的各项性能指标,所以征集材料在锂电池中占据核心地位,征集材料的性能和价格是制约锂电池进一步向高能量长寿面和低成本发展。
  • 高分电影推荐华尔街之狼(堪称韩版华尔街之狼)
  • 2024-07-01堪称韩版华尔街之狼何以解忧,唯有暴富这句话很多人都有深刻的感受,也是本部电影男主的逆袭过程韩国电影《钱》刚上映就票房火爆,登上韩国电影的票房榜首,这是一部以金钱为线索的金融类犯罪电影,演员阵容强大,由柳俊烈、刘智泰、赵。
  • 四大队集中整治非机动车(北京将开展非机动车执法检查)
  • 2024-07-01北京将开展非机动车执法检查3月10日,北京市人大常委会启动《北京市非机动车管理条例》实施情况执法检查据悉,这是本届人大常委会任期内第二次对该条例开展执法检查执法检查围绕电动自行车销售落实目录管理、推行带牌销售等六方面的重点展开。
  • 氨气的电子式谢谢
  • 2024-07-01氨气的电子式谢谢氨气是氮氢化合物,它是一种具有较强的刺激性气味的气体物质,它的化学式是NH3,即每个氨气分子中含有一个氮原子和三个氢原子,而氮原子中最外电子层的5个电子中有三个电子分别与每个氢原子上一个电子形成各自的。
  • 苹果手机充值游戏怎么退款(具体退款方法)
  • 2024-07-01具体退款方法首先需要一台电脑,需要在电脑上安装iTunes点击账户,登录你的AppleID然后在账户一栏找到查看我的账户进去之后找到购买历史记录,然后显示全部你就能够看见该ID所有的购买项目,包括免费和付费的,退。
  • 写给天堂的爸妈的一封家书(给天堂妈妈的一封家书)
  • 2024-07-01给天堂妈妈的一封家书妈妈,我是你女儿星星,最近流行写家书,我也趁机给您写了一封,您在我三岁的时候就不在了,整整走了27年了,您在哪边还好吗?从小奶奶就和我说:没了你,你妈妈就不会死的,就是你命硬才害你妈妈的就这样女儿背了。
  • ai控制动物大脑(大脑神经元堪比狒狒)
  • 2024-07-01大脑神经元堪比狒狒《比较神经学期刊》(JournalofComparativeNeurology)最新研究显示,暴龙大脑含足够神经元,有堪比狒狒的智慧能解决问题甚至构建群体文化《华盛顿邮报》报道,长久以来人们对暴龙的印。