C面向对象程序设计课后习题答案114章.docx
- 文档编号:9956713
- 上传时间:2023-02-07
- 格式:DOCX
- 页数:193
- 大小:50.20KB
C面向对象程序设计课后习题答案114章.docx
《C面向对象程序设计课后习题答案114章.docx》由会员分享,可在线阅读,更多相关《C面向对象程序设计课后习题答案114章.docx(193页珍藏版)》请在冰豆网上搜索。
C面向对象程序设计课后习题答案114章
第一章
1-5、#include
usingnamespacestd;
intmain()
{
cout<<"This"<<"is";
cout<<"a"<<"C++";
cout<<"program.";
return0;
}
1-6、#include
usingnamespacestd;
intmain()
{
inta,b,c;
a=10;
b=23;
c=a+b;
cout<<"a+b=";
cout< cout< return0; } 1-7#include usingnamespacestd; intmain() { inta,b,c; intf(intx,inty,intz); cin>>a>>b>>c; c=f(a,b,c); cout< return0; } intf(intx,inty,intz) { intm; if(x elsem=y; if(z return(m); } 1-8、#include usingnamespacestd; intmain() { inta,b,c; cin>>a>>b; c=a+b; cout<<"a+b="< return0; } 1-9、#include usingnamespacestd; intmain() { inta,b,c; intadd(intx,inty); cin>>a>>b; c=add(a,b); cout<<"a+b="< return0; } intadd(intx,inty) {intz; z=x+y; return(z); } 第二章 2-3、#include usingnamespacestd; intmain() {charc1='a',c2='b',c3='c',c4='\101',c5='\116'; cout< cout<<"\t\b"< return0; } 2-4、#include usingnamespacestd; intmain() {charc1='C',c2='+',c3='+'; cout<<"Isay: \""< cout<<"\t\t"<<"Hesays: \"C++isveryinteresting! \""<<'\n'; return0; } 2-7、#include usingnamespacestd; intmain() {inti,j,m,n; i=8; j=10; m=++i+j++; n=(++i)+(++j)+m; cout< return0; } 2-8、#include usingnamespacestd; intmain() {charc1='C',c2='h',c3='i',c4='n',c5='a'; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; cout<<"passwordis: "< return0; } 第三章 3-2、#include #include usingnamespacestd; intmain() {floath,r,l,s,sq,vq,vz; constfloatpi=3.1415926; cout<<"pleaseenterr,h: "; cin>>r>>h; 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; cout< : fixed)< : right) < (2); cout<<"l="< cout<<"s="< cout<<"sq="< cout<<"vq="< cout<<"vz="< return0; } 3-3、#include usingnamespacestd; intmain() {floatc,f; cout<<"请输入一个华氏温度: "; cin>>f; c=(5.0/9.0)*(f-32);//注意5和9要用实型表示,否则5/9值为0 cout<<"摄氏温度为: "< return0; }; 3-4、#include usingnamespacestd; intmain() {charc1,c2; cout<<"请输入两个字符c1,c2: "; c1=getchar();//将输入的第一个字符赋给c1 c2=getchar();//将输入的第二个字符赋给c2 cout<<"用putchar函数输出结果为: "; putchar(c1); putchar(c2); cout< cout<<"用cout语句输出结果为: "; cout< return0; } 3-4-1、#include usingnamespacestd; intmain() {charc1,c2; cout<<"请输入两个字符c1,c2: "; c1=getchar();//将输入的第一个字符赋给c1 c2=getchar();//将输入的第二个字符赋给c2 cout<<"用putchar函数输出结果为: "; putchar(c1); putchar(44); putchar(c2); cout< cout<<"用cout语句输出结果为: "; cout< return0; } 3-5、#include usingnamespacestd; intmain() {charc1,c2; inti1,i2;//定义为整型 cout<<"请输入两个整数i1,i2: "; cin>>i1>>i2; c1=i1; c2=i2; cout<<"按字符输出结果为: "< return0; } 3-8、#include usingnamespacestd; intmain() {inta=3,b=4,c=5,x,y; cout<<(a+b>c&&b==c)< cout<<(a||b+c&&b-c)< cout<<(! (a>b)&&! c||1)< cout<<(! (x=a)&&(y=b)&&0)< cout<<(! (a+b)+c-1&&b+c/2)< return0; } 3-9-1、#include usingnamespacestd; intmain() {inta,b,c; cout<<"pleaseenterthreeintegernumbers: "; cin>>a>>b>>c; if(a if(b cout<<"max="< else cout<<"max="< elseif(a cout<<"max="< else cout<<"max="< cout< return0; } 3-9-2、#include usingnamespacestd; intmain() {inta,b,c,temp,max; cout<<"pleaseenterthreeintegernumbers: "; cin>>a>>b>>c; temp=(a>b)? a: b;/*将a和b中的大者存入temp中*/ max=(temp>c)? temp: c;/*将a和b中的大者与c比较,最大者存入max*/ cout<<"max="< return0; } 3-10、#include usingnamespacestd; intmain() {intx,y; cout<<"enterx: "; cin>>x; if(x<1) {y=x; cout<<"x="< } elseif(x<10)//1≤x<10 {y=2*x-1; cout<<"x="< } else//x≥10 {y=3*x-11; cout<<"x="< } cout< return0; } 3-11、#include usingnamespacestd; intmain() {floatscore; chargrade; cout<<"pleaseenterscoreofstudent: "; cin>>score; while(score>100||score<0) {cout<<"dataerror,enterdataagain."; cin>>score; } switch(int(score/10)) {case10: case9: grade='A';break; case8: grade='B';break; case7: grade='C';break; case6: grade='D';break; default: grade='E'; } cout<<"scoreis"< return0; } 3-12、#include usingnamespacestd; intmain() {longintnum; intindiv,ten,hundred,thousand,ten_thousand,place; /*分别代表个位,十位,百位,千位,万位和位数*/ cout<<"enteraninteger(0~99999): "; cin>>num; if(num>9999) place=5; elseif(num>999) place=4; elseif(num>99) place=3; elseif(num>9) place=2; elseplace=1; cout<<"place="< //计算各位数字 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); cout<<"originalorder: "; switch(place) {case5: cout< cout<<"reverseorder: "; cout< break; case4: cout< cout<<"reverseorder: "; cout< break; case3: cout< cout<<"reverseorder: "; cout< break; case2: cout< cout<<"reverseorder: "; cout< break; case1: cout< cout<<"reverseorder: "; cout< break; } return0; } 3-13-1、#include usingnamespacestd; intmain() {longi;//i为利润 floatbonus,bon1,bon2,bon4,bon6,bon10; bon1=100000*0.1;//利润为10万元时的奖金 bon2=bon1+100000*0.075;//利润为20万元时的奖金 bon4=bon2+100000*0.05;//利润为40万元时的奖金 bon6=bon4+100000*0.03;//利润为60万元时的奖金 bon10=bon6+400000*0.015;//利润为100万元时的奖金 cout<<"enteri: "; cin>>i; if(i<=100000) bonus=i*0.1;//利润在10万元以内按10%提成奖金 elseif(i<=200000) bonus=bon1+(i-100000)*0.075;//利润在10万元至20万时的奖金 elseif(i<=400000) bonus=bon2+(i-200000)*0.05;//利润在20万元至40万时的奖金 elseif(i<=600000) bonus=bon4+(i-400000)*0.03;//利润在40万元至60万时的奖金 elseif(i<=1000000) bonus=bon6+(i-600000)*0.015;//利润在60万元至100万时的奖金 else bonus=bon10+(i-1000000)*0.01;//利润在100万元以上时的奖金 cout<<"bonus="< return0; } 3-13-2、#include usingnamespacestd; intmain() {longi; floatbonus,bon1,bon2,bon4,bon6,bon10; intc; bon1=100000*0.1; bon2=bon1+100000*0.075; bon4=bon2+200000*0.05; bon6=bon4+200000*0.03; bon10=bon6+400000*0.015; cout<<"enteri: "; cin>>i; c=i/100000; if(c>10)c=10; switch(c) {case0: bonus=i*0.1;break; case1: bonus=bon1+(i-100000)*0.075;break; case2: case3: bonus=bon2+(i-200000)*0.05;break; case4: case5: bonus=bon4+(i-400000)*0.03;break; case6: case7: case8: case9: bonus=bon6+(i-600000)*0.015;break; case10: bonus=bon10+(i-1000000)*0.01; } cout<<"bonus="< return0; } 3-14、#include usingnamespacestd; intmain() {intt,a,b,c,d; cout<<"enterfournumbers: "; cin>>a>>b>>c>>d;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象 程序设计 课后 习题 答案 114
![提示](https://static.bdocx.com/images/bang_tan.gif)