c++-primer-plus(第六版)第二至第六章课后编程练习全部答案.docx
《c++-primer-plus(第六版)第二至第六章课后编程练习全部答案.docx》由会员分享,可在线阅读,更多相关《c++-primer-plus(第六版)第二至第六章课后编程练习全部答案.docx(26页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、其次章:起先学习C+ /ex2.1-display your name and address #includeint main(void)(using namespace std;coutMy name is liao chunguang and I live in hunan chenzhou.n; )/ex2.2-convert the furlong units to yard uints-把浪单位换位码单位 #includedouble fur2yd(double);int main()(using namespace std;coutenter the distance measu
2、red by furlong units:;double fur;cinfur;coutconvert the furlong to yardendl;double yd;yd=fur2yd(fur);coutfur furlong is yd yardendl;return 0;)double fur2yd(double t)(return 220*t;)ex2.3-每个函数都被调用两次#includevoid mice();void see();using namespace std;int main()(mice();mice();see();see();return 0;getline
3、(cin,fname);note:将一行输入读取到 string 类对象中运用的是 getline(cin,str) 它没有运用句点表示法,所以不是类方法coutEnter your last name:;getline(cinjname);attach=H,fullname=lname+attach+fname;coutHeres the information in a single string:,fullnameendl;return 0;)/ex4.5 declare a struct and initialize it 声明结果并创建一个变量#includeconst int As
4、ize=20;struct CandyBar(char brandAsize;double weight;int calory;);int main()(using namespace std;CandyBar snack=Mocha Munch,2.3,350;coutHHeres the information of snack:n;coutbrand:snack.brandendl;coutweight:snack.weightendl;coutcalory:snack.caloryendl;return 0;)/ex4.6结构数组的声明及初始化#includeconst int Asi
5、ze=20;struct CandyBar(char brandfAsize;double weight;int calory;);int main()using namespace std;CandyBar snack3=Mocha Munch”,2.3,350,XuFuJin,1.1,300,Alps;O.lOO;for(int i=0;i3;i+)利用for循环来显示snack变量的内容 (coutsnacki.brandendl snacki.weightendl snacki.caloryendlendl;return 0;)/ex4.7 pizza 披萨饼#include#incl
6、udeconst int Size=20;struct pizza声明结构 (char companySize;double diameter;double weight;);int main() (using namespace std;pizza pie;创建一个名为pie的结构变量 coutWhats the name of pizza company:; cin.getline(pie pany,Size);coutWhats the diameter of pizza:; cinpie.diameter;coutWhats the weight of pizza:;cinpie.we
7、ight;coutcompany:pie panyendl;coutdiameter:,pie.diameterinches,endl; coutweight:pie.weightounchesendl;return 0; )/ex4.8 pizza pie披萨饼 运用new创建动态结构 #include#includeconst int Size=20;struct pizza声明结构 (char companySize;double diameter;double weight;);int main()(using namespace std;pizza *pie=new pizza;运用
8、new创建动态结构 coutWhats the diameter of pizza:;cinpie-diameter;cin.get();读取下一个字符coutWhats the name of pizza company:;cin.get(pie-company,Size);coutWhats the weight of pizza:;cinpie-weight;cout,diameter:,pie-diameter inchesendl; coutcompany:pie-companyendl;coutweight:pie-weight ounchesendl; delete pie;/d
9、elete 释放内存return 0;)运用new动态支配数组一方法1#include#includeusing namespace std;struct CandyBar (string brand;double weight;int calory;);int main()CandyBar *snack= new CandyBar3;snack0.brand=A;单个初始化由new动态支配的内存snackO.weight=l.l;snack0.calory=200;snackl.brand=B;snackl.weight=2.2;snackl.calory=400;snack2.brand=
10、C;snack2.weight=4.4;snack2.calory=500;for(int i=0;i3;i+)(cout ” brand: snacki.brand endl;cout weight: snacki.weight endl;cout ” calorie: snacki.calory endlendl;delete snack;return 0;)数组一方法1#include int main()(using namespace std;const int Size = 3;int successSize;coutEnter your success of the three
11、times 40 meters running:n;cin success0successlsuccess2;coutsuccessl:success0endl;coutsuccess2:,successlendl;coutsuccess3:,success2endl;double average=(success0+successl+success2)/3;coutaverage:averageendl;return 0;)/ex.4.10 array一方法 2#include #include int main()(using namespace std;arrayad=0;coutEnt
12、er your success of the three times 40 meters running:n;cin ad0adlad2;coutsuccessl:ad0endl;coutsuccess2:adlendl;coutsuccess3:ad2endl; ad3=(ad0+adl+ad2);coutaverage:ad3endl;return 0;)第五章循环和关系表达式ttinclude int main()(using namespace std;coutnumlnum2;int sum=0;for(int temp=numl;temp=num2;+temp)/or temp+
13、sum十=temp;cout,zThe sum fromto /,num2z/ is ,zsumendl;return 0;#include #includeint main()(using namespace std;arrayad=0;adl=ad0=lL;for(int i=2;i101;i+)adi=i*adi-l;for (int i=0;i101;i+)coutiz,! = z,adiendl;/ex.5.3ttinclude int main()(using namespace std;coutz/Please enter an integer: ;int sum=O, num;
14、while (cinnum) &num!=0)(sum+=num;coutzzSo far, the sum is z/sumendl;coutz/Please enter an integer: ;)return 0;)#include int main()(using namespace std;double suml,sum2;suml=sum2=0.0;int year=O;while(sum2=suml)(+year;suml+=10;sum2=(100+sum2)*0.05+sum2;cout“经过year”年后,Cleo的投资价值才能超过Daphne的投资价值。“endl;cou
15、t”此时,Cleo的投资价值为而Daphne的投资价值为sum2endl;return 0;#include const int MONTHS = 12;constchar*monthsMONTHS=“January, February, March, April”, May, June, July, August, Sept ember,October, November, December; int main() ( using namespace std; int salesMONTHS, sum=0;for(int i=0;iMONTHS;i+) cout”请输入在monthsi的C+
16、 For Fools的销售量:; cinsalesi; sum+=sales i;)cout这一年中的C+ For Fools的总销售量为:sumendl; return 0;)ttinclude const int MONTHS = 12;constchar*monthsMONTHS=“January, February, March, April”, May,June,July, August, Sept ember, October, November, December;const char* years 3 = 第一年,其次年,第三年;int main() (using namesp
17、ace std;int year_sale3, sum=0, sales3MONTHS;for(int i=0;i3;i+) int temp=0;coutyearsi的每个月销售量:,endl;for (int j=0;jMONTHS;j+) (cout请输入months j salesi j;temp+=salesi j;year salei=temp;sum+=year_salei;)for(int i=0;i3;i+)coutyears i 的销售量为:year_sale i endl;cout*这三年的总销售量为:sumendl;return 0;#include ttinclude
18、 using namespace std; struct carstring name; int year;);int main() (coutnum). get ();car* ps=new carnum;for(int i=0;inum;+i) coutzzCar + : n ; coutz,Please enter the make: ; getline(cin, psi. name);coutps i. year). get ();)cout,zHere is your collection: nz,;for(int i=0;inum;+i)coutps i. year ,psi. n
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- primer plus 第六 第二 课后 编程 练习 全部 答案
限制150内