C_编程题库(一)

上传人:微*** 文档编号:171401530 上传时间:2022-11-26 格式:DOCX 页数:60 大小:132.17KB
收藏 版权申诉 举报 下载
C_编程题库(一)_第1页
第1页 / 共60页
C_编程题库(一)_第2页
第2页 / 共60页
C_编程题库(一)_第3页
第3页 / 共60页
资源描述:

《C_编程题库(一)》由会员分享,可在线阅读,更多相关《C_编程题库(一)(60页珍藏版)》请在装配图网上搜索。

1、31.定义盒子Box类,要求具有以下成员:长、宽、高分别为x,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的表面积。ttinclude #include using namespace std;class Boxpublic:int weight;int length;int hight;void box_shape(int w, int 1, int h);int box_volume(int w, int 1, int h);int box_area(int w, int 1, int h););int main()(Box mybox;cout z Please Enter wei

2、ght and length and hight:,z;cin mybox. weight mybox. length mybox. hight;int box_v, box_a;mybox. box_shape(mybox. weight, mybox.length, mybox. hight);box_v = mybox. box_volume(mybox. weight, mybox. length, mybox. hight);cout z,rFhis box, s volume =“ box_v endl;box_a = mybox. box_area(mybox. weight,

3、mybox. length, mybox. hight);cout This box s area = box_a endl;void Box:box_shape(int w, int 1, int h)if(w =1&1= h)cout This is a Cube! endl;elsecout This is a cuboid! endl;)int Box:box_volume(int w, int 1, int h)(return w * J.* h;)int Box:box_area(int w, int 1, int h)return 2*w*l+2*l*h+2*w*h;32.有两个

4、长方柱,其长、宽、髙分别为:(1)30,20,10;(2)12,10,20。分别求他们的体积。编一个基于对象的程序,在类中用带参数的构造函数。#include class Boxprivate:int length;int weight;int hight;public:Box(int, int, int);int volume (););int main()(using namespace std;Box myboxl (30,20,10);cout zThe first Box,s volume = myboxl. volume() endl;Box mybox2(12,10,30);co

5、ut z,The second Box,s volume = mybox2. volume () endl; return 0;)Box:Box(int 1, int w, int h)(length =1;weight = w; hight = h;)int Box:volume()return (hight * weight * length);33.有两个长方柱,其长、宽、高分别为:(1)12,20,25;(2)10,30,20。分别求他们的体积。编个基于对象的程序,且定义两个构造函数,其中一个有参数,个无参数。Sinclude class Boxprivate:int length;i

6、nt wight;int height;public:Box ():Box (int, int, int);int volume ();;int main()(using namespace std;Box mybox1;cout The first boxs volume = myboxl. volume() endl;Box mybox2(10,30,20);cout The second box,s volume = mybox2, volume() endl;return 0;)Box:Box()(length =!2;wight =20;height =25;)Box:Box(int

7、 1, int w, int h)(length =1;wight = w;height = h;)int Box:volume()(return length * wight * height;)34 .声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。#include using namespace std; template class Compare public:Compare(numtype a,numtype b)x=a;y=b; numtype max()return (xy)?x:y; numtype min()return (xy)?x:y; pr

8、ivate: numtype x, y;);int main()Comparecmp1(3,4);coutcmpl. max ()z, is the Maximum of two inteder numbers.,zendl;coutcmpl. min() “ is the Minimum Compare cmp2 (45. 78, 93. 6);coutcmp2. max() is the Maximum coutcmp2. min() is the Minimum Compare cmp31 a,A);coutcmp3. max() is the Maximum coutcmp3. min

9、 () is the Minimum return 0:of two inteder numbers. endlendl;of two float numbers. endl;of two float numbers. endlendl;of two characters. endl;of two characters. endl;35 .建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。初值自拟。#includeusing namespace std;class Studentpublic:Student (int n, int s):n

10、um(n), score (s)void display()coutnumdisplay();return 0;36.建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。初值自拟。#includeusing namespace std;class Student(public:Student (int n, int s):num(n), score(s)int num;int score;int main()(Studentstud5=Student(01,70), Student(02,71

