C编程题库.doc

上传人:s****u 文档编号:12742720 上传时间:2020-05-21 格式:DOC 页数:61 大小:296.02KB
返回 下载 相关 举报
C编程题库.doc_第1页
第1页 / 共61页
C编程题库.doc_第2页
第2页 / 共61页
C编程题库.doc_第3页
第3页 / 共61页
点击查看更多>>
资源描述
31定义盒子Box类,要求具有以下成员:长、宽、高分别为x,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的表面积。#include #include using namespace std; class Boxpublic:int weight;int length;int hight; void box_shape(int w, int l, int h);int box_volume(int w, int l, int h);int box_area(int w, int l, int h);int main()Box mybox;cout 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 This boxs volume = box_v endl;box_a = mybox.box_area(mybox.weight, mybox.length, mybox.hight);cout This boxs area = box_a endl;void Box:box_shape(int w, int l, int h)if(w = l & l = h)cout This is a Cube! endl;elsecout This is a cuboid! endl; int Box:box_volume(int w, int l, int h)return w * l * h;int Box:box_area(int w, int l, int h)return 2 * w * l + 2 * l * h + 2 * w * h;32 有两个长方柱,其长、宽、高分别为:(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 mybox1(30, 20, 10);cout The first Boxs volume = mybox1.volume() endl;Box mybox2(12, 10, 30);cout The second Boxs volume = mybox2.volume() endl;return 0;Box:Box(int l, int w, int h)length = l;weight = w;hight = h;int Box:volume()return (hight * weight * length);33 有两个长方柱,其长、宽、高分别为:(1)12,20,25;(2)10,30,20。分别求他们的体积。编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参数。#include class Boxprivate:int length;int wight;int height;public:Box();Box(int, int, int);int volume();int main()using namespace std;Box mybox1;cout The first boxs volume = mybox1.volume() endl;Box mybox2(10, 30, 20);cout The second boxs volume = mybox2.volume() endl;return 0;Box:Box()length = 12;wight = 20;height = 25;Box:Box(int l, int w, int h)length = l;wight = w;height = h;int Box:volume()return length * wight * height;34 声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。 #include using namespace std;templateclass Comparepublic:Compare(numtype a,numtype b)x=a;y=b;numtype max()return (xy)?x:y;numtype min()return (xy)?x:y;private:numtype x,y;int main()Comparecmp1(3,4);coutcmp1.max() is the Maximum of two inteder numbers.endl; coutcmp1.min() is the Minimum of two inteder numbers.endlendl;Compare cmp2(45.78,93.6); coutcmp2.max() is the Maximum of two float numbers.endl; coutcmp2.min() is the Minimum of two float numbers.endlendl; Compare cmp3(a,A); coutcmp3.max() is the Maximum of two characters.endl; coutcmp3.min() is the Minimum of two characters.endl; return 0; 35 建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。初值自拟。#includeusing namespace std;class Studentpublic:Student(int n,int s):num(n),score(s)void display()coutnum scoreendl;private:int num;int score;int main()Student stud5=Student(01,70),Student(02,71),Student(03,72),Student(04,73),Student(05,74);Student *p=stud;for(int i=0;idisplay();return 0;36 建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。初值自拟。#includeusing namespace std;class Studentpublic:Student(int n,int s):num(n),score(s)int num;int score;int main()Student stud5=Student(01,70),Student(02,71),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=0;i5;i+)if(max_scorearri.score)max_score=arri.score;coutmax_score arri.numendl;38. 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。初值自拟。#include class Complexprivate:double real;double image;public:Complex();Complex(double, double);double get_real();double get_image();Complex operator + (Complex &, Complex &);int main()Complex s1(3, 4);Complex s2(5, -10);Complex c3;c3 = s1 + s2;std:cout s1 + s2 = ;std:cout ( c3.get_real() , c3.get_image() i) std:endl;/Complex s1(3, 4);/std:cout s1.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 + (Complex & s1, Complex & s2)Complex c(s1.get_real() + s2.get_real(), s1.get_image() + s2.get_image();return c;39 定义一个复数类Complex,重载运算符“”,“”,使之能用于复数的加,减运算,运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数之和,差。初值自拟。#include class Complexprivate:double real;double image;public:Complex();Complex(double, double);Complex operator + (Complex &);Complex operator - (Complex &);void display();int main()Complex s1(3, 4), s2(5, 6), c;c = s1 + s2;c.display();c = s1 - s2;c.display();return 0;Complex:Complex()real = 0;image = 0;Complex:Complex(double r, double i)real = r;image = i;Complex Complex:operator + (Complex & s1)Complex c;c.real = s1.real + real;c.image = s1.image + image;return c;Complex Complex:operator - (Complex & s1)Complex c;c.real = real - s1.real;c.image = image - s1.image;return c;void Complex:display()using namespace std;cout real image endl;40. 定义一个复数类Complex,重载运算符“”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。编程序,分别求两个复数之和、整数和复数之和。初值自拟。#include class Complexprivate: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);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,Complex &c)return Complex(i+c.real,c.imag);int main()using namespace std; Complex c1(3,4),c2(5,-10),c3;int i=5;c3=c1+c2;coutc1+c2=;c3.display();c3=i+c1;couti+c1=;c3.display();c3=c1+i;coutc1+i=;c3.display();return 0;41. 有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如c=a+b。初值自拟。#include using namespace std;class Matrix /定义Matrix类 public: Matrix(); /默认构造函数 friend Matrix operator+(Matrix &,Matrix &); /重载运算符“+” void input(); /输入数据函数 void display(); /输出数据函数 private: int mat23; ;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+) for(int j=0;j3;j+) c.matij=a.matij+b.matij; return c; void Matrix:input() /定义输入数据函数coutinput value of matrix:endl; for(int i=0;i2;i+) for(int j=0;jmatij;void Matrix:display() /定义输出数据函数for (int i=0;i2;i+) for(int j=0;j3;j+) coutmatij ; coutendl;int main()Matrix a,b,c; a.input(); b.input(); coutendlMatrix 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 Compareprivate:int real;int imag;public:Compare();Compare(int, int);friend Compare operator+(Compare & c1, Compare & c2);void display();int main()Compare c1(3, 4), c2(2, 5), c3;c3 = c1 + 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 namespace std;cout real imag i endl;Compare operator+(Compare & c1, Compare & c2)Compare c(c1.real + c2.real, c1.imag + c2.imag);return c;43. 定义一个字符串类String,用来存放不定长的字符串,重载运算符“”,用于两个字符串的等于比较运算。初值自拟。44. 定义一个字符串类String,用来存放不定长的字符串,重载运算符,用于两个字符串的大于的比较运算。初值自拟。#include #include using namespace std;class String public:String() p=NULL;String(char *str);friend bool operator(String &string1,String &string2);friend bool operator(String &string1,String &string2);friend bool operator=(String &string1,String &string2);void display();private:char *p; ;String:String(char *str) p=str; void String:display() cout(String &string1,String &string2) if(strcmp(string1.p,string2.p)0) return true;else return false; bool operator(String &string1,String &string2) if(strcmp(string1.p,string2.p)(string1,string2)=1) string1.display();cout;string2.display(); else if(operator(string1,string2)=1) string1.display();cout;string2.display(); else if(operator=(string1,string2)=1) string1.display();cout=;string2.display(); coutendl; int main() String string1(Hello),string2(Book), string3(Computer),string4(Hello); compare(string1,string2); compare(string2,string3); compare(string1,string4); return 0; 46. 定义一个描述学生基本情况的类,数据成员包括姓名、学号、C+成绩、英语和数学成绩,成员函数包括输出数据,求出总成绩和平均成绩。数据自拟。#include class Studentprivate: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;id = i;cpp_source = cs;eng_source = es;math_source = ms;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 The avg of source: avg std:endl;47. 先建立一个Point(点)类,包含数据成员x,y(坐标点)。以它为基类,派生出一个Circle(圆)类,增加数据成员r(半径),再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,在增加数据成员h(高)。编写程序,重载运算符“”,使之能够用于输出以上类对象。#include class 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 getR()return r;class Cylinder:public Circleprotected: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:istream & input, Cylinder & cc)int _x, _y, _r, _h;std:cout Enter the Cylinder: _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.getR() cc.getH() cc;cout cc;return 0;48. 写一个程序,定义抽象类型Shape,由他派生三个类:Circle(圆形),Rectangle(矩形),Trapezoid(梯形),用一个函数printArea分别输出三者的面积,3个图形的数据在定义对象是给定。#include class Shapepublic:virtual double area() const = 0;class Circle:public Shapeprivate:double r;public:Circle(double a):r(a)virtual double area() constreturn 3.14 * r * r;class Rectangle:public Shapeprivate:double h, w;public:Rectangle(double a, double b):h(a),w(b)virtual double area() constreturn h * w;class Trapezoid:public Shapeprivate:double h, w;public:Trapezoid(double a, double b):h(a), w(b)virtual double 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(增加教龄),并实现对学生和教师信息的输入输出。#include class Cpersonprotected:char name10;int id;int sex;public:virtual void getInfor() = 0;virtual void printInfor() = 0;class CStudent:public Cpersonprotected:int course;public:virtual void getInfor()using namespace std;cout Enter the students inforamtion endl;cout name ;cout id;cout sex;cout course;virtual void printInfor()using namespace std;cout id endl;cout name endl;cout sex endl;cout course endl;class Cteacher:public Cpersonprotected:int age;public:virtual void getInfor()using namespace std;cout Enter the teachers inforamtion endl;cout name ;cout id;cout sex;cout age;virtual void printInfor()using namespace std;cout id endl;cout name endl;cout sex endl;cout age endl;int main()CStudent stu;stu.getInfor();stu.printInfor();Cteacher tea;tea.getInfor();tea.printInfor();return 0;50. 定义一个学生类Student做基类,再派生一个Graduate类,学生类有学号、姓名、和分数,研究生增加工资,它们有同名的函数display(),利用虚函数,编程分别输出学生和研究生的数据,具体数据自拟。#include #include using namespace std;class Studentprotected:int id;string name;int 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 Studentprotected:int salary;public:Graduate(int i, string n, 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(1, John, 99);stu.display();Graduate gra(2, JOHNLIU, 100, 5000);gra.display();return 0;2、 第二类题目(40道,每题9分,请自行设计输出格式)蒋玉金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=0;i=90)b+;else if(ai=80)c+;else if(ai=70)d+;else if(ai=60)e+;else f+;coutb=bendl;coutc=cendl;coutd=dendl;coute=eendl;coutf=fendl;return 0;2. 把有序的两个数组a和b 合并,要求合并后数组依然有序,数据自拟。#includeint main()int i=0;int j=0;int k=0;int c7;int a4=2,3,5,8;int b3=1,4,6;while(i4&j3)if(aibj) ck+=ai+;else ck+=bj+;while(i4)ck+=ai+;while(j3)ck+=bj+;for(int n=0;n7;n+)coutcn ;return 0;3. 编写一个函数prn_pict(int m,int n),输出m行n列的图形,图形的第一行由n个字符A组成,图形的第二行由n个B组成,依次递推。用主函数调用执行。4. 编写一个函数,将一个数插入到已是升序的数组中,且插入后该数组仍是升序数组。已是升序数组的内容由主函数给出,待插入的数在主函数中输入。#includeint main()int i,j,number,a11=1,2,4,6,8,9,12,15,20,30; coutnumber; cout开始的数组:; for(i=0;i10;i+) coutai ; coutendl; i=0; while (i=ai) i+; for(j=10;j=i;j-) aj=aj-1; ai=number; cout插入后的数组:; for(i=0;i11;i+) coutai ; return 0;5. 编写一个程序,不断要求用户输入两个数,直到其中的一个为0,则结束。对于每两个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给main(), 而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下 调和平均数= 2.0 * x * y / (x + y)#include double tiaohe(double, double);int main()double x, y;cout x y & x * y != 0)double 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=0,b=0,d=0;while(c=getchar()!=n)if(c=a&c=A&c=0&c=9)b+;else d+;cout数字的个数:bendl;cout字母的个数:aendl;cout其他字符的个数:dendl;return 0;7. 有一个34的矩阵,要求编写程序求出其中值最大的那个元素的值,以及其所在的行号和列号,数据自拟。#includeusing namespace std;int main()int a34 = 1,3,5,6,8,10,11,9,18,13,14,15;int max = a00,n,m;for(int i=0;i3;i+)for(int j=0;jmax)max = aij;n = i;m = j;couta34矩阵中最大的值为:maxendl;cout最大值行号:nendl;cout最大值列号:mendl;return 0;8. 任意输入一串字符串,其中有大写也有小写,编程将其中的小写字母转为大写并输出。#includeusing namespace std;int main()char c;cout=a&c=z)c = c-32;coutc;coutendl;return 0
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 考试试卷


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

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


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