《c++程序设计》谭浩强课后习题与答案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

.Word范文第一章1.5题#includeiostreamusingnamespacestd;intmain(){coutThisis;coutaC++;coutprogram.;return0;1.6题#includeiostreamusingnamespacestd;intmain(){inta,b,c;a=10;b=23;c=a+b;couta+b=;coutc;coutendl;return0;}1.7七题#includeiostreamusingnamespacestd;intmain(){inta,b,c;intf(intx,inty,intz);cinabc;c=f(a,b,c);coutcendl;return0;}intf(intx,inty,intz){intm;if(xy)m=x;elsem=y;if(zm)m=z;return(m);.Word范文}1.8题#includeiostreamusingnamespacestd;intmain(){inta,b,c;cinab;c=a+b;couta+b=a+bendl;return0;}1.9题#includeiostreamusingnamespacestd;intmain(){inta,b,c;intadd(intx,inty);cinab;c=add(a,b);couta+b=cendl;return0;}intadd(intx,inty){intz;z=x+y;return(z);}2.3题#includeiostreamusingnamespacestd;intmain(){charc1='a',c2='b',c3='c',c4='\101',c5='\116';coutc1c2c3'\n';cout\t\bc4'\t'c5'\n';return0;}2.4题#includeiostreamusingnamespacestd;intmain(){charc1='C',c2='+',c3='+';.Word范文coutIsay:\c1c2c3'\';cout\t\tHesays:\C++isveryinteresting!\'\n';return0;}2.7题#includeiostreamusingnamespacestd;intmain(){inti,j,m,n;i=8;j=10;m=++i+j++;n=(++i)+(++j)+m;couti'\t'j'\t'm'\t'nendl;return0;}2.8题#includeiostreamusingnamespacestd;intmain(){charc1='C',c2='h',c3='i',c4='n',c5='a';c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;coutpasswordis:c1c2c3c4c5endl;return0;}3.2题#includeiostream#includeiomanipusingnamespacestd;intmain(){floath,r,l,s,sq,vq,vz;constfloatpi=3.1415926;coutpleaseenterr,h:;cinrh;l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;vq=3.0/4.0*pi*r*r*r;vz=pi*r*r*h;coutsetiosflags(ios::fixed)setiosflags(ios::right).Word范文setprecision(2);coutl=setw(10)lendl;couts=setw(10)sendl;coutsq=setw(10)sqendl;coutvq=setw(10)vqendl;coutvz=setw(10)vzendl;return0;}3.3题#includeiostreamusingnamespacestd;intmain(){floatc,f;cout请输入一个华氏温度:;cinf;c=(5.0/9.0)*(f-32);//注意5和9要用实型表示,否则5/9值为0cout摄氏温度为:cendl;return0;};3.4题#includeiostreamusingnamespacestd;intmain(){charc1,c2;cout请输入两个字符c1,c2:;c1=getchar();//将输入的第一个字符赋给c1c2=getchar();//将输入的第二个字符赋给c2cout用putchar函数输出结果为:;putchar(c1);putchar(c2);coutendl;cout用cout语句输出结果为:;coutc1c2endl;return0;}3.4题另一解#includeiostreamusingnamespacestd;intmain(){charc1,c2;cout请输入两个字符c1,c2:;c1=getchar();//将输入的第一个字符赋给c1c2=getchar();//将输入的第二个字符赋给c2.Word范文cout用putchar函数输出结果为:;putchar(c1);putchar(44);putchar(c2);coutendl;cout用cout语句输出结果为:;coutc1,c2endl;return0;}3.5题#includeiostreamusingnamespacestd;intmain(){charc1,c2;inti1,i2;//定义为整型cout请输入两个整数i1,i2:;cini1i2;c1=i1;c2=i2;cout按字符输出结果为:c1,c2endl;return0;}3.8题#includeiostreamusingnamespacestd;intmain(){inta=3,b=4,c=5,x,y;cout(a+bc&&b==c)endl;cout(a||b+c&&b-c)endl;cout(!(ab)&&!c||1)endl;cout(!(x=a)&&(y=b)&&0)endl;cout(!(a+b)+c-1&&b+c/2)endl;return0;}3.9题includeiostreamusingnamespacestd;intmain(){inta,b,c;coutpleaseenterthreeintegernumbers:;cinabc;if(ab)if(bc)coutmax=c;.Word范文elsecoutmax=b;elseif(ac)coutmax=c;elsecoutmax=a;coutendl;return0;}3.9题另一解#includeiostreamusingnamespacestd;intmain(){inta,b,c,temp,max;coutpleaseenterthreeintegernumbers:;cinabc;temp=(ab)?a:b;/*将a和b中的大者存入temp中*/max=(tempc)?temp:c;/*将a和b中的大者与c比较,最大者存入max*/coutmax=maxendl;return0;}3.10题#includeiostreamusingnamespacestd;intmain(){intx,y;coutenterx:;cinx;if(x1){y=x;coutx=x,y=x=y;}elseif(x10)//1≤x<10{y=2*x-1;coutx=x,y=2*x-1=y;}else//x≥10{y=3*x-11;coutx=x,y=3*x-11=y;}coutendl;.Word范文return0;}3.11题#includeiostreamusingnamespacestd;intmain(){floatscore;chargrade;coutpleaseenterscoreofstudent:;cinscore;while(score100||score0){coutdataerror,enterdataagain.;cinscore;}switch(int(score/10)){case10:case9:grade='A';break;case8:grade='B';break;case7:grade='C';break;case6:grade='D';break;default:grade='E';}coutscoreisscore,gradeisgradeendl;return0;}3.12题#includeiostreamusingnamespacestd;intmain(){longintnum;intindiv,ten,hundred,thousand,ten_thousand,place;/*分别代表个位,十位,百位,千位,万位和位数*/coutenteraninteger(0~99999):;cinnum;if(num9999)place=5;elseif(num999)place=4;elseif(num99)place=3;elseif(num9)place=2;elseplace=1;.Word范文coutplace=placeendl;//计算各位数字ten_thousand=num/10000;thousand=(int)(num-ten_thousand*10000)/1000;hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);coutoriginalorder:;switch(place){case5:coutten_thousand,thousand,hundred,ten,indivendl;coutreverseorder:;coutindivtenhundredthousandten_thousandendl;break;case4:coutthousand,hundred,ten,indivendl;coutreverseorder:;coutindivtenhundredthousandendl;break;case3:couthundred,ten,indivendl;coutreverseorder:;coutindivtenhundredendl;break;case2:coutten,indivendl;coutreverseorder:;coutindivtenendl;break;case1:coutindive

1 / 131
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功