C++Primer Plus(第6版)中文版编程练习答案--第四章.pdf
1注释:这些程序是唐刀在学习的时候编写的,其中可能存在错误和不足,大家注释:这些程序是唐刀在学习的时候编写的,其中可能存在错误和不足,大家做个参考就好。做个参考就好。1.#include#include#includeusing namespace std;void main()string first_name;string last_name;coutwhats your first name?;getline(cin,first_name);coutwhats your last name?;getline(cin,last_name);char grade;coutgrade;int age;coutage;coutname:last_name,first_nameendl;coutgrade:char(grade+1)endl;coutage:ageendl;2.#include#include#includeusing namespace std;void main()string name;string dessert;2coutenter your name:endl;getline(cin,name);coutenter your favourite dessert:endl;getline(cin,dessert);coutI have some delicious dessert for you,nameendl;3.#include#include#includeusing namespace std;void main()char first_name20;char last_name20;coutenter your first name:endl;cin.getline(first_name,20);coutenter your last name:endl;cin.getline(last_name,20);couthere is the information in a single string:strcat(strcat(first_name,),last_name)endl;4.#include#include#includeusing namespace std;void main()string first_name;string last_name;coutenter your first name:endl;getline(cin,first_name);3coutenter your last name:endl;getline(cin,last_name);couthere is the information in a single string:last_name+,+first_nameendl;5.#include#include#includeusing namespace std;void main()struct CandyBarchar name20;double weight;int calary;CandyBar snack=Mocha Munch,2.3,350;coutname:snack.nameendl;coutweight:snack.weightendl;coutcalary:snack.calaryendl;6.#includeusing namespace std;void main()struct CandyBarchar name20;double weight;int calary;4CandyBar snack3=Mocha Munch,2.3,350,Bluce Lian,2.5,555,Jim Green,3.5,79;for(int i=0;i3;i+)coutThis is the i+1 personendl;coutname:snacki.nameendl;coutweight:snacki.weightendl;coutcalary:snacki.calaryendl;coutendl;7.#includeusing namespace std;void main()struct messagechar name20;double diameter;double weight;message mess;coutplease input the message:endl;coutname:endl;cin.getline(mess.name,20);coutdiameter:mess.diameter;coutweight:mess.weight;coutthe message of the record:endl;coutname:mess.nameendl;coutdiameter:mess.diameterendl;coutweight:mess.weightendl;58.#includeusing namespace std;void main()struct messagechar name20;double diameter;double weight;int num;coutnum;cin.get();message*mess=new messagenum;for(int i=0;inum;i+)coutplease input the message of the i+1 record:endl;coutname:endl;cin.getline(messi.name,20);coutdiameter:messi.diameter;coutweight:messi.weight;cin.get();for(int i=0;inum;i+)coutthe message of the i+1 record:endl;coutname:messi.nameendl;coutdiameter:messi.diameterendl;coutweight:messi.weightendl;9.#includeusing namespace std;6void main()struct CandyBarchar name20;double weight;int calary;coutnum;CandyBar*arr=new CandyBarnum;for(int i=0;inum;i+)coutinput the message:endl;coutarri.name;coutarri.weight;coutarri.calary;for(int i=0;inum;i+)coutthe message:endl;coutname:arri.nameendl;coutweight:arri.weightendl;coutcalary:arri.calaryendl;10.#includeusing namespace std;void main()coutnum;7double*time=new doublenum;for(int j=0;jnum;j+)coutj+1 intput:timej;double sum=0;for(int j=0;jnum;j+)sum+=timej;coutyou have num records.endl;coutaverage scores is sum/numendl;