C编程题库.docx
- 文档编号:26224149
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:92
- 大小:41.50KB
C编程题库.docx
《C编程题库.docx》由会员分享,可在线阅读,更多相关《C编程题库.docx(92页珍藏版)》请在冰豆网上搜索。
C编程题库
31.定义盒子Box类,要求具有以下成员:
长、宽、高分别为x,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的表面积。
#include
#include
usingnamespacestd;
classBox{
public:
intweight;
intlength;
inthight;
voidbox_shape(intw,intl,inth);
intbox_volume(intw,intl,inth);
intbox_area(intw,intl,inth);
};
intmain()
{
Boxmybox;
cout<<"PleaseEnterweightandlengthandhight:
";
cin>>mybox.weight>>mybox.length>>mybox.hight;
intbox_v,box_a;
mybox.box_shape(mybox.weight,mybox.length,mybox.hight);
box_v=mybox.box_volume(mybox.weight,mybox.length,mybox.hight);
cout<<"Thisbox'svolume="< box_a=mybox.box_area(mybox.weight,mybox.length,mybox.hight); cout<<"Thisbox'sarea="< } voidBox: : box_shape(intw,intl,inth) { if(w==l&&l==h) cout<<"ThisisaCube! "< else cout<<"Thisisacuboid! "< } intBox: : box_volume(intw,intl,inth) { returnw*l*h; } intBox: : box_area(intw,intl,inth) { return2*w*l+2*l*h+2*w*h; } 32.有两个长方柱,其长、宽、高分别为: (1)30,20,10; (2)12,10,20。 分别求他们的体积。 编一个基于对象的程序,在类中用带参数的构造函数。 #include classBox{ private: intlength; intweight; inthight; public: Box(int,int,int); intvolume(); }; intmain() { usingnamespacestd; Boxmybox1(30,20,10); cout<<"ThefirstBox'svolume="< Boxmybox2(12,10,30); cout<<"ThesecondBox'svolume="< return0; } Box: : Box(intl,intw,inth) { length=l; weight=w; hight=h; } intBox: : volume() { return(hight*weight*length); } 33.有两个长方柱,其长、宽、高分别为: (1)12,20,25; (2)10,30,20。 分别求他们的体积。 编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参数。 #include classBox{ private: intlength; intwight; intheight; public: Box(); Box(int,int,int); intvolume(); }; intmain() { usingnamespacestd; Boxmybox1; cout<<"Thefirstbox'svolume="< Boxmybox2(10,30,20); cout<<"Thesecondbox'svolume="< return0; } Box: : Box() { length=12; wight=20; height=25; } Box: : Box(intl,intw,inth) { length=l; wight=w; height=h; } intBox: : volume() { returnlength*wight*height; } 34.声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。 #include usingnamespacestd; template classCompare {public: Compare(numtypea,numtypeb){x=a;y=b;} numtypemax(){return(x>y)? x: y;} numtypemin(){return(x x: y;} private: numtypex,y; }; intmain() {Compare cout< cout< Compare cout< cout< Compare cout< cout< return0; } 35.建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。 初值自拟。 #include usingnamespacestd; classStudent { public: Student(intn,ints): num(n),score(s){} voiddisplay() {cout< private: intnum; intscore; }; intmain() { Studentstud[5]={Student(01,70),Student(02,71),Student(03,72),Student(04,73),Student(05,74)}; Student*p=stud; for(inti=0;i<=2;p=p+1,i++) p->display(); return0; } 36.建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。 初值自拟。 #include usingnamespacestd; classStudent { public: Student(intn,ints): num(n),score(s){} intnum; intscore; }; intmain() { Studentstud[5]={Student(01,70),Student(02,71),Student(03,72),Student(04,73),Student(05,74)}; voidmax(Student*); Student*p=&stud[0]; max(p); return0; } voidmax(Student*arr) { floatmax_score=arr[0].score; for(inti=0;i<5;i++) if(max_score {max_score=arr[i].score;} cout< } 38.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。 将运算符函数重载为非成员、非友元的普通函数。 编写程序,求两个复数之和。 初值自拟。 #include classComplex{ private: doublereal; doubleimage; public: Complex(); Complex(double,double); doubleget_real(); doubleget_image(); }; Complexoperator+(Complex&,Complex&); intmain() { Complexs1(3,4); Complexs2(5,-10); Complexc3; c3=s1+s2; std: : cout<<"s1+s2="; std: : cout<<"("< : endl; //Complexs1(3,4); //std: : cout< : endl; return0; } Complex: : Complex(doubler,doublei) { real=r; image=i; } Complex: : Complex() { real=0; image=0; } doubleComplex: : get_real() { returnreal; } doubleComplex: : get_image() { returnimage; } Complexoperator+(Complex&s1,Complex&s2) { Complexc(s1.get_real()+s2.get_real(),s1.get_image()+s2.get_image()); returnc; } 39.定义一个复数类Complex,重载运算符“+”,“—”,使之能用于复数的加,减运算,运算符重载函数作为Complex类的成员函数。 编程序,分别求出两个复数之和,差。 初值自拟。 #include classComplex{ private: doublereal; doubleimage; public: Complex(); Complex(double,double); Complexoperator+(Complex&); Complexoperator-(Complex&); voiddisplay(); }; intmain() { Complexs1(3,4),s2(5,6),c; c=s1+s2; c.display(); c=s1-s2; c.display(); return0; } Complex: : Complex() { real=0; image=0; } Complex: : Complex(doubler,doublei) { real=r; image=i; } ComplexComplex: : operator+(Complex&s1) { Complexc; c.real=s1.real+real; c.image=s1.image+image; returnc; } ComplexComplex: : operator-(Complex&s1) { Complexc; c.real=real-s1.real; c.image=image-s1.image; returnc; } voidComplex: : display() { usingnamespacestd; cout< } 40.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。 参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。 例如: c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。 编程序,分别求两个复数之和、整数和复数之和。 初值自拟。 #include classComplex { private: doublereal; doubleimag; public: Complex(){real=0;imag=0;} Complex(doubler,doublei){real=r;imag=i;} Complexoperator+(Complex&c2); Complexoperator+(int&i); friendComplexoperator+(int&,Complex&); voiddisplay(); }; ComplexComplex: : operator+(Complex&c) { returnComplex(real+c.real,imag+c.imag); } ComplexComplex: : operator+(int&i) { returnComplex(real+i,imag); } voidComplex: : display() { usingnamespacestd; cout<<"("< } Complexoperator+(int&i,Complex&c) { returnComplex(i+c.real,c.imag); } intmain() { usingnamespacestd; Complexc1(3,4),c2(5,-10),c3; inti=5; c3=c1+c2; cout<<"c1+c2="; c3.display(); c3=i+c1; cout<<"i+c1="; c3.display(); c3=c1+i; cout<<"c1+i="; c3.display(); return0; } 41.有两个矩阵a和b,均为2行3列。 求两个矩阵之和。 重载运算符“+”,使之能用于矩阵相加。 如c=a+b。 初值自拟。 #include usingnamespacestd; classMatrix//定义Matrix类 {public: Matrix();//默认构造函数 friendMatrixoperator+(Matrix&,Matrix&);//重载运算符“+” voidinput();//输入数据函数 voiddisplay();//输出数据函数 private: intmat[2][3]; }; Matrix: : Matrix()//定义构造函数 {for(inti=0;i<2;i++) for(intj=0;j<3;j++) mat[i][j]=0; } Matrixoperator+(Matrix&a,Matrix&b)//定义重载运算符“+”函数 {Matrixc; for(inti=0;i<2;i++) for(intj=0;j<3;j++) {c.mat[i][j]=a.mat[i][j]+b.mat[i][j];} returnc; } voidMatrix: : input()//定义输入数据函数 {cout<<"inputvalueofmatrix: "< for(inti=0;i<2;i++) for(intj=0;j<3;j++) cin>>mat[i][j]; } voidMatrix: : display()//定义输出数据函数 {for(inti=0;i<2;i++) {for(intj=0;j<3;j++) {cout< cout< } intmain() {Matrixa,b,c; a.input(); b.input(); cout< "< a.display(); cout< "< b.display(); c=a+b;//用重载运算符“+”实现两个矩阵相加 cout< "< c.display(); return0; } 42.将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为Complex类的友元函数。 初值自拟。 #include classCompare { private: intreal; intimag; public: Compare(); Compare(int,int); friendCompareoperator+(Compare&c1,Compare&c2); voiddisplay(); }; intmain() { Comparec1(3,4),c2(2,5),c3; c3=c1+c2; c3.display(); return0; } Compare: : Compare() { real=0; imag=0; } Compare: : Compare(intr,inti) { real=r; imag=i; } voidCompare: : display() { usingnamespacestd; cout< } Compareoperator+(Compare&c1,Compare&c2) { Comparec(c1.real+c2.real,c1.imag+c2.imag); returnc; } 43.定义一个字符串类String,用来存放不定长的字符串,重载运算符“==”,,用于两个字符串的等于比较运算。 初值自拟。 44.定义一个字符串类String,用来存放不定长的字符串,重载运算符"<",用于两个字符串的小于的比较运算。 初值自拟。 45.定义一个字符串类String,用来存放不定长的字符串,重载运算符">",用于两个字符串的大于的比较运算。 初值自拟。 #include #include usingnamespacestd; classString { public: String(){p=NULL;} String(char*str); friendbooloperator>(String&string1,String&string2); friendbooloperator<(String&string1,String&string2); friendbooloperator==(String&string1,String&string2); voiddisplay(); private: char*p; }; String: : String(char*str) {p=str;} voidString: : display() {cout< booloperator>(String&string1,String&string2) { if(strcmp(string1.p,string2.p)>0) returntrue; else returnfalse; } booloperator<(String&string1,String&string2) { if(strcmp(string1.p,string2.p)<0) returntrue; else returnfalse; } booloperator==(String&string1,String&string2) { if(strcmp(string1.p,string2.p)==0) returntrue; else returnfalse; } voidcompare(String&string1,String&string2) { if(operator>(string1,string2)==1) { string1.display();cout<<">";string2.display();} elseif(operato
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编程 题库