您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > C++程序设计教程(修订版)——设计思想与实现习题解答钱能
二2.1#includeiostream.hvoidmain(){//本题原考虑在16位机器上实验目前多为32位机器故已过时。inta=42486;coutoctaendlhexaendl;unsignedb=42486;coutdec(signed)bendl;}2.2#includeiostream.h#includeiomanip.hconstdoublepi=3.1415926;voidmain(){doubleradius1,radius2;coutpleaseinputtwonumbers:\n;cinradius1radius2;coutsetw(10)pisetw(10)radius1setw(10)(pi*radius1*radius1)endlsetw(10)pisetw(10)radius2setw(10)(pi*radius2*radius2)endl;}2.3#includeiostream.h#includeiomanip.hconstdoublee=2.718281828;voidmain(){coutsetprecision(10)eendlsetiosflags(ios::fixed)setprecision(8)eendlsetiosflags(ios::scientific)eendl;}2.4#includeiostream.hvoidmain(){cout\Howmanystudentshere?\\n\500\\n;}2.5#includeiostream.hvoidmain(){coutsizeofcharsizeof(char)byte\nsizeofunsignedcharsizeof(unsignedchar)byte\nsizeofsignedcharsizeof(signedchar)byte\nsizeofintsizeof(int)byte\nsizeofunsignedsizeof(unsigned)byte\nsizeofsignedsizeof(signed)byte\nsizeofshortsizeof(short)byte\nsizeofunsignedshortsizeof(unsignedshort)byte\nsizeoflongsizeof(long)byte\nsizeofsignedlongsizeof(signedlong)byte\nsizeofunsignedlongsizeof(unsignedlong)byte\nsizeoffloatsizeof(float)byte\nsizeofdoublesizeof(double)byte\nsizeoflongdoublesizeof(longdouble)byte\n;}2.61)pleaseinput3sidesofonetriangle:6,6,8a=6.00,b=6.00,c=8.00areaoftriangleis17.888542)该程序计算三角形的面积前后分为三部分输入处理输出。3)//#includestdio.h#includeiostream.h#includeiomanip.h#includemath.hvoidmain(){floata,b,c,s,area;//printf(pleaseinput3sidesofonetriangle:\n);coutpleaseinput3sidesofonetriangle:\n;//scanf(%f,%f,%f,&a,&b,&c);//输入时以逗号作为数据间隔cinabc;//输入时以空格作为数据间隔s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c));//printf(a=%7.2f,b=%7.2f,c=%7.2f\n,a,b,c);coutsetiosflags(ios::fixed)setprecision(2)a=setw(7)a,b=setw(7)b,c=setw(7)cendl;//printf(areaoftriangleis%10.5f,area);coutareaoftriangleissetw(10)setprecision(5)areaendl;}4)#includeiostream.h#includeiomanip.h#includemath.hfloatarea(floata,floatb,floatc);//函数声明voidmain(){floata,b,c;coutpleaseinput3sidesofonetriangle:\n;cinabc;//输入时以空格作为数据间隔floatresult=area(a,b,c);//函数调用coutsetiosflags(ios::fixed)setprecision(2)a=setw(7)a,b=setw(7)b,c=setw(7)cendl;coutareaoftriangleissetw(10)setprecision(5)resultendl;}floatarea(floata,floatb,floatc)//函数定义{floats=(a+b+c)/2;returnsqrt(s*(s-a)*(s-b)*(s-c));}2.7Inmain():Entertwonumbers:38Callingadd():Inadd(),received3and8andreturn11Backinmain():cwassetto11Exiting...2.8#includeiostream.h#includemath.hdoubleCylinder(doubler,doubleh);voidmain(){doubleradius,height;cout请输入圆柱体的半径和高:\n;cinradiusheight;doublevolume=Cylinder(radius,height);cout该圆柱体的体积为volumeendl;}doubleCylinder(doubler,doubleh){returnr*r*M_PI*h;}三3.1(1)sqrt(pow(sin(x),2.5))(2)(a*x+(a+x)/(4*a))/2(3)pow(c,x*x)/sqrt(2*M_PI)//M_PI为BC中math.h中的圆周率常数3.213.72.593.3(1)a1=1a2=1(2)1.1(3)2,0.0(4)203.4#includeiostream.hvoidmain(){intx;coutpleaseinputx:\n;cinx;if(x=-1)cout(x-1)endl;if(x-1&&x=2)cout2*xendl;if(2x&&x=10)coutx*(x+2);}3.5#includeiostream.hvoidmain(){inta;coutpleaseinputanumber:\n;cina;intc1=a%3==0;intc2=a%5==0;intc3=a%7==0;switch((c12)+(c21)+c3){case0:cout不能被3,5,7整除.\n;break;case1:cout只能被7整除.\n;break;case2:cout只能被5整除.\n;break;case3:cout可以被5,7整除.\n;break;case4:cout只能被3整除.\n;break;case5:cout可以被3,7整除.\n;break;case6:cout可以被3,5整除.\n;break;case7:cout可以被3,5,7整除.\n;break;}}3.6#includeiostream.hvoidmain(){intgrade;coutpleaseinputanumber:\n;cingrade;if(grade100||grade0)cout错误的成绩.\n;elseif(grade=90)coutA.\n;elseif(grade=80)coutB.\n;elseif(grade=70)coutC.\n;elseif(grade=60)coutD.\n;elsecoutE.\n;}四4.11#includeiostream.h#includemath.hvoidmain(){doublesum=1,t=-1,x;inti=1;coutpleaseinputavalue:\n;cinx;do{t*=(-1)*x/i;sum+=t;i++;}while(fabs(t)1e-8);coutsum=sumendl;}2#includeiostream.h#includemath.hvoidmain(){doublesum=1,t=-1,x;coutpleaseinputavalue:\n;cinx;inti=1;while(fabs(t)1e-8){t*=(-1)*x/i;sum+=t;i++;}coutsum=sumendl;}3#includeiostream.h#includemath.hvoidmain(){doublesum=1,t=-1,x;coutpleaseinputavalue:\n;cinx;for(inti=1;fabs(t)1e-8;i++){t*=(-1)*x/i;sum+=t;}coutsum=sumendl;}4.2#includeiostream.hvoidmain(){longsum=0,t=1;for(inti=1;i=15;i++){t*=i;sum+=t;}coutsum=sumendl;}4.3#includeiostream.hvoidmain(){for(inti=1;i=9;i++)for(intj=0;j=9;j++)for(intk=0;k=9;k++)if(i*i*i+j*j*j+k*k*k==100*i+10*j+k)cout(100*i+10*j+k)是水仙花数.\n;}4.4#includeiostream.hvoidmain(){for(inti=1;i1000;i++){intsum=0;for(intj=1;j=i/2;j++)if(i%j==0)sum+=j;if(sum==i)couti是完数.\n;}}4.5#includeiostream.hvoidmain(){floats=100,h=100;for(inti=1;i10;i++){s+=h;h/=2;}cout共经过s米第10次反弹h米高.\n;}4.6#includeiostream.hvoidmain(){intpeachs=1;for(inti=1;i10;i++)peachs=(peachs+1)*2;cout第一天共摘下peachs个桃子.\n;}4.7#includeiostream.h#includemath.hvoidmain(){doublex,a;coutpl
本文标题:C++程序设计教程(修订版)——设计思想与实现习题解答钱能
链接地址:https://www.777doc.com/doc-2309905 .html