11、), Student(03,72), Student(04,73),Student (05,74);void max(Student *);Student *p=&stud0;max (p);return 0;)void max (Student *arr)(float max_score=arr0. score;for(int i二;i5;i+)if(max_scorearri. score)max_score=arri. score;coutmax_scorearri. numendl;38.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员

12、、非友元的普通函数。编写程序,求两个复数之和。初值自拟。#include class Complex private: double real; double image; public:Complex ();Complex(double, double);double get_real();double get_image (););Complex operator +(Complex &, Complex &);int main()(Complex si (3,4);Complex s2(5,-10);Complex c3;c3= si + s2;std:cout sl + s2=std:

13、cout ( c3. get_real (), c3. get_image()i) std:endl;/ Complex si (3,4);/ std:cout si. get_real() std:endl;return 0;)Complex:Complex(double r, double i)(real = r;image = i;)Complex:Complex()(real =0;image =0;double Complex:get_real()return real;double Complex:get_image()return image;)Complex operator

14、+(Complex & si, Complex & s2)(Complex c (si. get_real ()+ s2. get_real (), si. get_image ()+ s2. get_image(); return c;)39.定义个复数类Complex,重载运算符“+”,“一”,使之能用于复数的加,减运算,运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数之和,差。初值自拟。#include class Complexprivate:double real;double image;public:Complex ();Complex(double, do

15、uble);Complex operator +(Complex &);Complex operator -(Complex &);void display (););int main()(Complex si (3,4), s2(5,6), c;c = si + s2;c. display ();c = si - s2;c. display ();return 0;Complex:Complex()(real =0;image =0;)Complex:Complex(double r, double i)real = r;image = i;)Complex Complex:operator

16、 +(Complex & si)(Complex c;c. real = si. real + real;c. image = si. image + image;return c;)Complex Complex:operator -(Complex & si)(Complex c;c. real = real - si. real;c. image = image - si. image;return c;)void Complex:display()(using namespace std;cout real “ image endl;)40.定义个复数类Complex,重载运算符“十”

17、,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如: cl+c2, i+cl, cl+i均合法(设i为整数,cl, c2为复数)。编程序,分别求两个复数之和、整数和复数之和。初值自拟。Sinclude class Complex(private:double real;double imag;public:Complex()real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator+(Complex &c2);Complex operator+(int &i);

18、friend Complex operator+(int&, Complex &); void display ();;Complex Complex:operator+(Complex &c)(return Complex(real+c. real, imag+c. imag);)Complex Complex:operator+(int &i)(return Complex(real+i, imag);)void Complex:display()using namespace std;cout”(real,imagi)endl;Complex operator+(int &i, Comp

19、lex &c)(return Complex(i+c. real, c. imag);)int main()(using namespace std;Complex cl (3,4), c2(5,-10), c3;int i=5;c3=cl+c2;coutcl+c2=;c3. display ();c3二i+cl;couti+cl=;c3. display ();c3=cl+i;cout“cl+i=”;c3. display ();return 0;41,有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如c=a+b。初值自拟。ftinclude using

20、 namespace std;class Matrixpublic:Matrix();friend Matrix operator+(Matrix &, Matrix &);void input ();void display ();private:int mat 23;定义Matrix类默认构造函数重载运算符“ + ”输入数据函数输出数据函数Matrix:Matrix()for(int i=0;i2;i+)for(int j=0;j3;j+) matij=0;)Matrix operator+(Matrix &a, Matrix &b)Matrix c;for(int i=0;i2;i+)f

21、or(int j=0;j3;j+)c. mati j=a. mati j+b. mati j; return c;)void Matrix:input()cout input value of matrix: endl;for (int i=0;i2;i+)for(int j=0;j3;j+) cinmati j;)void Matrix:display()for (int i=0;i2;i+)for(int j=0;j3;j+)coutmatij ; coutendl;定义构造函数定义重载运算符“ + ”函数定义输入数据函数定义输出数据函数);int main()Matrix a, b, c

