《c++题库编程题(19页).doc》由会员分享,可在线阅读,更多相关《c++题库编程题(19页).doc(19页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-c+题库编程题-第 19 页/2-5#include#include#includeusing namespace std;class Triangle int a,b,c;public: double Area() double s=(a+b+c)/2; return sqrt(s*(s-a)*(s-b)*(s-c); ; int Perimeter() return a+b+c; void Input() cinabc; ; void main() Triangle t1; t1.Input(); coutt1.Area() t1.Perimeter();/2-3#includeinli
2、ne int max(int a,int b)if(ab)return a;else return b;inline int max(int a,int b,int c)return max(a,max(b,c);void main()int x=21,y=15,z=22,d;d=max(x,max(y,z);coutd;/2-4#includeint overload(int n);char overload(char n);void main()int n;char s;coutn;coutn=overload(n)endl;cout输入as;couta=;coutoverload(s)e
3、ndl;int overload(int n)return n;char overload(char n)coutbendl;return 0;/2-2#includeiostreamusing namespace std;inline char trans(char ch);int main()char ch;while(ch=getchar()!=n)couttrans(ch);cout=a&ch=z)return ch-32;else return ch+32;/3-1#includeusing std:cout;using std:endl;int Min(int a,int b)re
4、turn (ab?a:b);int Min(int a,int b,int c)return (Min(a,b)c?Min(a,b):c);int Min(int a,int b,int c,int d)return (Min(a,b,c)d?Min(a,b,c):d);void main(void)coutMIN(1,2)=Min(1,2)endl;coutMIN(3,4,5)=Min(3,4,5)endl;coutMIN(4,5,6,7)=Min(4,5,6,7)endl;/3-2#includeusing namespace std;void Area(float r)coutthe c
5、ircles area is: 3.14*r*rendl;void Area(float a,float b)coutthe rectangles(squares) area is: a*bendl;void Area(float a,float b,float h)coutthe trapzoids area is: (a+b)*h/2endl;void main()Area(5);Area(3,3);Area(3,5,4);/3-3#includeusing namespace std;void Sort(int a,int n)bool flag;for(int i=0;in-1;i+)
6、flag=false;for(int j=1;jaj)int temp=aj-1;aj-1=aj;aj=temp;flag=true;if(!flag)break;for(int m=0;mn;m+)coutam ;coutendl;void Sort(float b,int n)bool flag;for(int i=0;in-1;i+)flag=false;for(int j=1;jbj)float temp=bj-1;bj-1=bj;bj=temp;flag=true;if(!flag)break;for(int m=0;mn;m+)coutbm ;coutendl;void main(
7、)int a10=3,5,1,2,9,0,8,6,4,7;float b10=0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0;Sort(a,10);Sort(b,10);/3-4#includeusing namespace std;class Rectanglefloat a,b;public:Rectangle()coutinput the two side of the rectangle:ab;Rectangle(float x,float y):a(x),b(y)float Cir()return (2*(a+b);float Area()return
8、 (a*b);void main()Rectangle Re;coutthe rectangles circumference is:Re.Cir()endlthe rectangles area is:Re.Area()endl;/3-5#includeusing namespace std;class Circlefloat r;public:Circle()coutinput the radius of the circle:r;Circle(float x):r(x)float Cir()return 2*3.14*r;float Area()return 3.14*r*r;void
9、main()Circle C(5);cout the Circles circumference is:C.Cir()endlthe Circles Area is:C.Area()endl;/4-1class studentint no;char name20;public:student();int getno()const; char*getname();#includestudent:student()no=1;strcpy(name,wang);int student:getno ()constreturn no; char*student:getname()return name;
10、int main()student s1;s1.getno();s1.getname();const student s2;s2.getno();return 0;/4-3#includeclass objectfloat a;float weight;public:object(float w);object()coutdestruct weightendl;class box:public objectfloat b;float height,widt;public:box(float w,float h,float wi):object(w),height(h),widt(wi)cout
11、construct heightendl;coutconstruct widtendl;box()coutdestruct heightendl;coutdestruct widtendl;object:object(float w)weight=w;coutconstruct weightendl;int main()box b(1.1,2.2,3.3);return 0;/4-4#includeiostreamusing namespace std;class baseclasspublic:baseclass()couta.cendl;virtual baseclass()couta.d
12、endl;class derivedclass:public baseclasspublic:derivedclass()coutb.cendl;derivedclass()coutb.dendl;int main()derivedclass obj;baseclass *p;p=&obj;return 0;/4-5#includeiostream#includeusing namespace std;class Doumentchar *name;public: Doument(char *na)name=new char strlen (na)+1;strcpy(name,na); vir
13、tual void print() const cout姓名:nameendl;class Book:public Doumentint pagecount;char *name;public:Book(int p,char *na):Doument(na)p=pagecount; name=new char strlen (na)+1; strcpy(name,na);void print() const cout页码:pagecount姓名:nameendl;void disp(Doument& i)i.print();int main()Book b(23,小明);disp(b);ret
14、urn 0;/5-1#includeiostreamusing namespace std;class pointprivate:int x;int y;int z;static int num;public:point(int a,int b,int c);int print();int point:num=0;int main()point p1(1,2,3);p1.print();point p2(5,3,2);p1.print();point p3(3,6,4);p1.print();return 0; point:point(int a,int b,int c)x=a;y=b;z=c
15、;num+; int point:print()cout第num个点x,y,zendl;return 0;/5-2#include#includeusing namespace std;class Stuchar name15;int age;float score;public:static int count;static float sum;Stu(char *p,int a,float s):age(a),score(s)strcpy(name,p);count+;sum+=score;Stu()coutinput the students name,age,score:nameage
16、score;count+;sum+=score;int Stu:count=0;float Stu:sum=0;void main()Stu p(子机,19,98),q;coutthe number of students is: Stu:countendlthe total score is: Stu:sumendl;/5-3#includeusing namespace std;class Pointfloat x,y,z;public:Point(float a=0,float b=0,float c=0):x(a),y(b),z(c)Point operator +();Point o
17、perator -();Point operator +(int);Point operator -(int);void show()cout( x , y , z ) = ( x , y , z )endl;Point Point:operator +()return Point(+x,+y,+z);Point Point:operator -()return Point(-x,-y,-z);Point Point:operator +(int)Point temp;temp.x=x+;temp.y=y+;temp.z=z+;return temp;Point Point:operator
18、-(int)Point temp;temp.x=x-;temp.y=y-;temp.z=z-;return temp;void main()Point a(1,1,1),b(2,2,2),c,d;couta :;a.show();coutb :;b.show();coutendl;c=a+;coutc = a+n;coutc :;c.show();coutendl;d=+b;coutd = +bn;coutd :;d.show();coutNew a and b :n;a.show();b.show();coutendl;c=a-;coutc = a-n;coutc :;c.show();co
19、utendl;d=-b;coutd = -bn;coutd :;d.show();coutNew a and b :n;a.show();b.show();/5-4#includeusing namespace std;class Complexfloat real,image;public:Complex(float a,float b):real(a),image(b)Complex()friend bool operator =(Complex &x,Complex &y)return (x.real=y.real&x.image=y.image);void main()Complex
20、Cp1(1,2),Cp2(3,4),Cp3(1,2);cout(Cp1=Cp2)endl(Cp1=Cp3)endl;/6-1#includeusing namespace std;class Complexfloat real,image;public:Complex(float x,float y):real(x),image(y)Complex()Complex operator +(Complex &a)return Complex(real+a.real,image+a.image);Complex operator -(Complex &b)return Complex(real-b
21、.real,image-b.image);friend ostream & operator (ostream &output,Complex &c)output(c.real,c.image);return output;void main()Complex Cp1(1,-2),Cp2(2,5),Cp3,Cp4;Cp3=Cp1+Cp2;Cp4=Cp1-Cp2;coutCp1+Cp2=Cp3endlCp1-Cp2=Cp4endl;/6-2#includeusing namespace std;class computerfloat x,y;public:computer(float a=0,f
22、loat b=0):x(a),y(b)computer operator +();computer operator -();computer operator +(int);computer operator -(int);void show()cout( x , y ) = ( x , y )endl;computer computer:operator +()return computer(+x,+y);computer computer:operator -()return computer(-x,-y);computer computer:operator +(int)compute
23、r temp;temp.x=x+;temp.y=y+;return temp;computer computer:operator -(int)computer temp;temp.x=x-;temp.y=y-;return temp;void main()computer a(1,1),b(2,2),c,d;couta :;a.show();coutb :;b.show();coutendl;c=a+;coutc = a+n;coutc :;c.show();coutendl;d=+b;coutd = +bn;coutd :;d.show();coutNew a and b :n;a.sho
24、w();b.show();coutendl;c=a-;coutc = a-n;coutc :;c.show();coutendl;d=-b;coutd = -bn;coutd :;d.show();coutNew a and b :n;a.show();b.show();/6-3#include#includeclass stringchar *xstring;public: string(char *str); string(); string &operator=(const string &);void print();string:string(char *str)xstring=ne
25、w char strlen(str)+1;strcpy(xstring,str);string:string()delete xstring;void string:print()coutxstringendl;int main()string s1(hello);string s2(yangchenhuan);s2.print();s2=s1;cout修改后:;s2.print();return 0;string & string :operator=(const string &s)if(this=&s)return *this;delete xstring;xstring=new cha
26、r strlen(s.xstring)+1;strcpy(xstring,s.xstring);return *this;/6-4#include #include using namespace std;class String public:String() p=NULL;String(char *str);friend bool operator=(String &string1,String &string2);void display();private:char *p; ;String:String(char *str) p=str; void String:display() c
27、outp; /输出P所指向的字符串bool operator=(String &string1,String &string2) if(strcmp(string1.p,string2.p)=0) return true; else return false; void compare(String &string1,String &string2) if(operator=(string1,string2)=0) string1.display(); cout=; string2.display(); else couterror!endl;int main() String string1
28、(Hello),string2(china); compare(string1,string2); return 0; /6-5#include#includeusing namespace std;class Stringchar *p;public:String()p=NULL;String(const char *s)p=new char(strlen(s)+1);strcpy(p,s);char * operator +(const char *s)char *temp=new char(strlen(p)+strlen(s)+1);strcpy(temp,p);strcat(temp
29、,s);return temp;friend bool operator =(const String &a,const String &b)return (!strcmp(a.p,b.p);friend ostream &operator (ostream &output,const String &d)outputd.p;return output;void main()String Str1(Malagebi!),Str2(Ready,Go!),Str3;Str3=Str1+nishi;coutStr3endl(Str1=Str2)endl;/7-1#includeusing names
30、pace std;class Carint wnum;float weight;public:Car(int a,float b):wnum(a),weight(b)void Cshow()cout轮子个数: wnum 个endl车重: weight Tendl;class SmallCar:private Carint s_cnum;public:SmallCar(int a,float b,int c):Car(a,b),s_cnum(c)void Sshow()Cshow();cout载客量: s_cnum 人次endl;class BigCar:private Carint b_cnu
31、m;float b_cweight;public:BigCar(int a,float b,int c,float d):Car(a,b),b_cnum(c),b_cweight(d)void Bshow()Cshow();cout载客量: b_cnum 人次endl载重量 b_cweight Tendl;int main()Car car(4,8);SmallCar smallcar(8,16,8);BigCar bigcar(16,32,16,32);cout汽车: endl;car.Cshow();coutn小车: endl;smallcar.Sshow();coutn卡车: endl;
32、bigcar.Bshow();return 0;/7-2#include#includeusing namespace std;class Shapefloat x,y;public:Shape(float a,float b):x(a),y(b)void show()cout左上角坐标 (x,y)= (x,y)n;virtual float GetArea()=0;class Rec:virtual public Shapefloat high,width;public:Rec(float a,float b,float c,float d):Shape(a,b),high(c),width
33、(d)void show()cout矩形 : n;Shape:show();cout矩形宽、高: width highendl;float GetArea()show();return high*width;class Ell:virtual public Shapefloat xx,yy;public:Ell(float a,float b,float c,float d):Shape(a,b),xx(c),yy(d)void show()cout椭圆 : n;Shape:show();cout椭圆x轴,y轴: xx yyendl;float GetArea()show();return 4
34、*atan(1)*xx*yy;int main()Rec rec(5,5,5,5);Ell ell(8,8,12,10);Shape *s;s=&rec;cout矩形的面积: GetArea()endlendl;s=ℓcout椭圆的面积: GetArea()endlendl;return 0;/7-3#include /#include using namespace std;class mammal int m;public:mammal(int i=0):m(i)coutmammal is constructendl;mammal() coutmammal is destructe
35、ndl;class cat:public mammalint c;public:cat(int j=0):c(j)cout cat is constructendl;cat() coutcat is destructendl;class dog:public mammalint d;public:dog(int k=0):d(k)cout dog is constructendl;dog() coutdog is destructendl;int main()cat c;dog d; return 0;/7-4p39.7/8-1#includeusing namespace std;templ
36、ate double sum(T1 a,T2 b)return (a+b);void main()coutsum(1,3)endl;coutsum(1.2,3)endl;coutsum(1,3.1)endl;coutsum(1.2,3.34)endl;/8-2P课本179/8-3#includeusing namespace std;const int M=100;template class ArrayT arrayM;int len;public:Array();void sort();void search()coutx;for(int i=0;ilen;i+)if(x=arrayi)coutnthe position of the num x is i+1endl;break;if(i=len)coutnthe num x is not exist ! n;T sum()float s=0;for(int i=0;ilen;i+)s+=arrayi;return s;void show()for(int i=0;ilen;i+)coutarrayi ;coutendl;template Array:Array()coutlen;coutinput the element of the array :n;for(int i=0;ilen