C++Primer Plus(第6版)中文版编程练习答案--第五章.pdf
1注释:这些程序是唐刀在学习的时候编写的,其中可能存在错误和不足,大家注释:这些程序是唐刀在学习的时候编写的,其中可能存在错误和不足,大家做个参考就好。做个参考就好。1.#includeint main()using namespace std;int num_big;int num_small;coutplease input the small number:num_small;coutplease input the big number:num_big;int total=0;for(int i=num_small;i=num_big;i+)total+=i;couttotal is totalendl;return 0;2.#include#includeusing namespace std;intmain()arrayarr;arr1=arr0=1LD;for(int i=2;i=100;i+)arri=i*arri-1;for(int i=2;i=100;i+)couti!=arriendl;return 0;23.#includeusing namespace std;int main()double num=0;double total=0;coutinput the number:num;while(num!=0)total+=num;coutinput the number again:num;coutgame over!endl;coutthe total of these number is totalendl;return 0;4.#includeusing namespace std;int main()double d_value=0;double c_value=0;int year;d_value=100+100*0.1;c_value=100+100*0.05;for(int i=2;d_value=c_value;i+)d_value=d_value+100*0.1;c_value=c_value*1.05;year=i;coutafter year years,Cleos money will much than Daphne!endl;return 0;35.#include#includeusing namespace std;int main()/*char*month12=January,February,March,April,May,June,July,August,September,October,November,December;int num12;for(int i=0;i12;i+)coutplease input the number of books in monthi:numi;int total=0;for(int i=0;i12;i+)total+=numi;coutthe total of books is totalendl;*/string month12=January,February,March,April,May,June,July,August,September,October,November,December;int num12;for(int i=0;i12;i+)coutplease input the number of books in monthi:numi;int total=0;for(int i=0;i12;i+)total+=numi;4coutthe total of books is totalendl;return 0;6.#include#includeusing namespace std;int main()string month12=January,February,March,April,May,June,July,August,September,October,November,December;int num312;for(int j=0;j3;j+)coutyear j+1endl;for(int i=0;i12;i+)coutplease input the number of books in monthi:numji;int total=0;for(int j=0;j3;j+)for(int i=0;i12;i+)total+=numji;coutthe total of books is totalendl;return 0;7.#include#include5#includeusing namespace std;int main()struct carstring name;int year;int num;couthow many cars do you want to catalog?num;car*cars=new carnum;for(int i=0;inum;i+)coutCar#i+1:endl;coutplease enter the make:;cin.sync();getline(cin,carsi.name);coutcarsi.year;couthere is your cellection:endl;for(int i=0;inum;i+)coutcarsi.year carsi.nameendl;return 0;8.#include#includeusing namespace std;int main()char word20;int i=0;6coutenter words(to stop,type the word done):word;while(strcmp(word,done)!=0)i+;cinword;coutyou entered a total of i words.endl;return 0;9.#include#include#includeusing namespace std;int main()string word;int i=0;coutenter words(to stop,type the word done):word;while(word!=done)i+;cinword;coutyou entered a total of i words.endl;return 0;10.#includeusing namespace std;void show(int n)for(int i=1;i=n;i+)for(int j=0;jn-i;j+)cout.;7for(int k=0;ki;k+)cout*;coutendl;int main()coutplease enter number of rows:num;show(num);return 0;