VisualC++面向对象编程教程第2版清华大学出版社课后答案_第1页
VisualC++面向对象编程教程第2版清华大学出版社课后答案_第2页
VisualC++面向对象编程教程第2版清华大学出版社课后答案_第3页
VisualC++面向对象编程教程第2版清华大学出版社课后答案_第4页
VisualC++面向对象编程教程第2版清华大学出版社课后答案_第5页
已阅读5页,还剩87页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

...wd......wd......wd...2-45编写一个程序,输入三角形的三条边的边长,求三角形的面积。2-45#include<iostream.h>#include<math.h>intmain(){ inta,b,c; ints,S; cin>>a>>b>>c; s=(a+b+c)/2; S=powl(s*(s-a)*(s-b)*(s-c),0.5); if(S==0) cout<<"此三边不能构成三角形!"<<endl; else { cout<<"面积:"<<endl; cout<<S<<endl; } return0;}2-46从键盘输入一个大写字母,然后改用小写字母在屏幕输出。2-46#include<iostream.h>intmain(){ charch; inta; cout<<"输入一个大写字母:"<<endl; cin>>ch; if(ch>=65&&ch<=91) { ch=ch+32;// a=(int)ch; cout<<ch<<endl; } elsecout<<"该字母不是字母或不是字母大写!"<<endl;return0;}2-47用户输入两个整数,编程输出稍大于第一个整数而又是第2个整数的倍数的数。计算公式是:valuel+value2-value1%value2.2-47#include<iostream.h>intmain(){ intvalue1,value2; intresult; cin>>value1>>value2; result=value1+value2-value1%value2; cout<<"该整数为:"<<result<<endl; return0;}2-48华氏温度转换为摄氏温度的公式是:C=〔F-32〕*5/9.。编写一个程序,输入一个华氏温度,程序输出相应的摄氏温度。请将32和5/9用const型变量表示。2-48#include<iostream.h>intmain(){ constfloati=32.0; constfloatj=5.0/9.0; floatdegFahr; floatdegCel; cin>>degFahr;// for(degFahr=0;degFahr<=300;degFahr+=10)// { degCel=j*(degFahr-i); cout<<"华氏温度:"<<degFahr<<endl; cout<<"摄氏温度:"<<degCel<<endl;// } return0;}2-49从键盘输入20个整数,检查100是否存在于这些整数中,假设是的话,求出他是第几个被输入的。2-49#include<iostream.h>intmain(){ intArray[20]; inti,count=0,c=0; for(i=0;i<20;i++) cin>>Array[i]; for(i=0;i<20;i++) { if(Array[i]==100&&count==0) { cout<<"100存在该数组中!"<<endl; count++; } if(Array[i]!=100) { c++; if(c==19) cout<<"100不存在该数组中!"<<endl; } } for(i=0;i<20;i++) { if(Array[i]==100) cout<<"它是第"<<i+1<<"个被输入的!"<<endl; } return0;}2-50从键盘输入一个NXN的整型数组,并将每一行的最大值显示输出。2-50#include<iostream.h>intmain(){ intArray[100][100]={0}; intn,temp; cin>>n; for(inti=0;i<n;i++) { for(intj=0;j<n;j++) cin>>Array[i][j]; for(intx=0;x<n;x++) for(inty=x+1;y<n;y++) if(Array[i][x]<Array[i][y]) { temp=Array[i][x]; Array[i][x]=Array[i][y]; Array[i][y]=temp; } } cout<<"每一行的最大值:"<<endl; for(i=0;i<n;i++) cout<<Array[i][0]<<""; cout<<endl;}2-51输入三个整数,采用指针方法将三个数按从大到小的顺序输出。2-51#include<iostream.h>#include<math.h>intmain(){ intArray[3]; int*pA=Array; int*a=Array; inti; inttemp; for(i=0;i<3;i++) cin>>Array[i]; for(i=0;i<3;i++) { a++; if(*pA<*a) { temp=*pA; *pA=*a; *a=temp; a++;pA++; } a=&Array[0]; } for(i=0;i<3;i++) cout<<Array[i]<<""; return0;}2-52采用指针方法将一个数组中的所有元素颠倒顺序,结果仍然存放在原来的数组中,要求使用最少的辅助存储单元2-52#include<iostream.h>intmain(){ intn; cout<<"输入数组的长度(小于100):"<<endl; cin>>n; cout<<"输入"<<n<<"个数:"<<endl; intArray[100]; int*pA=Array; inti; for(i=0;i<n;i++) { cin>>Array[i];pA++; } for(i=0;i<n;i++) {pA--; cout<<*pA<<""; }return0;}2-53输入两个字符串,如果两个字符串的字符和长度都一样〔认为它们相等〕,在屏幕上输出“Equal〞,否则在屏幕上输出“Unequal〞。要求使用字符指针。2-53#include<iostream.h>intmain(){ char*pstr="Enterastring:"; charstr1[100],str2[100]; cout<<pstr<<endl; cin.get(str1,100); cin.get(); cin.get(str2,100); intlen1=0,len2=0; while(str1[len1]!='\0') len1++; while(str2[len2]!='\0') len2++; if(len1!=len2) cout<<"Unequal"<<endl; elseif(len1==len2) { inti=0; for(i=0;i<len1;i++) if((str1[i]==str2[i])&&i<len1) continue; if(i=len1-1) cout<<"Equal"<<endl; elsecout<<"Unequal"<<endl; } return0;}2-54编程将一个整数转换成对应的数字串,例如将值1234转换为数字串“1234〞。2-54#include<iostream.h>intmain(){ intnumber; intArray[100]; inti,j; cin>>number; for(i=0;number!=0;i++) { Array[i]=number%10; number=number/10; } j=i; for(i=j;i>=0;i--) cout<<Array[i-1]<<""; cout<<endl; return0;}2-55编程求两个复数的和。2-55#include<iostream.h>intmain(){ floata[2],b[2],c[2]; inti; for(i=0;i<2;i++) cin>>a[i]; for(i=0;i<2;i++) cin>>b[i]; for(i=0;i<2;i++) c[i]=a[i]+b[i]; cout<<c[0]<<""<<c[1]<<endl; return0;}2-56使用构造变量表示每个学生的信息:姓名、学号和三门课的成绩。从键盘输入10个学生的数据,然后输出每个学生的姓名和三门课的平均成绩。2-56#include<stdio.h>structstudent { charnum[10]; charname[20]; floatgrade[3]; floataverage; };intmain(){ studentstu[10]; inti,j; floatsum=0; for(i=0;i<=9;i++) { printf("Enternum:\n"); scanf("%s",&stu[i].num); printf("Entername:\n"); scanf("%s",&stu[i].name); printf("Enterthreegrades:\n"); for(j=0;j<3;j++) { scanf("%f",&stu[i].grade[j]); sum+=stu[i].grade[j]; } stu[i].average=sum/3; printf("%f%s\n",stu[i].average,stu[i].name); } return0;}2-57用构造数组建设并初始化一个工资表,然后输入一个人的姓名,查询其工资情况,并在屏幕上输出。2-572-58用枚举值MON、TUE、WED、THU、FRI、SAT和SUN表示一个星期中的7天。从键盘输入一个0~6之间的整数,根据输入的整数输出对应的英文缩写。2-582-59编写一个求解一元二次方程的根的程序,方程的系数由用户输入。2-59#include<iostream.h>#include<math.h>intmain(){ inta,b,c; intR; floatp; floatx1,x2; cin>>a>>b>>c; R=b*b-4*a*c; if(R>=0) { p=sqrt(R); x1=(-b+p)/(2*a); x2=(-b-p)/(2*a); if(x1==x2) cout<<"仅且只有一个根"<<x1<<endl; else cout<<"有两个不同的根"<<x1<<x2<<endl; } elseif(R<0)cout<<"无根"<<endl;return0;}2-60从键盘输入一个字符,判断输入的字符是m、a、n或其他字符。如果是m则输出“Goodmorning!〞;如果是a则输出“Goodafternoon!〞;如果是n则输出“Goodnight!〞;如果是其他字符则输出“Ican‘tundersrand!〞。2-602-61编程实现两个整数的加、减、乘、除四则运算,运算式形如“32+120〞。2-612-62编写一个程序,利用swith语句将百分制的学生成绩转换为优、良、中、及格和不及格5分制成绩。2-622-63从键盘输入一个字符,判断输入的字符是数字、空格还是其他字符,并给出相应的提示信息。2-63#include<iostream.h>intmain(){ charch; cout<<"请输入一个字符:"; cin.get(ch); if(ch=='') cout<<"这是一个空格!"<<endl; elseif((ch>='0')&&(ch<='9')) cout<<"这是一个数字!"<<endl; else cout<<"这是一个其他字符!"<<endl; return0;}2-64从键盘输入一个字符序列,编程统计其中的数字个数和英文字母个数。输入的字符序列以“#〞作为完毕符。2-64#include<iostream.h>intmain(){ charsymbol[100]; inti,end,j=0,k=0; cin>>symbol; for(i=0;i<100;i++) if(symbol[i]=='#') { end=i; break; } for(i=0;i<end;i++) { if((symbol[i]>='0')&&(symbol[i]<='9')) j++; elsek++; } cout<<"digit:"<<j<<endl; cout<<"letter:"<<k<<endl; return0;}2-65输入一个由假设干单词组成的文本串,每个单词之间用一些空格分隔,统计此文本串单词的个数。2-65#include<iostream.h>intmain(){ charstr[1000]; intcount=1; cin.get(str,1000);// while(!str[0])// { for(inti=0;str[i]!=0;i++) if(str[i]==''&&str[i+1]!='') count++;// } cout<<"thecountis:"<<count<<endl; return0;}2-67编程求π值,使用如下公式:π/4=1-1/3+1/5-1/7+…,直到最后一项的绝对值小于10-6为止。2-67〔1〕#include<iostream.h>#include<math.h>intmain(){ inti,n=1,j=1; doubles=0; for(i=0;(2*i+1)<pow(10,6);i++) { /*cout<<i<<endl; cout<<"******************"; cout<<2*i+1<<endl; cout<<"********************************"; cout<<pow((2*i+1),-1)<<endl; cout<<"******************";*/ s+=pow((2*i+1),-1)*n; //j=2*i+1; n=n*(-1); } cout<<i<<endl; cout<<4*s<<endl; return0;}2-67(2)#include<iostream.h>#include<math.h>intmain(){floati=0;doubles=0;for(i=0;2*i+1<pow(10,6);i++){s+=pow(-1,i)*(1/(2*i+1));}cout<<s*4<<endl;return0;}2-68把100~150之间不能被3整除的数输出,要求一行输出10个数。2-682-69编程输出一个九九乘法表。2-69#include<iostream.h>intmain(){ chartable[9][9]; inti,j,X,Y; for(i=0;i<9;i++) { for(j=0;j<9;j++) { X=i+1; Y=j+1; if(X>=Y) cout<<X*Y<<''; } cout<<endl; } return0;}2-70编程计算整型数各位数字之和,例如数2007各位数字之和为2+0+0+7=9.2-70#include<iostream.h>intmain(){ intnum; intsum=0,i; cin>>num; for(i=0;num!=0;i++) { sum+=num%10; num=num/10; } cout<<"sum="<<sum<<endl; return0;}2-71输入n个整数,利用冒泡排序法将它们从小到大排列,并在屏幕上输出。2-71#include<iostream.h>intmain(){ intn; inti,j,temp; intnum[100]; cin>>n; for(i=0;i<n;i++) cin>>num[i]; for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(num[i]<num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } for(i=0;i<n;i++) cout<<num[i]<<''; cout<<endl; return0;}2-72编程求出从键盘输入的10个数之和,遇到负数时终止输入求和。2-72#include<iostream.h>intmain(){ intArray[10]; intsum=0; for(inti=0;i<10;i++) { cin>>Array[i]; if(Array[i]>0) sum+=Array[i]; else break; } cout<<sum<<endl; return0;}2-73编程求出从键盘输入的10个数中所有正数之和,负数不参加求和。2-73#include<iostream.h>intmain(){ intnum[10]; inti,sum=0; for(i=0;i<=9;i++) { cin>>num[i]; if(num[i]>0) sum+=num[i]; } cout<<"sumis"<<sum<<endl;return0;}2-74设计函数prime(),它只带一个整型参数,当这个参数的值是素数时,该函数返回非0,否则返回0.利用这个函数编写一个程序来验证哥德巴赫猜想:任何一个大于2的偶数都可以表示成两个素数之和。2-74#include<iostream.h>intprime(intx){ inti; for(i=2;i<x/2;i++) if(x%i==0) return0; returnx;} intmain(){ inta,b,c,d; intj=2,k; cin>>a; if(a>2&&a%2==0) { for(j=2;j<a;j++) { b=prime(j); if(b==j) c=a-b; d=prime(c); if(d==c) cout<<"该定理成立!"<<endl; } } elsecout<<"此数不是大于2的偶数!"<<endl;return0;}2-75编制如下函数原型的函数:intindex(constchar*str,charc),这个函数返回字符串str中第一次出现字符c的位置。2-75#include<iostream.h>intindex(constchar*str,charc){ intcount=1; intCcount=0; for(inti=0;str[i]!='\0';i++) { if(str[i]==c&&Ccount==0) { Ccount++; break; } count++; } returncount;} intmain(){ charstring[100]; charC; intV=0; intresult; cin>>string; cin>>C; while(string[V]!='\0') V++; result=index(string,C); if((V+1)==result) cout<<"此字符不在字符串中!"<<endl; else cout<<result<<endl; return0;}2-76首先编写以下函数声明的函数:voidswap(float*px,float*py),该函数用于交换两个实型变量的值,然后编写一个主函数验证函数swap()的功能。2-76#include<iostream.h>voidswap(float*px,float*py){ floattemp; temp=*px; *px=*py; *py=temp;}intmain(){ floatX,Y; cin>>X>>Y; swap(&X,&Y); cout<<"使用swap函数的值"; cout<<X<<''<<Y<<endl;/* floattemp; { temp=X; X=Y; Y=temp; }*/ cout<<"验证值:"<<X<<''<<Y<<endl;return0;}2-77定义将一个字符串反转的函数,例如将字符串“abcd〞反转为“dcba〞。2-77#include<iostream.h>#include<string.h>intArray(char*s){ intj,n; charstring[100]; strcpy(string,s); for(j=0;j<100;j++) if(string[j]=='\0') n=j; returnn;}intmain(){ charstr[100]; inti,z; cin>>str; z=Array(str);// cout<<z; for(i=z;i>=0;i--) cout<<str[i]; cout<<endl;return0;}2-78首先编写一个冒泡排序函数,然后在主函数中调用排序函数对10个整数从小到达进展排序。提示:采用数组名作为函数参数。2-782-79将习题2-76中的swap()函数改为内联函数,并实现一样的程序功能。2-79#include<iostream.h>inlinevoidswap(float*px,float*py){ floattemp; temp=*px; *px=*py; *py=temp;}intmain(){ floatX,Y; cin>>X>>Y; swap(&X,&Y); cout<<"使用swap函数的值"; cout<<X<<''<<Y<<endl;/* floattemp; { temp=X; X=Y; Y=temp; }*/ cout<<"验证值:"<<X<<''<<Y<<endl;return0;}2-80编写一个函数maxmin(),该函数有两个实型参数,执行函数后,第一个参数为两个参数中值较大者,第二个参数为较小者。要求使用引用作为函数参数,并编写主函数验证函数功能。2-80#include<iostream.h>voidmaxmin(int&A,int&B){ inttemp; if(A<B) { temp=A; A=B; B=temp; }}intmain(){ intX,Y; cin>>X>>Y; maxmin(X,Y); cout<<X<<''<<Y<<endl;return0;}2-81编写一个函数swapstruct(),实现交换两个构造变量的功能。编写主函数验证函数swapstruct()的功能,要求使用引用传递参数。2-81#include<iostream.h>#include<string.h>structstudent{ charname[20]; intscore;};voidswapstruct(student&s1,student&s2){ inttemp; chars[20]; strcpy(s,); strcpy(,); strcpy(,s); temp=s1.score; s1.score=s2.score; s2.score=temp;}intmain(){ studentstu1={"zhangsan",90}; studentstu2={"lisi",85}; cout<<"stu1:"<<<<''<<stu1.score<<endl; cout<<"stu2:"<<<<''<<stu2.score<<endl; swapstruct(stu1,stu2); cout<<"stu1:"<<<<''<<stu1.score<<endl; cout<<"stu2:"<<<<''<<stu2.score<<endl;return0;}2-82编写一个程序,在主函数main()的外部和内局部别声明两个同名的整型变量并赋值,然后在主函数main()中分别访问两个变量。2-82#include<iostream.h>inti=3;intmain(){ cout<<i<<endl; inti=5; cout<<i<<endl; return0;}2-83一个程序由两个C++源文件组成,在一个源文件中定义主函数main()并声明一个外部整型变量n,在另一个源文件中定义一个不带参数的函数factorial(void),该函数用于计算变量n的阶乘。编程在主函数main()中输入一个整数并求它的阶乘。2-83#include<iostream.h>intfactorial(intx){ inty,pro=1; for(y=x;y>0;y--) pro*=y; return(pro);}voidmain(){ intn,R; cout<<"Thenumberis:"; cin>>n; R=factorial(n); cout<<"Theproductis:"<<R<<endl;}2-84采用外部函数的方式使用习题2-75中的函数index(),即在一个源文件中定义该函数,然后在另一个源文件中调用该函数。2-842-85编写一段程序,利用new运算动态分配float型、long型和char型三个内存单元,将它们的首地址分别赋给指针pf、pl和pc。给这些存储但愿赋值,并在屏幕上显示它们的值。最后利用delete运算释放所有动态分配的内存单元。2-85#include<iostream.h>intmain(){ float*pf=newfloat; *pf=3.14; long*pl=newlong; *pl=2135567889; char*pc=newchar; *pc='A'; cout<<*pf<<endl; cout<<*pl<<endl; cout<<*pc<<endl; deletepf; deletepl; deletepc; return0;} 2-86编写一个程序,用new运算为一个整型数组动态分配内存空间,对其进展赋值,并在屏幕上输出。2-86#include<iostream.h>intmain(){ intsize; intnum; cin>>size; int*f=newint[size]; for(inti=0;i<size;i++) { cin>>num; f[i]=num; } for(i=0;i<size;i++) cout<<f[i]<<''; delete[]f;return0;}2-87采用动态内存分配方法设计一个学生成绩处理程序,要求输入任何数量学生的学号、姓名和四门课的成绩,并按平均成绩的上下输出每个学生的上下输出每个学生的姓名和成绩。2-87#include<iostream.h>#include<string.h>structstudent{ intID; charname[20]; intgrade[4]; floataverage;};intmain(){ intnum; inti,j,k,temp; floatsum=0; chars[20]; cin>>num; student*stu=newstudent[num]; for(i=0;i<num;i++) { cout<<"输入第"<<i+1<<"人的信息"<<endl; cout<<"ID:"; cin>>stu[i].ID; cout<<"name:"; cin>>stu[i].name; cout<<"grade:"; for(j=0;j<4;j++) { cin>>stu[i].grade[j]; sum+=stu[i].grade[j]; } stu[i].average=sum/4; sum=0; cout<<"average:"<<stu[i].average<<endl; } for(i=0;i<num;i++)//排序 for(j=i+1;j<num;j++) if(stu[i].average<stu[j].average) { { strcpy(s,stu[i].name); strcpy(stu[i].name,stu[j].name); strcpy(stu[j].name,s); } for(k=0;k<4;k++) { temp=stu[i].grade[k]; stu[i].grade[k]=stu[j].grade[k]; stu[j].grade[k]=temp; } } cout<<"成绩排名:"<<endl; cout<<"name"<<""<<"grade"<<endl; for(i=0;i<num;i++) { cout<<stu[i].name<<""; for(j=0;j<4;j++) cout<<stu[i].grade[j]<<''; cout<<endl; } return0;}2-88输入一行字符,建设一个链表,链表的每一个结点含有一个输入的字符,通过访问链表中的每个结点计算链表中结点的总个数。2-882-89使用带参数的宏定义计算两个实数之和,并编写主函数验证宏的功能。2-89#include<iostream.h>#definesum(a,b)(a+b)intmain(){ intx,y; cin>>x>>y; cout<<"主函数计算结果:"<<x+y<<endl; cout<<"宏定义函数结果:"<<sum(x,y)<<endl; return0;}2-90定义一个带参数的宏,求出三个数中最大的一个数,并进展验证。2-90#include<iostream.h>#defineMAX(a,b)((a)>(b)?(a):(b))voidmain(){ intx,y,z; cin>>x>>y>>z; cout<<"宏定义参数结果"; cout<<MAX(MAX(x,y),z)<<endl; if(x>y) { if(x>z) cout<<"验证结果为"<<x<<endl; elsecout<<"验证结果为"<<z<<endl; } else { if(y>z) cout<<"验证结果为"<<y<<endl; elsecout<<"验证结果为"<<z<<endl; }}2-91输入一个字符串,根据需要设置条件编译,使之能将输入的字符串以大写字母的形式或小写字母的形式输出。2-91#include<iostream.h>intmain(){ charstr[100]; charX; inti; cin>>str; cout<<"是否输出大写形式(YorN)"<<endl; cin>>X; if(X=='Y') for(i=0;str[i]!='\0';i++) { str[i]=str[i]-32; cout<<str[i]; } elseif(X=='N') for(i=0;str[i]!='\0';i++) cout<<str[i]; cout<<endl; return0;}2-92修改习题2-61中的求和程序,在程序中定义一个调试宏,利用条件编译指令编译不同的代码段,使得在调试程序时能够输出一些调试信息。2-922-93假设有三个文件:test13.h、test2.h和test.cpp,在test1.h中定义了一个宏PI,test2.h文件包含了test1.h文件,而test.cpp文件又包含了test1.h文件和test2.h文件。请问编译时会出现什么错误假设何解决提示:宏的重复定义。2-933-44一个名为CPerson的类有如下属性:姓名、身份证号、性别和年龄,请用C++语言定义这个类,并为上述属性定义相应的方法。#include<iostream.h>#include<string.h>classCPerson{private:charName[10];charID[20];charSex[4];intAge;public:CPerson(char*na,char*id,char*se,intag);voidShow();};CPerson::CPerson(char*na,char*id,char*se,intag){strcpy(Name,na);strcpy(ID,id);strcpy(Sex,se);Age=ag;}voidCPerson::Show(){cout<<"姓名:"<<Name<<endl;cout<<"身份证:"<<ID<<endl;cout<<"性别:"<<Sex<<endl;cout<<"年龄:"<<Age<<endl;}voidmain(){CPersonperson("王三",,"男",21);person.Show();}3-45设计一个日期类Date,该类用于表示日期值〔年、月、日〕。要求除了能够通过相应得成员函数设置和获取日期外,还能够实现将日期加一天的操作。#include<iostream.h>classdate{intyear;intmonth;intday;boolflag;public:date(){year=0;month=0;day=0;}date(intyr,intmo,intda);voidsetdate();intgetyear();intgetmonth();intgetday();voidaddday();voidshow();};date::date(intyr,intmo,intda){flag=false;if(mo>=1&&mo<=12&&da>=1&&da<=31){year=yr;month=mo;day=da;}else{flag=true;}}voiddate::setdate(){cout<<"请输入年分"<<endl;cin>>year;cout<<"请输入月份(1-12)"<<endl;cin>>month;while(month<1||month>12){cout<<"输入有误,请重新输入月份(1-12)"<<endl;cin>>month;}cout<<"请输入日期(1-31)"<<endl;cin>>day;while(day<1||day>31){cout<<"输入有误,请重新输入日期(1-31)"<<endl;cin>>day;}flag=false;}voiddate::show(){if(!flag)cout<<year<<'/'<<month<<'/'<<day<<endl;elsecout<<"日期设置有误,不能输出"<<endl;}intdate::getyear(){returnyear;}intdate::getmonth(){returnmonth;}intdate::getday(){returnday;}voiddate::addday(){day++;if(month==2)//判断是否是二月{boolleapyear;leapyear=((year%4==0&&year%100!=0)||(year%400==0));//定义闰年if(leapyear){if(day>29)//假设是闰年的二月当Day大于29时,Day=1,Mon增加一个月{day=1;month++;}}else{if(day>28)//假设不是闰年的二月当Day大于28时,Day=1,Mon增加一个月{day=1;month++;}}}elseif(month==1||month==3||month==5||month==7||month==8||month==10||month==12){if(day>31)//假设不是二月月大时,Day=1,Mon增加一个月{day=1;month++;}}else{if(day>30)//假设不是二月月小时,Day=1,Mon增加一个月{day=1;month++;}}if(month>12)//假设月大于12则Mon=1,Year增加一年{month=1;year++;}}voidmain(){dated1(1999,5,30);d1.show();d1.setdate();d1.show();cout<<"日期增加一天后为:";d1.addday();d1.show();}3-46#include<iostream>usingnamespacestd;classCRectangle{private:doubleX;doubleY; doublelength; doublewidth;public:CRectangle(doubles,doublee,doublel,doublew) { X=s; Y=e; length=l; width=w; } ~CRectangle(){} voidMove(double,double); voidSize(double,double); voidWhere(); voidArea();};voidCRectangle::Move(doublex,doubley){ cout<<"矩形按向量("<<x<<","<<y<<")移动"<<endl; cout<<"现在矩形左上角所在的位置:"<<endl; cout<<"("<<X+x<<","<<Y+y<<")"<<endl;}voidCRectangle::Size(doublecl,doublecw){ cout<<"要更改的长和宽:"<<cl<<cw; length=cl; width=cw; cout<<length*width<<endl;}voidCRectangle::Where(){ cout<<"现在矩形左上角所在的位置:"<<endl; cout<<"("<<X<<","<<Y<<")"<<endl;}voidCRectangle::Area(){doublearea;area=length*width;cout<<area;}intmain(){ CRectanglecr(2,3,5,4); cr.Where(); cr.Area(); cr.Move(1,2); cr.Size(2,3); return0;}3-47#include<iostream>usingnamespacestd;classCRectangle{private:doubleX;doubleY; doublelength; doublewidth;public:CRectangle(doubles,doublee,doublel,doublew) { X=s; Y=e; length=l; width=w; } ~CRectangle(){} voidMove(double,double); voidSize(double,double); voidWhere(); voidArea();};voidCRectangle::Move(doublex,doubley){ cout<<"矩形按向量("<<x<<","<<y<<")移动"<<endl; cout<<"现在矩形左上角所在的位置:"<<endl; cout<<"("<<X+x<<","<<Y+y<<")"<<endl;}voidCRectangle::Size(doublecl,doublecw){ cout<<"要更改的长和宽:"<<cl<<cw; length=cl; width=cw; cout<<length*width<<endl;}voidCRectangle::Where(){ cout<<"现在矩形左上角所在的位置:"<<endl; cout<<"("<<X<<","<<Y<<")"<<endl;}voidCRectangle::Area(){doublearea;area=length*width;cout<<area;}intmain(){ CRectanglecr(2,3,5,4); cr.Where(); cr.Area(); cr.Move(1,2); cr.Size(2,3); return0;}3-48#include<iostream.h>#include<string.h>classBank{private: charnumber[20]; intmoney;public: voidsetnumber(char*num,intmon) {strcpy(number,num); money=mon; } voidchanger(intmon) { money+=mon; } voidshow() { cout<<"当前的总人民币数为:"<<money<<endl; }};voidmain(){ charnum[20]; intmoney; intaddmoney; cout<<"输入用户名和存入的人民币:"<<endl;cin>>num>>money; Bankbank; bank.setnumber(num,money); bank.show(); cout<<"输入转账的人民币:"<<endl; cin>>addmoney;bank.changer(addmoney); bank.show();}3-49#include<iostream>usingnamespacestd;classProduct{private: charname[20]; doubleprice; intm_count;public: Product(){} ~Product(){} voidsetproduct(); voidsellproduct(); voidshow(); };voidProduct::setproduct(){ intn; cout<<"输入生产产品数量:"<<endl; cin>>n; m_count=n;}voidProduct::sellproduct(){ intn; cout<<"输入销售产品数量:"<<endl; cin>>n; m_count-=n;}voidProduct::show(){ cout<<"输出剩余产品数量:"<<endl; cout<<m_count<<endl;}intmain(){ Productp; p.setproduct(); p.sellproduct(); p.show(); return0;}3-50建设一个名为Student的类,该类有以下几个私有成员变量:学生姓名、学号、性别和年龄。还有以下两个成员函数:一个用于初始化学生姓名、学号、性别和年龄的构造函数,一个用于输出学生信息的函数。编写一个主函数,声明一个学生对象,然后调用成员函数在屏幕上输出学生信息。#include<iostream.h>#include<string.h>classStudent{public:Student(char*name,char*num,char*sex,intage);~Student();voidshow();private:char*Name;char*Num;char*Sex;intAge;};Student::Student(char*name,char*num,char*sex,intage){Name=newchar[strlen(name)+1];//注意字符串的赋值strcpy(Name,name);Num=newchar[strlen(num)+1];strcpy(Num,num);Sex=newchar[strlen(sex)+1];strcpy(Sex,sex);Age=age;}Student::~Student(){deleteName;deleteNum;deleteSex;}voidStudent::show(){cout<<"姓名:"<<Name<<endl;cout<<"学号:"<<Num<<endl;cout<<"性别:"<<Sex<<endl;cout<<"年龄:"<<Age<<endl;}voidmain(){Studentstudent("张三","0401011201","男",18);student.show();}3-51设计一个CPetrol类,包含以下几个私有成员:90号93号98号汽油加油量和单价,当天的总收入。类还包含以下几个成员函数:设置有关数据成员的构造函数,输入加油量并计算总收入的成员函数。利用类编写主函数:假设加油站某天909398号汽油单价分别为3.964.054.38计算并输出加油站一天的收入。#include<iostream>usingnamespacestd;classCPetrol{public:CPetrol();voidsetamount();doubletotal;private:doubleam_90;doubleam_93;doubleam_98;doubleprice_90;doubleprice_93;doubleprice_98;};CPetrol::CPetrol(){price_90=3.96;price_93=4.05;price_98=4.38;}voidCPetrol::setamount(){cout<<"inputthreeamounts!"<<endl;cin>>am_90>>am_93>>am_98;total=am_90*price_90+am_93+price_93+am_98+price_98;}voidmain(){CPetrolc;c.setamount();cout<<"Thetotalis"<<c.total<<endl;}3-52修改习题3-50中的类Student,添加一个静态成员变量,用于表示已创立对象的数量;添加两个静态成员函数,一个用于输出已创立对象的数量,一个用于输出一个学生的姓名和学号。#include<iostream.h>#include<string.h>classStudent{public:Student(char*name,char*num,char*sex,intage);~Student();staticvoidshow(Student&a);staticvoidshowstudentnum();private:char*Name;char*Num;char*Sex;intAge;staticintstudentnum;};intStudent::studentnum=0;Student::Student(char*name,char*num,char*sex,intage){studentnum++;Name=newchar[strlen(name)+1];strcpy(Name,name);Num=newchar[strlen(num)+1];strcpy(Num,num);Sex=newchar[strlen(sex)+1];strcpy(Sex,sex);Age=age;}Student::~Student(){deleteName;deleteSex;}voidStudent::showstudentnum(){cout<<"学生的数量是:"<<studentnum<<endl;}voidStudent::show(Student&a){cout<<"姓名:"<<a.Name<<endl;cout<<"学号:"<<a.Num<<endl;cout<<"性别:"<<a.Sex<<endl;cout<<"年龄:"<<a.Age<<endl;}voidmain(){Studentstudent1("张三","0401011201","男",18);Student::show(student1);//注意调用方式,静态成员可以通过类名调用Student::showstudentnum();//注意调用方式Studentstudent2("李四","0401011202","男",18);Student::show(student2);Student::showstudentnum();}3-53编写程序用静态成员的方法实现对班费的管理,要求定义一个类Student,除了声明一个存放班费的静态成员,还要分别定义一个上交班费的成员函数Contribute〔〕,花费班费的成员函数Spend〔〕和显示班费的静态成员函数Display〔〕//student.cpp#include<iostream>usingnamespacestd;classStudent{private:staticdoublefee;//fee--班费,静态成员数据public:Student(){}//默认构造函数,析构函数~Student(){}voidContribute(doublen);//n--上缴的班费数额voidSpend(doublen);//n--花费班费数量staticvoidDisplay();//静态成员函数};doubleStudent::fee=0;//类声明外面对静态数据成员初始化//类方法voidStudent::Contribute(doublen){fee=fee+n;}voidStudent::Spend(doublen){if(fee<n)cout<<"班费不够,请求失败!\n";elsefee=fee-n;}voidStudent::Display(){cout<<"现有班费:"<<fee<<endl;}intmain(){Studentstu;stu.Display();stu.Contribute(103.4);//交人民币stu.Display();stu.Spend(42.3);//花人民币stu.Display();return0;}放在了一个文件里了,上面是类声明,下面是测试小程序,运行过了,没问题3-54定义一个类A,该类除了有两个数据成员x,y外,还有一个对象备份函数copy。copy函数的功能说明如下:对于类A的对象a1和a2,函数调用a1.copy〔a2〕表示将对象a2赋值给对象a1.〔提示利用this指针防止一个对象对自己赋值〕#include<iostream>usingnamespacestd;classTest{private:char*a,*b;public:Test(){a=newchar[100];b=newchar[100];}~Test(){delete[]a;delete[]b;}Test©(Test&B){if(this==&B)return*this;intlen=strlen(B.a);delete[]a;a=newchar[len+1];strcpy(a,B.a);len=strlen(B.b);delete[]b;b=newchar[len+1];strcpy(b,B.b);return*this;}voidmytest(char*str1,char*str2){strcpy(a,str1);strcpy(b,str2);}voidmyprint(){cout<<a<<""<<b<<endl;}}A,B;intmain(){B.mytest("thisis","B");B.myprint();A.copy(B);A.myprint();return0;}3-55将习题3-45中类Date的“日期加一天〞成员函数改为友员函数。#include<iostream.h>classdate{intyear;intmonth;intday;boolflag;public:date(){year=0;month=0;day=0;}date(intyr,intmo,intda);voidsetdate();intgetyear();intgetmonth();intgetday();friendvoidaddday(date&d);voidshow();};date::date(intyr,intmo,intda){flag=false;if(mo>=1&&mo<=12&&da>=1&&da<=31){year=yr;month=mo;day=da;}else{flag=true;}}voiddate::setdate(){cout<<"请输入年分"<<endl;cin>>year;cout<<"请输入月份(1-12)"<<endl;cin>>month;while(month<1||month>12){cout<<"输入有误,请重新输入月份(1-12)"<<endl;cin>>month;}cout<<"请输入日期(1-31)"<<endl;cin>>day;while(day<1||day>31){cout<<"输入有误,请重新输入日期(1-31)"<<endl;cin>>day;}flag=false;}voiddate::show(){if(!flag)cout<<year<<'/'<<month<<'/'<<day<<endl;elsecout<<"日期设置有误,不能输出"<<endl;}intdate::getyear(){returnyear;}intdate::getmonth(){returnmonth;}intdate::getday(){returnday;}voidaddday(date&d){d.day++;if(d.month==2)//判断是否是二月{boolleapyear;leapyear=((d.year%4==0&&d.year%100!=0)||(d.year%400==0));//定义闰年if(leapyear){if(d.day>29)//假设是闰年的二月当Day大于29时,Day=1,Mon增加一个月{d.day=1;d.month++;}}else{if(d.day>28)//假设不是闰年的二月当Day大于28时,Day=1,Mon增加一个月{d.day=1;d.month++;}}}elseif(d.month==1||d.month==3||d.month==5||d.month==7||d.month==8||d.month==10||d.month==12){if(d.day>31)//假设不是二月月大时,Day=1,Mon增加一个月{d.day=1;d.month++;}}else{if(d.day>30)//假设不是二月月小时,Day=1,Mon增加一个月{d.day=1;d.month++;}}if(d.month>12)//假设月大于12则Mon=1,Year增加一年{d.month=1;d.year++;}}voidmain(){dated1(1999,5,30);d1.show();d1.setdate();d1.show();cout<<"日期增加一天后为:";addday(d1);d1.show();}3-56将习题3-50中类Student的学生信息输出函数改为友员函数。#include<iostream.h>#include<string.h>classStudent{public:Student(char*name,char*num,char*sex,intage);~Student();friendvoidshow(Student&);private:char*Name;char*Num;char*Sex;intAge;};Student::Student(char*name,char*num,char*sex,intage){Name=newchar[strlen(name)+1];strcpy(Name,name);Num=newchar[strlen(num)+1];strcpy(Num,num);Sex=newchar[strlen(sex)+1];strcpy(Sex,sex);Age=age;//注意字符串的赋值}Student::~Student(){deleteName;deleteSex;}voidshow(Student&stu){cout<<"姓名:"<<stu.Name<<endl;cout<<"学号:"<<stu.Num<<endl;cout<<"性别:"<<stu.Sex<<endl;cout<<"年龄:"<<stu.Age<<endl;}voidmain(){Studentstudent("张三","0401011201","男",18);show(student);}3-57设计一个直线类Line〔设直线方程为ax+bx+c=0〕,其中,包含三个数据成员a、b、c,一个显示数据成员的disp成员函数和一个求两直线交点的友元函数setpoint,并在main()中给出对象或数据进展测试。请填空完成程序并上机运行验证。#include<iostream.h>classLine{friendvoidsetpoint(Line&A,Line&B);private: doublea; doubleb; doublec;public: Line(doublea1,doubleb1,doublec1) { a=a1; b=b1; c=c1; } ~Line(){};};voidsetpoint(Line&A,Line&B){ doublex; if(A.a/B.a!=A.b/B.b) { x=-100; while(1) { if(((-A.c-A.a*x)/A.b-(-B.c-B.a*x)/B.b)<

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论