c--面向对象程序设计课后习题答案(谭浩强版)

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

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

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

资源描述

第一章5:#includeiostreamusingnamespacestd;intmain(){coutThisis;coutaC++;coutprogram.endl;return0;}6:#includeiostreamusingnamespacestd;intmain(){inta,b,c;a=10;b=23;c=a+b;couta+b=;coutc;coutendl;return0;}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);}8:#includeiostreamusingnamespacestd;intmain(){inta,b,c;cinab;c=a+b;couta+b=a+bendl;return0;}9:#includeiostreamusingnamespacestd;intmain(){intadd(intx,inty);inta,b,c;cinab;c=add(a,b);couta+b=cendl;return0;}intadd(intx,inty){intc;c=x+y;return(c);}10:#includeiostreamusingnamespacestd;intmain(){voidsort(intx,inty,intz);intx,y,z;cinxyz;sort(x,y,z);return0;}voidsort(intx,inty,intz){inttemp;if(xy){temp=x;x=y;y=temp;}//{}内3个语句的作用是将x和y的值互换)if(zx)coutz','x','yendl;elseif(zy)coutx','z','yendl;elsecoutx','y','zendl;}11:#includeiostreamusingnamespacestd;intmain(){intmax(inta,intb,intc=0);inta,b,c;cinabc;coutmax(a,b,c)=max(a,b,c)endl;coutmax(a,b)=max(a,b)endl;return0;}intmax(inta,intb,intc){if(ba)a=b;if(ca)a=c;returna;}12:#includeiostreamusingnamespacestd;intmain(){voidchange(int,int);inta,b;cinab;if(ab)change(a,b);coutmax=amin=bendl;return0;}voidchange(int,int){intr1,r2,temp;temp=r1;r1=r2;r2=temp;}13:#includeiostreamusingnamespacestd;intmain(){voidsort(int&,int&,int&);inta,b,c,a1,b1,c1;coutPleaseenter3integers:;cinabc;a1=a;b1=b;c1=c;sort(a1,b1,c1);coutabcinsortedorderis;couta1b1c1endl;return0;}voidsort(int&i,int&j,int&k){voidchange(int&,int&);if(ij)change(i,j);if(ik)change(i,k);if(jk)change(j,k);}voidchange(int&x,int&y){inttemp;temp=x;x=y;y=temp;}14:#includeiostream#includestringusingnamespacestd;intmain(){strings1=week,s2=end;couts1=s1endl;couts2=s2endl;s1=s1+s2;coutThenewstringis:s1endl;return0;}15:#includeiostream#includestringusingnamespacestd;intmain(){stringstr;inti,n;chartemp;coutpleaseinputastring:;cinstr;n=str.size();for(i=0;in/2;i++){temp=str[i];str[i]=str[n-i-1];str[n-i-1]=temp;}coutstrendl;return0;}16:#includeiostream#includestringusingnamespacestd;intmain(){inti;stringstr[5]={BASIC,C,FORTRAN,C++,PASCAL};voidsort(string[]);sort(str);coutthesortedstrings:endl;for(i=0;i5;i++)coutstr[i];coutendl;return0;}voidsort(strings[]){inti,j;stringt;for(j=0;j5;j++)for(i=0;i5-j;i++)if(s[i]s[i+1]){t=s[i];s[i]=s[i+1];s[i+1]=t;}}17:#includeiostream#includestringusingnamespacestd;intmain(){longc[5]={10100,-123567,1198783,-165654,3456};inta[5]={1,9,0,23,-45};floatb[5]={2.4,7.6,5.5,6.6,-2.3};voidsort(int[]);voidsort(float[]);voidsort(long[]);sort(a);sort(b);sort(c);return0;}voidsort(inta[]){inti,j,t;for(j=0;j5;j++)for(i=0;i5-j;i++)if(a[i]a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}coutthesortednumbers:endl;for(i=0;i5;i++)couta[i];coutendlendl;}voidsort(longa[]){inti,j;longt;for(j=0;j5;j++)for(i=0;i5-j;i++)if(a[i]a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}coutthesortednumbers:endl;for(i=0;i5;i++)couta[i];coutendlendl;}voidsort(floata[]){inti,j;floatt;for(j=0;j5;j++)for(i=0;i5-j;i++)if(a[i]a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}coutthesortednumbers:endl;for(i=0;i5;i++)couta[i];coutendlendl;}18:#includeiostream#includestringusingnamespacestd;templatetypenameTvoidsort(Ta[]){inti,j,min;Tt;for(i=0;i5;i++){min=i;for(j=i+1;j5;j++)if(a[min]a[j])min=j;t=a[i];a[i]=a[min];a[min]=t;}coutthesortednumbers:endl;for(i=0;i5;i++)couta[i];coutendlendl;}intmain(){inta[5]={1,9,0,23,-45};floatb[5]={2.4,7.6,5.5,6.6,-2.3};longc[5]={10100,-123567,1198783,-165654,3456};sort(a);sort(b);sort(c);return0;}第二章1#includeiostreamusingnamespacestd;classTime{public:voidset_time();voidshow_time();private://成员改为公用的inthour;intminute;intsec;};voidTime::set_time()//在main函数之前定义{cinhour;cinminute;cinsec;}voidTime::show_time()//在main函数之前定义{couthour:minute:secendl;}intmain(){Timet1;t1.set_time();t1.show_time();return0;}2:#includeiostreamusingnamespacestd;classTime{public:voidset_time(void){cinhour;cinminute;cinsec;}voidshow_time(void){couthour:minute:secendl;}private:inthour;intminute;intsec;};Timet;intmain(){t.set_time();t.show_time();return0;}3:#includeiostreamusingnamespacestd;classTime{public:voidset_time(void);voidshow_time(void);private:inthour;intminute;intsec;};voidTime::set_time(void){cinhour;cinminute;cinsec;}voidTime::show_time(void){couthour:minute:secendl;}Timet;intmain(){t.set_time();t.show_time();return0;}4://xt2-4-1.cpp(main.cpp)#includeiostreamusingnamespacestd;#includext2-4.hintmain(){Studentstud;stud.set_value();stud.display();return0;}//xt2-4-2.cpp(即student.cpp)#includext2-4.h//在此文件中进行函数的定义#includeiostreamusingnamespacestd;//不要漏写此行voidStudent::display(){coutnum:numendl;coutname:nameendl;coutsex:sexendl;}voidStudent

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

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

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

×
保存成功