22、;a. input ();b. input ();coutendl“Matrix a:endl;a. display ();coutendlMatrix b:endl;b. display ();c=a+b;用重载运算符“+”实现两个矩阵相加coutendlMatrix c = Matrix a + Matrix b :endl;c. display ();return 0;)42 .将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为Complex类的友元函数。初值自拟。#include class Compare private:int real;int imag;

23、public:Compare ();Compare(int, int);friend Compare operator+(Compare & cl, Compare & c2);void display (););int main()(Compare cl (3,4), c2(2,5), c3;c3= cl + c2;c3. display ();return 0;)Compare:Compare ()real =0;imag =0;Compare:Compare(int r, int i)(real = r;imag = i;)void Compare:display()(using nam

24、espace std;cout real ” imag i,用于两个字符串的大于的比较运算。初值自拟。ttinclude #include using namespace std; class String ( public:String() p=NULL;String(char *str);friend bool operator(String &stringl, String &string2);friend bool operator(String &stringl,String &string2)if (strcmp(stringl. p, string2. p)0) return t

25、rue;elsereturn false;)bool operator(String &stringl, String &string2)if (strcmp(stringl. p, string2. p)(stringl, string2)=1)(stringl. display();cout;string2 display();else if(operator(stringl, string2)=l)stringl.display();cout”;string2. display();)else if(operator=(stringl,string2)=1)(stringl.displa

26、y();cout=;string2. display();coutendl;int main()String stringl (Hello), string2(Book), string3(Computer),string4(Hello);compare(stringl, string2);compare(string2, strings);compare(stringl, string4);return 0;46 .定义个描述学生基本情况的类,数据成员包括姓名、学号、C+成绩、英语和数学成绩,成员函数包括输出数据,求出总成绩和平均成绩。数据自拟。#include class Student(

27、private:char * name;int id;int cpp_source;int eng_source;int math_source;public:Student (char *, int, int, int, int);void sum_source();void avg_source(););int main()(Student s(John,1,90,80,97);s. sum_source ();s. avg source ();return 0;Student:Student(char * n, int i, int cs, int es, int ms)name = n

28、;id = i;cpp_source = cs;eng_source = es;math sourcems;void Student:sum_source()(std:cout name The sum of source: cpp_source+eng_source+math_source std:endl;)void Student:avg_source()(double avg;avg =(cpp_source + eng_source + math_source)/3;std:cout Fhe avg of source:”,使之能够用于输出以上类对象。ftinclude class

29、Pointprotected:int x, y;public:Point()x =0, y =0;Point(int a, int b)(this-x = a;this-y = b;void setX(int a)x = a;void setY(int b)y = b;int getX()return x;int getY()return y;);class Circle:public Pointprotected:int r;public:Circle(int x, int y, int r):Point(x, y)this-r = r;void setR(int a)r = a;int g

30、etR()return r; class Cylinder:public Circle ( protected:int h;public:Cylinder ():Circle(0,0,0), h(0)Cylinder(int x, int y, int r, int h):Circle(x, y, r)this-h = h; void setH(int a)h = a; int getH()return h; friend std:istream & operator(std:istream &, Cylinder &); friend std:ostream & operator(std:i

31、stream & input, Cylinder & cc)int _x,_y,_r,一h;std:cout Enter the Cylinder: std:endl; input x _y _r _h;cc. setX(_x);cc. setY(_y);cc. setR(_r);cc. setH(_h);return input;)std:ostream & operator(std:ostream & os, Cylinder & cc)os cc. getX() cc. getY() cc. getRO cc. getH() std:endl; return os;)int main()

32、using namespace std;Cylinder cc; cin cc; cout cc;return 0;48 .写个程序,定义抽象类型Shape,由他派生三个类:Circle (圆形),Rectangle (矩形),Trapezoid (梯形),用个函数printArea分别输出三者的面积,3个图形的数据在定义对象是给定。ttinclude class Shape(public:virtual double area() const =0;);class Circle:public Shape(private:double r;public:Circle (double a):r

33、(a)virtual double area() const(return 3.14* r * r;);class Rectangle:public Shape(private:double h, w;public:Rectangle (double a, double b):h(a), w(b)virtual double area() const(return h * w;);class Trapezoid:public Shape(private:double h, w;public:Trapezoid (double a, double b): h (a), w(b)virtual d

34、ouble area() constreturn 0.5* h * w;);void printArea(const Shape & c)(std:cout c.area() std:endl;)int main()Circle c(2);printArea (c);Rectangle r(2,4);printArea(r);Trapezoid t (3,5);printArea (t);return 0;)49,定义个人员类Cperson,包括数据成员:姓名、编号、性别和用于输入输出的成员函数。在此基础上派生出学生类CStudent (增加成绩)和老师类Cteacher (增加教龄),并实现

35、对学生和教师信息的输入输出。#include class Cperson(protected:char name10;int id;int sex;public:virtual void getlnfor()=0;virtual void printlnfor()=0;);class CStudent:public Cpersonprotected:int course;public:virtual void getlnfor()using namespace std;cout Enterthestudentsinforamtion endl;cout Enterthename:;cin na

36、me ;cout Entertheid:cin id;cout Enterthesex (1 or01. men 0. women):;cin sex;cout Enterthecourse:cin course;virtual void printlnfor ()(using namespace std;cout id endl;cout name endl;cout sex endl;cout course endl;class Cteacher:public Cperson protected:int age;public:virtual void getlnfor()using nam

37、espace std;cout Enter cout Enter cin name ;cout Enter cin id;cout Enter cin sex;cout Enterthe teacher,s the name:;the id:;the sex (1 orthe age:;inforamtion endl;0 1. men 0. women):cin age;virtual void printlnfor()(using namespace std;cout id endl;cout name endl;cout sex endl;cout age endl;int main()

38、(CStudent stu; stu. getlnfor (); stu. printlnfor ();Cteacher tea; tea. getlnfor (); tea. printlnfor ();return 0;50.定义个学生类Student做基类,再派生一个Graduate类,学生类有学号、姓名、和分数,研究生增加工资,它们有同名的函数display。,利用虚函数,编程分别输出学生和研究生的数据,具体数据自拟。ttinclude ttinclude using namespace std;class Studentprotected:int id;string name;int

39、 score;public:Student (int ,string , int);virtual void display。;);Student:Student(int i, string n, int cr)(id = i;name = n;score = cr;)void Student:display()(/using namespace std;cout id : name endl;cout score endl;class Graduate:public Student(protected: int salary;public:Graduate(int i, string n,

40、int cr, int sa):Student (i, n, cr), salary(sa) void display(););void Graduate:display()(/using namespace std;cout id : name endl;cout score endl; cout salary endl;)int main()(Student stu(l,John”,99);stu. display ();Graduate gra(2,JOHNLIU,100,5000);gra. display ():return 0;二、第二类题目(40道,每题9分,请自行设计输出格式)

41、蒋玉金1-5题1 .输入20个学生成绩(设为int型),统计各分数段的人数,分数段为90及90以上、80到89分、70到79分、60到69分、60分以下。#includeint main ()int a20=55,66,77,88,99,54,64,74,84,94,56,66,76,86,96,60,70,80,90,85);int b, c, d, e, f ;b=c=d=e=f=0;for(int i二;i20;i+)if(ai=90)b+;else if(ai=80)C+;else if(ai=70)d+;else if(ai=60)e+;else f+;)coutb=bendl;co

42、ut“c=”cendl;coutd二”dendl;coute=eendl;cout“f二fendl;return 0;)2 .把有序的两个数组a和b合并,要求合并后数组依然有序,数据自拟。#includeint main ()int i=0;int j=0;int k二;int c7;int a4=2,3,5,8;int b3=l,4,6;while(i4&j3)if (aibj) ck+=ai+;else ck+二bj+;while(i4)ck+=ai+;while(j3)ck+=bj+;for(int n二;n7;n+)coutcn;return 0;)3 .编写个函数prn_pict(i

43、nt m, int n),输出m行n列的图形,图形的第一行由n个字符A组成,图形的第二行由n个B组成,依次递推。用主函数调用执行。4 .编写一个函数,将一个数插入到已是升序的数组中,且插入后该数组仍是升序数组。已是升序数组的内容由主函数给出,待插入的数在主函数中输入。#includeint main ()int i, j, number, all=l,2,4,6,8,9,12,15,20,30);cout 请输入个数:;cinnumber;cout 开始的数组:;for(i=0;i10;i+)coutai;coutendl;i=0;while (i=ai) i+;for(j=10; j=i;

44、j)aj=aj-l;ai=number;cout 插入后的数组:;for(i=0;ill;i+)coutai“return 0;5 .编写个程序,不断要求用户输入两个数,直到其中的个为0,则结束。对于每两个数,程序将使用个函数来计算它们的调和平均数,并将结果返回给mainO,而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下调和平均数=2.0* x * y /(x + y)ttinclude double tiaohe(double, double);int main()double x, y;cout 请输入两个数:;while(cin x y & x * y !=0)doub

45、le t_tiaohe = tiaohe(x, y);cout 调和平均数=t_tiaohe endl;cout 请输入两个数:;)return 0;double tiaohe(double a, double b)double num;num =2.0* a * b /(a + b);return num;)胡圣亮6-13题6 .输入一行字符,分别统计其中包含的数字、字母和其他字符的个数。#includeusing namespace std;int main ()(char c;int a二, b二, d二;while(c=getchar()!=,n )if (c=,a&c= A&c=O&

46、c二9)b+;else d+;)cout”数字的个数:bendl;cout字母的个数:aendl;cout 其他字符的个数: d endl;return 0;7 .有一个3X4的矩阵,要求编写程序求出其中值最大的那个元素的值,以及其所在的行号和列号,数据自拟。#includeusing namespace std;int main()(int a34=1,3,5,6,8,10,11,9,18,13,14,15);int max = a, n, m;for(int i=0;i3;i+)for(int j=0;jmax) max = aij; n = i;m = j;couta34矩阵中最大的值为

