c++primer plus第五章编程练习
来源:互联网 发布:pyqt设计软件 编辑:程序博客网 时间:2024/05/01 11:23
1.编写一个要求用户输入两个整数的程序,giant程序将计算并输出这两个整数之间(包括这两个整数)所有的整数的和。这里假设先输入较小的整数,例如如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44.
#include <iostream>using namespace std;int main(){ int x,y; cin>>x; cin>>y; int sum=0; for(int i=x;x<=y;x++) { sum=sum+x; } cout<<sum<<endl; return 0;}
3.编写一个要求用户输入数字的程序,每次输入后,程序都将报告到目前为止,所有输入的累积和,当用户输入0时,程序结束。
#include <iostream>int main(){ using namespace std; int sum=0; int num; cout<<"Enter the number ;enter 0to quit:\n "; cin>>num; while(num!=0) { sum +=num; cout<<sum; cin>>num; } cout<<endl; cout<<sum<<endl; return 0;}4.Daphne以10%的单利投资了100美元,每一年利润都是投资额的10%,即每年10美元。而Cleo以5%的复利投资了100美元,利息是当前存款(包括获得的利息)的5%,Cleo在第一年投资100美元的盈利是5%得到了105美元,下一年的盈利是105美元的5%,以此类推编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示两个人的投资价值。
#include <iostream>int main(){ using namespace std; double daphne=100; double cleo=100; int count=0; while(cleo<=daphne) { count++; daphne +=10; cleo *=1.05; } cout<<count<<endl; cout<<"Daphne: "<<daphne<<endl; cout<<"Cleo: "<<cleo<<endl; return 0;}
5.假设要销《c++for fool》一书,请编写一个程序,输入全年中每个月的销售量,图书数量而不是销售额
,程序通过循环,使用初始化为月份字符串的char*数组,或string对象数组,逐月进行体术并肩输入的数据出、、UN除杂䘝int数组中,然后程序计算数组中各元素的总数,并报告这一年的销售情况。
#include <iostream>int main(){ using namespace std; int sales[12]; string month[12]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "}; int sum=0; for(int i=0;i<12;i++) { cout<<"Please enter the sales of"<<month[i]<<"is: "; cin>>sales[i]; sum +=sales[i]; } for(int i=0;i<+12;i++) { cout<<"The sales of "<<month[i]<<"is: "<<sales[i]<<endl; } cout<<"The sales of a year is "<<sum<<endl; return 0;}6.完成编程练习5,但这一次使用一个二维数组来存储输入---3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。
#include <iostream>#include<string>int main(){ using namespace std; int sales[3][12]; string month[12]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "}; int sum[3]={0,0,0}; for(int i=0;i<3;i++) { for(int j=0;j<12;j++) { cout<<"Please enter the sales of "<<month[j]<<" in "<<(i+1)<<" year is: "; cin>>sales[i][j]; sum[i] +=sales[i][j]; } } for(int i=0;i<=2;i++) { cout<<"The sales of "<<(i+1)<<" year is: "<<sum[i]<<endl; } cout<<"The sales of three year is "<<(sum[0]+sum[1]+sum[2])<<endl; return 0;}7.设计一个名为car的结构,用它存储下述有关汽车的信息: 生产商(存储在字符数组或string对象中的字符串)、 生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。 接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取 数值和字符串(参见第4章)。最后,程序将显示每个结构的内容。
#include <iostream>#include<string>struct car{ char producer[20];//生产商 int pro_year;//生产年份};int main(){ using namespace std; cout<<"How many cars do you wish to catalog?"; int count; (cin>>count).get(); car *ca=new car[count]; for(int i=1;i<=count;i++) { cout<<"car #"<<i<<":"<<endl; cout<<"Please enter the make :"; cin.getline(ca[i].producer,20);// cin.getline(ca[i].producer,20); cout<<"Please enter the year made: "; (cin>>ca[i].pro_year).get(); } cout<<"Here is your collection:"<<endl; for(int i=1;i<=count;i++) { cout<<ca[i].pro_year<<" "<<ca[i].producer<<endl; } delete []ca; 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>#int main(){ using namespace std; int count=0; char word[20]; cout<<"Enter worsds(to stop,type the word done):"; cin>>word; while(strcmp(word,"done")) { count++; cin>>word; } cout<<"You entered a total of "<<count<<"words"<<endl; return 0;}9.编写一个满足前一个练习中描述的程序,但是用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。
#include <iostream>#include<string>int main(){ using namespace std; int count=0; string word; string c = "done"; cout<<"Enter worsds(to stop,type the word done):"; cin>>word; while(word!=c) { count++; cin>>word; } cout<<"You entered a total of "<<count<<" words"<<endl; return 0;}
10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行,然后程序将显是相应的函数的星号,其中第一行包括一个星号,第二行包括两个星号,以此类推,每一行包含的字符数等于用户指定的函数,在星号不够的情况下,在星号前面加上句点。
Enter number of rows: 5
....*
...**
..***
.****
*****
#include <iostream>#include<string>int main(){ using namespace std; int count; cout<<"Enter number of row: "; cin>>count; for(int i=1;i<=count;i++) { for(int j=1;j<= (count-i);j++) { cout<<" . "; } for(int j=1;j<=i;j++) cout<<" * "; cout<<endl; } return 0;}
0 0
- c++primer plus第五章编程练习
- C primer plus(第五版)编程练习第五章
- C++.Primer.Plus第五版第五章编程练习答案
- C Primer Plus (第五版) 第五章 编程练习 答案
- 《C Primer Plus 第五版》第十章编程练习
- 《c primer plus》编程练习回顾-第五章
- C primer plus(第五版)编程练习第二章
- C primer plus(第五版)编程练习第三章
- C primer plus(第五版)编程练习第四章
- C primer plus(第五版)编程练习第六章
- C primer plus(第五版)编程练习第七章
- C primer plus(第五版)编程练习第八章
- C primer plus(第五版)编程练习第九章
- C primer plus(第五版)编程练习第十章
- C primer plus(第五版)编程练习第十一章
- C primer plus(第五版)编程练习第十二章
- C primer plus(第五版)编程练习第十三章
- C primer plus(第五版)编程练习第十四章
- JSON.stringify()之用----就是用来把对象转化成字符串的以便alert弹出来
- ARM 汇编指令学习:[0]编码格式与条件码域
- hibernate自学篇--SSH框架的概念及hibernate框架的简析
- sklearn 安装
- Java.lang.OutOfMemoryError: PermGen space
- c++primer plus第五章编程练习
- androidstudio添加github上的第三方库报错解决Error:Cannot change dependencies of configuration ':app:_debugAnnotat
- linux基础知识(一)
- Nginx与Apache的Rewrite规则的区别
- Long类型比较大小
- 1.ArrayList和Vector
- 【matlab】物联网专题——Internet of Things : 物联网和你只有一个MATLAB的距离!
- 怎样设置无线路由器
- 【代码】统计网站的访问次数