c++课后部分习题答案.pdf
《c++课后部分习题答案.pdf》由会员分享,可在线阅读,更多相关《c++课后部分习题答案.pdf(45页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 课后 部分 习题 答案
限制150内