《c++课后部分习题答案.pdf》由会员分享,可在线阅读,更多相关《c++课后部分习题答案.pdf(45页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1.口令验证:#includeusing namespace std;int main()(char n10;coutvv”请输入 口 令:“vvendl;cinn;for(int i=0;i3;i+)(if(strcmp(n,welcome)=0)(coutv”口 令输入正确!”vvendl;break;)else(if(i2)(coutvv”密码错误,请再输入口令:v vendl;cinn;)elsecoutvv”你是非法用户!nendl;)1return 0;)2.#includeusing namespace std;int main()(int m30;double sum=0;do
2、uble average;int count=l;coutvv”请输入要统计的正实数,负数表示结束:“;do(count+;ci nm count;边输入边统计)while(mcount=0);for(int i=0;i=0)sum=sum+mi;)average=s u m/co unt;coutvc你输入 了“vvcountvc”个数“vvendl;coutvv他们的平均值为:vaverageendl;return 0;3.插入数组:#include using namespace std;int main()(int m5=1,3,579;int a,j,i;coutvv”插入前的数组为
3、:for(i=0;i5;i+)coutm i,tn;coutendl;cout”请输入要插入的数:;cina;fbr(i=0;imi)&(a=i;j-)mj+l=mjj;mi+1=a;)if(a=0;j-)mj+l=m|j;mO=a;)if(am4)(m5=a;)cout”插 入 a 后的数组为:;for(i=0;i6;i+)coutm itn;return 0;)4.删除数组#includeusing namespace std;int main()(int m5=135,7,9;int a,j,i;coutvv”插入前的数组为:;for(i=0;i5;i+)coutm i,tn;coute
4、ndl;coutvv”请输入要删除的数:;cina;fbr(i=0;i5;i+)if(mi=a)(for(j=i;j=5)coutv”查无此数!”vendl;coutvv”删 除 a 后的数组为:;for(i=0;i5;i+)coutm intn;)else(cout”删除a 后的数组为:“;for(i=0;i4;i+)coutm int;)return 0;)5.字符串复制:1 .#include#includeusing namespace std;int main()(int m=0;int i=0;char ch61=we are the best!;char a61;coutv”复制
5、后字符串为:while(chi!=A0)不可忘(ai=chi;i+;1ai=0,;couta;return 0;)2.#include#includeusing namespace std;int main()(int m=0;char ch61=nwe are the best!;char a61;coutv”复制后字符串为:fbr(int i=O;istrlen(ch);i+)#includeai=chi;coutai;)return 0;)6.进制转化:#include#includeusing namespace std;int main()(int a;int b10;cout”请输
6、入一个整数:cina;int i=0;while(a!=0)(bi=a%2;a=a/2;i+;)coutv=0;i)coutbi;coutendl;return 0;)6.打 印 金字塔#includeusing namespace std;void draw(int n)(fbr(i=0;in;i4-+)for(j=0;j2*n-l;j+)(if(j=n-i-l)(coutvvchar(A+i+j-5);)elsecout*coutendl;)int main()(int n;coutvv”请输入 n:1;cinn;draw(n);return 0;)7.计算多项式值:#includeusi
7、ng namespace std;int power(int i,int k)(intj;int result=l;fbr(j=l;j=k;j+)(result=result*i;return result;)int sumfac(int k,int i)(intj;int result=l;for(j=l;j=i;j+)result=result*k;return result;)int sum(int k,int n)(int i;int sum=0;fbr(i=l;i=n;i+)sum=sum+power(i,k)+sumfac(k,i);return sum;)int main()(i
8、nt k,n;cout”请输入k,n的值:;cin k n;cout”多项式 结果:Usum(k,n)e ndl;return 0;)8.置零:#includeusing namespace std;int main()(int m9=10,20,30,40,50,60,70,80,90);int i,j;cout 请输入 n:;cini;int*ptr=m;for(j=0;ji;j+)mj=0;coutvv”置零后的数组为:for(j=0;j9;j+)(cout*(ptr+j)ntn;)return 0;)9.字符串反转:#include#includeusing namespace std
9、;void mystrrev(char*string)(inti;char n20;int len=strlen(string);for(i=0;i=len;i+)nlen-i=*(string+i);fbr(i=O;i=len;i+)coutni;)int main()(char m20;cout vv”请输入待反转的字符串:cinm;char*ptr=m;cout”反转后的字符串为:;mystrrev(m);return 0;)1 0.比较字符串:#include#includeusing namespace std;int mystmicmp(char*str 1 ,char*str2,
10、int n)(int i,j;for(i=0;in;i+)(if(strcmp(strl,str2)0)j=l;)return j;)int main()(char m=competition”;char a=Mcoope ration;int n;char*strl=m;char*str2=a;coutvc请输入 n:;cinn;coutvv”比较结果为:mystrnicmp(strl,str2,n)endl;return 0;)去处空格:#include#includeusing namespace std;char*mytri m(char*string)(char*p=string;i
11、nt len=str)en(string);char*ptr=string+len-l;int i=0;while(*ptr=*)*ptr=、0;ptr-;)while(*ptr!=*p)ptr-;return ptr;)int main()(char str=who is buddha?cout 截取前字符串为:vvstrvvrvvendl;cout 截取后的字符串为:vvmytrim(str)vvTendl;return 0;)7.查找字符串:#includeusing namespace std;char*mystrchr(char*string,char c)(inti;int len
12、=strlen(string);fbr(i=0;i=len)return NULL;)int main()(char m=hello world!;char*string=m;char c;coutv”请输入要查找的字符串:cinc;coutv”该字符位置为:“vmystrchr(string,c)vvendl;return 0;)1 3.电话本:#include#includeusing namespace std;struct Personchar name10;int number;);int main()(int i;const int count=5;Person person co
13、unt;char name;int number;cout请输入5 个人的信息:vendl;fbr(i=O;i5;i+)(cinperson|i j.name;ci npersoni.number;)Person tmp;fbr(i=O;ii;j-)/必须有/(if(strcmp(personjl.name.pe rson j-1.name)0)(tmp=person|j|;personj|=person|j-l;/不可加 nam e,注意 /personj-l=tmp;)coutvv”按字典顺序排序:vendl;for(i=0;i5;i+)(coutpersoni.nameHtpersoni
14、.numberendl;)char m10;coutv”请输入待查找的名字:;cinm;fbr(i=O;i5;i+)(if(strcmp(personi.name,m)=0)(coutpersoni.namentnpersoni.numberendl;break;elsecoutvv”查无此人!”vvendl;)return 0;)14.查找数组最值:#includeusing namespace std;void result(int a,int ijnt&max_r,int&min_r)(intj;max_r=a|0|;min_r=a0;/不可缺少,否则无法正确输出最小值for(j=0;j
15、i;j+)(if(max_raj)注意引用max_r=aj;for(j=0;jaj)min_r=a|j;)int main()(int a10;int max,min;coutvv”请输入一组数:;for(int i=0;i5;i+)输入一组数cinai;result(a,5,max,min);coutvv”该组元素最大值和最小值为:;coutm ax Hm inendl;return 0;)15.构造猫类:#includeusing namespace std;class catchar Name20;char Sex 20;int Age;int Weight;public:cat(cha
16、r*name,char*sex,int age,int weight)(strcpy(N ame,name);strcpy(Sex,sex);Age=age;Wei ght=weight;coutvv,构造函数被调用!”vvendl;易疏漏)cat()(析构函数被调用!vvendl;)void displayO(coutNam et,S e x t,A g e Ht,W eightendl;用大写字母);int main()(coutvv第一只猫:vvendl;cat cat 1 (mi mi,female”,5,10);cat 1.displayO;调用函数时注意coutvv”第二只猫:vv
17、endl;cat cat2(huahua,male,6,14);cat2.display();return 0;)16.地址类:#includeusing namespace std;class address(char Name20;char Street20;char City20;int Postcode;public:void set(char*namel,char*street,char*city,int postcode)(strcpy(Name,name 1);strcpy(Street,street);strcpy(City,city);Postcode=postcode;)v
18、oid changename(char*name2)(strcpy(Name,name2);)void displayO(coutN am etStreett,C ity,tPostcodeendl;);int main()(char namel|20,street20,city20,name220J;int postcode;address address 1;coutnplease input the first address:;cinname 1 streetcitypostcode;address 1 ,set(name1,street,city,postcode);不可少addre
19、ss 1.displayO;coutplease input the changename:cinname2;address 1 .changename(name2);address 1.displayO;return 0;)17.圆类:#includeusing namespace std;class point(int x;int y;public:void set(int a,int b)(x=a;set不可忘y=b;)int getx()return x;)int gety()(return y;)void display()(cout Tx,y *e ndl;);class circ
20、le(point center;double radius;public:void setcenter(point p)(必须这样输,不可center=p;void setcenter(int a,int b)x=a;y=b;void setradius(double r)(radius=r;)double getradius()(return radius;)point getcenter()set后再get;get目的是取返回值return center;)double area()(return 3.14159*radius*radius;)double circumference()(
21、return 2*3.14159*radius;)void displayOcout,center=M;center.displayO;cout;radi us=Mradi usendl;);int main()(int a,b,r;point center;circle cl;coutvv”请输入圆心点:;cin a b;center.set(a,b);先调用 point 类coutvv”请输入半径:;cinr;c 1 .setcenter(center);再调用 circle 类cl.setradius(r);cl.displayO;cout”该圆的面积为:“vvcl.area()vven
22、dl;coutv”该圆的周长为:vvcl.circumference()vvendl;return 0;)18.汽车类:#includeusing namespace std;class vehicle(public:int Speed;必须全部设置为public,否则派生类无法调用double Weight;void set(int speed,double weight)(Speed=speed;Weight=weight;)void displayO(coutvv”最大时速:vvSpeedvv”t“vv”车体重量:“vvWeightvvendl;);class truck:public
23、vehicle 注意(double Load;double weight;public:void settru(int speed,double weight,double load)set(speed,weight);Load=load;)double ratio()(return Load/(Load+Weight);)void displaytru()(cout 最大载重:Load;displayO;);int main()int speed;double weight,load;coutv”请输入最高时速,车重,载重:;cinspeedweightload;truck trul;tru
24、 I.settru(speed,weight,load);trul.di splay tru();coutv”载重效率为:n tru l.ratio()endl;return 0;当机器提示出现无法访问私有函数时,可以将私有函数公有化.19.数据总分和平均分:#includeusing namespace std;class total_aveint array5,total;double ave;public:void get_aiTay()total=0;coutvv请输入5 个数据:vendl;for(int i=0;iget_array();必须使用指针,注意p-display();r
25、eturn 0;)20.时间类:#include#includeusing namespace std;class time(int hour,minute,second;public:time(int h=O,int m=0,int s=0);void settime(int h=O,int m=0,int s=0);void displayO;int seccalc();time ope rat or+(ti me&);注意格式time operator-(time&););time:time(int h,int m,int s)(settime(h,m,s);)void time:set
26、time(int hh,int mm,int ss)(hour=(hh23llhh60llmm60llss0)?0:ss;)void time:display()(cout(hour10)?,0,:,)h o u r,:(minute10)?,0:,)m inute:,(second10)?u0,:,)secondendl;注意表示方法,括号不可省,hour,second,minute 不可省)int time:seccalc()(return hour*3600+minute*60+second;)time time:operator+(time&t)(time tmp;int ss;ss=
27、seccalc()+t.seccalc();tmp.hour=ss/3600%24;tmp.minute=ss%3600/60;tmp.second=ss%3600%60;return tmp;)time time:operator-(time&t)(time tmp;int ss;ss=abs(seccalc()-t.seccalc();tmp.hour=ss/3600%24;tmp.minute=ss%3600/60;tmp.second=ss%3600%60;return tmp;)int main()(time tl(l,13,25),t2,t3;t2.settime(23,18,45
28、);couttl:“;tl.displayO;coutnt2:n;t2.display();t3=tl-t2;coutntl-t2:n;t3.display();t3=tl+t2;coutMtl+t2:u;t3.display();return 0;为计算时间差,和准备注意格式,不要输错operator注意设置tmp;注意小时,分钟,秒钟的表示注意返回值注意设置绝对值*21.文件的统计:#include#includeusing namespace std;int main()(ofstream outfile(Hal.txt);if(!outfile)(coutvv”文件打开失败!”vven
29、dl;return 1;)outfilenI like visual c+!nendl;outfile.close();ifstream infile(,al.txt);if(!infile)(coutvv”文件打开失败!Mendl;return 0;)char ch;int count=0;int sum=l;while(!infile.eof()(infile.get(ch);count+;if(ch=)sum+;1cout”文件总长度为:ncountendl;cout”单次个 数为:Hsum endl;infile.close();return 0;)2 2.不同域宽:#include#
30、includeusing namespace std;int main()(int a=12345;double b=L2345;cout vv”按不同的精度打印:n e ndl;for(int i=0;il l;i+)为什么总长度多2coutsetw(i)aendl;coutset w(i)b e ndl;)return 0;)23.表示字符:#includeusing namespace std;int main()(for(int i=33;i127;i+)(cout 十进制输出为:n d e c i e ndl;coutvv”八进制输出为:“vvoctvviendl;cout十六进制输
31、出为:n h ex i en d l;coutvv”字符输出为:char(i)endl;)return 0;)24.进制转化:#include#includeusing namespace std;int main()(ofstream outfile(Habc.txtM);int a;coutvv”请输入一个十进制数:;cina;if(!outfile)(cout v v”文件打开失败!n e ndl;return 1;)outfile vv”十 六 进 制 为 hexvav“t;outfilecv八进制为:octaendl;outfile.close();return 0;)25.统计非负
32、数:#include#includeusing namespace std;int main()ofstream outfile(,sum.txt,);int m10;int sum=0;inti;cout”请输入十个数:nendl;if(Joutfile)(coutvv”文 彳 牛 打开失败!”vvendl;return 1;)for(i=0;i10;i+)(cinmi;)for(i=0;i=0)(cout非负数为:outfilecv”非负数为:sum=sum+mi;)coutvv”非负数之和为:vvsumvvendl;outfile”非负数之和为:vsumendl;outfile.clos
33、e();return 0;)2 3.打印杨辉三角:#include#includeusing namespace std;int main()(ofstream outfile(Hreport.txtn);if(!outfile)(cout 文件打开失败!”e ndl;return 1;)int m2020;int i,j,n;coutplease input the integer:endl;f o r(i=0;i 1 0;i+)(m i O =l;m i i =l;f o r(i=2;i 1 0;i+)(f o r(j=l;j i;j+)m i j =m i-l U-l +m i-l U
34、;)f o r(i=0;i 10;i+)(f b r(j=O;j=i;j+)(c o u t m i|j n tn;o u t f i l e m i U ,tn;)c o u t e n d l;o u t f i l e e n d l;)o u t f i l e.c l o s e();r e t u r n 0;)27分解输出:#i n c l u d e#i n c l u d e u s i n g n a m e s p a c e s t d;i n t m a i n()(i n t m,t,a;c o u t v v”请输入一个数:;c i n m;f o r(t=0;t
35、 a)break;)for(intj=i-l;j=0;j-)(cout*(ptr+j)v”,”;)return 0;)如出现数据比较高时用字符数组输出。29.打印单词:#includeusing namespace std;int main()(char ch;int n,i;coutMplease input the letter:;cinch;coutplease input number:;cinn;if(ch-n=,A,&ch+n=,a,&(ch+n)=z,)(for(i=0;i=2*n;i+)(coutchar(ch-n+i);)else if(chna)(for(i=0;i=2*n
36、;i+)(if(ch-n+iz,)fbr(i=O;in;i+)(coutchar(ch-n+i);)fbr(i=O;i*z)(coutchar(ch+i-26);)elsecoutchar(ch+i);)return 0;)不可将大写,小写字母混淆;30.成绩统计:#includeusing namespace std;int main()(int m10;int i,tmp,j;double average;double sum=0;coutv”请输入十个评委打分:”;for(i=0;i10;i+)(cinmi;)coutv十个评委打分:vendl;for(i=0;i10;i+)(coutm
37、 intu;)for(i=0;ii;j-)if(m|j-l|mj)(tmp=m|j-l;mj-l=mj;mj=tmp;)for(i=0;i10;i+)sum=sum+mi;)sum二average=sum/6;cout”平均 分为:nave rage e ndl;return 0;)31o判断打破玻璃者:#includeusing namespace std;int main()(int k;for(k=l;k=4;k+)将四人分别赋值1,2,3,4,检验谁打破玻璃if(k!=l)+(k=3)+(k=4)+(k!=4)=3)8 戊 打破玻璃者是:“vchar(k+64)vendl;显示打破玻璃
38、者return 0;)31.搬砖:#includeusing namespace std;int main()(int x,y,z;x=0;赋初值很关犍coutvv”搬砖结果如下:endl;while(x8)(y=o;while(yl 1)(z=36-x-y;if(4*x+3*y+z/2=36)&(z%2=0)cout“男人:“v v x u 女人:n y 小孩:“v vz vve ndl;y+;x+;)return 0;)34.鸡数量:#includeusing namespace std;int main()(int x,y,z;x=l;while(x19)(y=0;while(y32)(
39、z=100-x-y;if(5*x+3*y+z/3=100&z%3=0)coin vv鸡翁:v v x v v 鸡母:“vyvv鸡雏:Nvzvvendl;y+;)x+;)return 0;)3 4.判断素数:#includeusing namespace std;int main()(int i,j,flag;cout 素数为:“vve ndl;for(i=100;i=200;i+)(flag=0;位置放对for(j=2;j=i/2;j+)(if(i%j=O)(flag=l;break;位置放对if(flag=l)continue;c o u t iM H;)return 0;)35.解方程:#
40、include#includeusing namespace std;int main()(double a,b,c,delta;coutvv”请输入 a,b,c:Mendl;c in a b c;if(a=0&b=0&c=0)coutvv”方程的解可为任意值!nendl;else if(a=0&b!=0)使用 else ifcoutx l=x2=-c/bendl;else(delta=b*b-4*a*c;if(delta0)(coutxl=-b/(2*a)4-,sqrt(abs(delta)/(2*a),i,endl;cout,x2=*,-b/(2*a),-,sqrt(abs(delta)/
41、(2*a),i,endl;)else(cout,xl=,-b/(2*a)n+,sqrt(delta)/(2*a)endl;cout,x2=n-b/(2*a)-,sqrt(delta)/(2*a)endl;)return 0;)36.字符串连接:#include#includeusing namespace std;int main()char destination=nabcdefgh;char source口 二 ABCDEFGH”;int i=strlen(destination);int a=strlen(source);intj;for(j=0;ja;j+)(desti nation
42、i+j=source j;)fbr(j=O;ja+i;j+)(coutdesti nation j;)return 0;)37.弥撒加密法:#include#includeusing namespace std;int main()(char str50=nI love you!;coutvv”加密前的字符串为:strendl;char m50;inti;for(i=0;i=,a,&stri=,A&stri=Z)stri=(stri+3-A)%26+,A,;)cout”加密后的字符串为:H s tr e ndl;return 0;)38.静态变量的使用:#includeusing namesp
43、ace std;int func()(static count=0;count+;return count;越界字母表示方法如 X 变为A;int main()for(int i=0;i10;i+)coutfunc(),t;coutendl;return 0;面向对象的程序设计特点:自顶向下,逐步求精一功能分解3 9.课本最后一题#include#includeusing namespace std;int main()(int i;ofstream outfilel(nal.txtn);char m|20;if(!outfilel)coutcannot open filenendl;retu
44、rn 1;)cout”请输入一行字符:e ndl;cin.get(m,20);outfilelm;输出输入字符时不用mi:outfilel.close();ifstream infile l(nal.txtM);ofstream outfile2(na2.txtn);char ch;if(!infilelll!outfile2)(coutcannot open filenendl;exit(l);)while(iinfilel.eof()infilel.get(ch);if(ch=a&ch=,z)outfile2char(ch+,A,-,a);elseoutfile2ch;infile l.c
45、lose();outfile2.close();return 0;2.#include#includeusing namespace std;int main()char array20;char ch;ofstream outfilel(/zal.txt,z);ifstream infile(al.txt);ofstream outfile2(a2.txt);if(!outf ilel&!inf ile&!outf ile2)(coutz/0pen error!z,endl;exit(1);)outfi lelz/abcdefghik,zendl;while(!infile,eof()(in
46、file,get(ch);outfile2.put(ch);outfilel.close();outfile2.close();infile.close0;)41.Hebingwenjian#include#includeusing namespace std;int main()(ifstream infilel(,zal.txtz,);ifstream infile2(z,a2.txtz,);ofstream outfile3(z,a3.txt);char ch;while(!infilel.eof()(infilel.get(ch);outfile3ch;w h i l e(!i n f
47、 i l e 2.e o f ()(i n f i l e 2.g e t (c h);o u t f i l e 3 c h;)i n f i l e l.c l o s e();i n f i l e 2.c l o s e O ;o u t f i l e 3.c l o s e();r e t u r n 0;)41.引用交换两数:#i n c l u d e u s i n g n a m e s p a c e s t d;v o i d s w a p (i n t&x,i n t&y)(i n t t m p;t m p=x;x=y;y=t m p;)i n t m a i n
48、()(i n t a=3,b=2;c o u t 交换前两数为:z/az z t,b e n d l;s w a p (a,b);c o u t 交换后两数为:,a,t,b e n d l;r e t u r n 0;)4 2.指针冒泡排序:#i n c l u d e u s i n g n a m e s p a c e s t d;v o i d b u b b l e u p(i n t*p t r)(i n t i,j;f o r (i=0;i=i;j )i n t t m p;i f(*(p t r+i-1)*(p t r+i)t m p=*(p t r+i-l);*(p t r+
49、i-1)=*(p t r+i);*(p t r+i)=t m p;)i n t m a i n ()(i n t i;i n t a =5 3,7,12,6 1,9 0,70,87);c o u t 排序前数列为:,e n d l;f o r (i=0;i 7;i+)c o u t a i ,t/;c o u t e n d l;b u b b l e u p (a);c o u t X 排序后数列为:e n d l;f o r (i=0;i 7;i+)c o u t a i /z tz,;r e t u r n 0;)4 5.复制字符数组:#i n c l u d e#i n c l u d
50、 e u s i n g n a m e s p a c e s t d;v o i d m y s t r c p y(c h a r*d e s t i n,c h a r*s o u r c e)(w h i l e(s o u r c e!=0)*d e s t i n=*s o u r c e;s o u r c e+;d e s t i n+;)*d e s t i n=,0;i n t m a i n()(c h a r a口 二 w h o i s b u d d h a?;c h a r m 3 0;m y s t r c p y (m,a);c o u t 复制后的字符为: