《c++面向对象程序设计课后习题答案及解析[谭浩强版]》由会员分享,可在线阅读,更多相关《c++面向对象程序设计课后习题答案及解析[谭浩强版](52页珍藏版)》请在金锄头文库上搜索。
1、.第一章5:#include using namespace std;int main coutThisis; coutaC+; coutprogram.endl; return 0;6:#include using namespace std;int main int a,b,c; a=10; b=23; c=a+b; couta+b=; coutc; coutendl; return 0;7:#include using namespace std;int main int a,b,c; int f; cinabc; c=f; coutcendl; return 0;int f int m
2、;if x m=x; else m=y; if z m=z; return; 8: #include using namespace std;int main int a,b,c; cinab; c=a+b; couta+b=a+bendl; return 0; 9: #include using namespace std;int mainint add; int a,b,c; cinab; c=add; couta+b=cendl; return 0;int addint c; c=x+y; return;10:#include using namespace std;int mainvo
3、id sort;int x,y,z; cinxyz; sort; return 0;void sort int temp; if y temp=x;x=y;y=temp; / 内3个语句的作用是将x和y的值互换 if z coutz,x,yendl; else if z coutx,z,yendl; else coutx,y,zendl; 11:#include using namespace std;int mainint max; int a,b,c; cinabc; coutmax=maxendl; coutmax=maxendl; return 0;int maxifa a=b; if
4、a a=c; return a;12:#include using namespace std;int main void change; int a,b; cinab; ifa change; coutmax=a min=bendl; return 0;void change int r1,r2,temp; temp=r1; r1=r2; r2=temp; 13:#include using namespace std;int mainvoid sort; int a,b,c,a1,b1,c1; coutabc; a1=a;b1=b;c1=c; sort; couta b c in sort
5、ed order is ; couta1 b1 c1endl; return 0;void sort void change;if j change; if k change; if k change;void change int temp; temp=x; x=y; y=temp;14:#include #include using namespace std;int main string s1=week,s2=end; couts1=s1endl; couts2=s2endl; s1=s1+s2; coutThe new string is:s1endl; return 0; 15:#
6、include #include using namespace std;int main string str; int i,n; char temp; coutstr; n=str.size; fori=0;itemp=stri;stri=strn-i-1;strn-i-1=temp; coutstrendl;return 0; 16:#include #include using namespace std;int main int i; string str5=BASIC,C,FORTRAN,C+,PASCAL; void sort; sort; coutthe sorted stri
7、ngs :endl; fori=0;i coutstri ; coutendl; return 0;void sortint i,j; string t; for j=0;j fori=0;i if si+1 t=si;si=si+1;si+1=t; 17: #include #include using namespace std;int main long c5=10100,-123567, 1198783,-165654, 3456; int a5=1,9,0,23,-45; float b5=2.4, 7.6, 5.5, 6.6, -2.3 ; void sort;void sort;
8、 void sort; sort; sort; sort; return 0;void sortint i,j,t; for j=0;j fori=0;i if ai+1 t=ai;ai=ai+1;ai+1=t; coutthe sorted numbers :endl; fori=0;i coutai ; coutendlendl; void sortint i,j; long t; for j=0;j fori=0;i if ai+1 t=ai;ai=ai+1;ai+1=t; coutthe sorted numbers :endl; fori=0;i coutai ; coutendle
9、ndl;void sortint i,j; float t; for j=0;j fori=0;i if ai+1 t=ai;ai=ai+1;ai+1=t; coutthe sorted numbers :endl; fori=0;i coutai ; coutendlendl;18: #include #include using namespace std;template void sort int i,j,min; T t; fori=0;imin=i; for j=i+1;j ifaj min=j; t=ai; ai=amin; amin=t; coutthe sorted numbers :endl; fori=0;i coutai ; coutendlendl;int main int a5=1,9,0,23,-45; float b5=2.4, 7.6, 5.5, 6.6, -2.3 ; long c5=10100,-123567, 1198783,-165654, 3456; sort; s