C编程题库.docx
- 文档编号:12277954
- 上传时间:2023-06-05
- 格式:DOCX
- 页数:61
- 大小:42.04KB
C编程题库.docx
《C编程题库.docx》由会员分享,可在线阅读,更多相关《C编程题库.docx(61页珍藏版)》请在冰点文库上搜索。
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>>>>>>;
intbox_v,box_a;
,;
box_v=,,;
cout<<"Thisbox'svolume="< box_a=,,; 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 y;} private: numtypex,y; }; intmain() {Compare cout<<()<<"istheMaximumoftwointedernumbers."< cout<<()<<"istheMinimumoftwointedernumbers."< Compare cout<<()<<"istheMaximumoftwofloatnumbers."< cout<<()<<"istheMinimumoftwofloatnumbers."< Compare cout<<()<<"istheMaximumoftwocharacters."< cout<<()<<"istheMinimumoftwocharacters."< 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<<"("<<()<<","<<()<<"i)"< : endl; .="< return0; } 1.编写一个程序,其中main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值),该程序按照下面的格式要求用户输入摄氏温度值,并显示结果: PleaseenteraCelsiusvalue: 20 20degreesCelsiusis68degreesFahrenheit. 转换公式如下: 华氏温度=x摄氏温度+ #include usingnamespacestd; floatfun(float); intmain() { floatc; cout<<"PleaseenteraCelsiusvalue: "; cin>>c; cout<<"20degreesCelsiusis"< return0; } floatfun(floatc) { floatf; f=*c+; returnf; } 2.把有序的两个数组a和b合并,要求合并后数组依然有序。 #include #defineM4 #defineN5 usingnamespacestd; intmain() { intc[M+N],i=0,j=0,k=0; inta[M]={2,4,6,8}; intb[N]={1,3,5,7,9}; while(i if(a[i] elsec[k++]=b[j++]; while(i c[k++]=a[i++]; while(j c[k++]=b[j++]; for(intz=0;z cout< return0; } 3.用指针方法完成: 对一个4×5矩阵(int型)分别进行横向和纵向汇总(如图)。 72946 56183 98639 51972 #include usingnamespacestd; intmain() { ints[4][5]={{7,2,9,4,6},{5,6,1,8,3},{9,8,6,3,9},{5,1,9,7,2}}; int*p; p=&s[0][0]; for(inti=0;i<4*5;i++) { cout< if((i+1)%5==0)cout< } return0; 14声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。 #include usingnamespacestd; template classCompare {public: Compare(numtypea,numtypeb) {x=a;y=b;} numtypemax() {return(x>y)x: y;} numtypemin() {return(x y;} private: numtypex,y; }; intmain() {Compare cout<<()<<"istheMaximumoftwointedernumbers."< cout<<()<<"istheMinimumoftwointedernumbers."< Compare cout<<()<<"istheMaximumoftwofloatnumbers."< cout<<()<<"istheMinimumoftwofloatnumbers."< Compare cout<<()<<"istheMaximumoftwocharacters."< cout<<()<<"istheMinimumoftwocharacters."< return0; } 15建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。 #include usingnamespacestd; classStudent {public: Student(intn,floats): num(n),score(s){} voiddisplay(); private: intnum; floatscore; }; voidStudent: : display() {cout< intmain() {Studentstud[5]={ Student(101,,Student(102,,Student(103,, Student(104,,Student(105,}; Student*p=stud; for(inti=0;i<=2;p=p+2,i++) p->display(); return0; } 16建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。 #include usingnamespacestd; classStudent {public: Student(intn,floats): num(n),score(s){} intnum; floatscore; }; voidmain() {Studentstud[5]={ Student(101,,Student(102,,Student(103,, Student(104,,Student(105,}; voidmax(Student*); Student*p=&stud[0]; max(p); } voidmax(Student*arr) {floatmax_score=arr[0].score; intk=0; for(inti=1;i<5;i++) if(arr[i].score>max_score){max_score=arr[i].score;k=i;} cout< } 17将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为complex类的友元函数。 #include<> classComplex {public: Complex(){real=0;imag=0;} Complex(doubler){real=r;imag=0;} Complex(doubler,doublei){real=r;imag=i;} friendComplexoperator+(Complex&c1,Complex&c2); voiddisplay(); private: doublereal; doubleimag; }; Complexoperator+(Complex&c1,Complex&c2) {returnComplex+,+;} voidComplex: : display() {cout<<"("< intmain() {Complexc1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c1=";(); cout<<"c2=";(); cout<<"c1+c2=";(); return0; } 18定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。 参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。 例如: c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。 编程序,分别求两个复数之和、整数和复数之和。 #include<> classComplex {public: Complex(){real=0;imag=0;} Complex(doubler,doublei){real=r;imag=i;} Complexoperator+(Complex&c2); Complexoperator+(int&i); friendComplexoperator+(int&,Complex&); voiddisplay(); private: doublereal; doubleimag; }; ComplexComplex: : operator+(Complex&c) {returnComplex(real+,imag+;} ComplexComplex: : operator+(int&i) {returnComplex(real+i,imag);} voidComplex: : display() {cout<<"("< Complexoperator+(int&i,Complex&c) {returnComplex(i+,;} intmain() {Complexc1(3,4),c2(5,-10),c3; inti=5; c3=c1+c2; cout<<"c1+c2="; (); c3=i+c1; cout<<"i+c1="; (); c3=c1+i; cout<<"c1+i="; (); return0; } 19编写一个程序读入多个string对象,把它们连接起来存放到一个更大的string对象中。 并输出连接后的string对象 #include #include intmain() { usingnamespacestd; stringstr1,str2,str3; cout<<"PleaseEnter2strings: "; cin>>str1>>str2; str3=str1+str2; cout< return0; } 20编写一个程序读入两个string对象,测试它们是否相等,若不相等,则指出两个中哪个较大。 #include #include intmain() { usingnamespacestd; stringstr1,str2; cout<<"PleaseEnter2string: "; cin>>str1>>str2; if(str1>str2) cout<<"str1isbigger! "< else cout<<"str2isbigger! "< return0; } 21.设计一个Time类,包括三个私有数据成员: hour,minute,sec,用构造函数初始化,内设公用函数display(Date&d),设计一个Date类,包括三个私有数据成员: month,day,year,也用构适函数初始化;分别定义两个带参数的对象t1(12,30,55),d1(3,25,2010),通过友员成员函数的应用,输出d1和t1的值。 #include usingnamespacestd; classDate; classTime {public: Time(int,int,int); voiddisplay(constDate&); private: inthour; intminute; intsec; }; classDate {public: Date(int,int,int); friendvoidTime: : display(constDate&); private: intmonth; intday; intyear; }; Time: : Time(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编程 题库