47、:maxendl;cout最大值行号:nendl;cout最大值列号:mendl;return 0;8 .任意输入一串字符串,其中有大写也有小写,编程将其中的小写字母转为大写并输出。#includeiostream using namespace std; int main ()char c;cout 请输入一串字符:;while (c=getchar ()!=n)if (c=,a&c=,z) c = c-32;coutc;)coutendl;return 0;)9 .在100200中找出同时满足3除余2,用5除余3和用7除余2的所有整数#includeusing namespace std;

48、int main()(for(int i=100;i=200;i+)(if(i%3=2&i%5=3&i%7=2)couti”)return 0;)10 .有一分数序列:23581321一,一,1235813求出这个数列的前20项之和。#includeusing namespace std;int main()(double a=2, b=l, temp;double sum=0.0;for(int i=l;i=20;i+)sum += a/b;temp = a;a=a+b;b=temp;cout2/1+3/2+5/3+8/5+13/8+21/13.=sumendl;return 0;11.编写

49、个程序,其中main。调用个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值),该程序按照下面的格式要求用户输入摄氏温度值,并显示结果:Please enter a Celsius value:2020 degrees Celsius is 68 degrees Fahrenheit.转换公式如下:华氏温度=1.8 x摄氏温度+32.0#includeusing namespace std;float fun(float);int main()(float c;cout Please enter a Celsius value:;cinc;cout20 degrees Celsius

50、 is ”fun(c) degrees Fahrenheit.# define M 4# define N 5using namespace std;int main()int cM+N, i=0, j=0, k=0;int aM=2,4,6,8;int bN=1,3,5,7,9;while (iM&jN)if(aibj) ck+= ai+;else ck+= bj+;while(iM)ck+= ai+;while(jN)ck+= bj+;for (int z=0;zM+N;z+)coutcz;return 0;)13 .用指针方法完成:对个4X5矩阵(int型)分别进行横向和纵向汇总(如图)。72946561839863951972#includeusing namespace std;int main()(int s45=7,2,9,4,6,5,6,1,8,3,9,8,6,3,9,5,1,9,7,2;int *p;P =&s;for (int i=0; i4*5;i+)coutpi;if (i+l)%5=0) coutendl;return 0;14声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。#include using namespace std; templatecl

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!