C++面向对象程序设计 2010期末考试 编程题
1、编写一个程序根据输入的三角形的三条边判断是否能组成三角形,如果可以则输出它的面积和三角形
类型(等边、等腰、直角三角形)。
#include
#include
using namespace std;
int main()
{
double a,b,c;
double v,p;
cout<<"请输入三角形三条边:"< cin>>a>>b>>c; if(a+b>c&&a+c>b&&b+c>a) { p=(a+b+c)/2; v=sqrt(p*(p-a)*(p-b)*(p-c)); cout<<"该三角形面积是"< if(a==b&&a==c) cout<<"该三角形是等边三角形!"< else if(a==b&&a!=c||a==c&&a!=b||b==c&&b!=a) cout<<"该三角形是等腰三角形!"< if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(c*c+b*b==a*a)) cout<<"该三角形是直角三角形!"< } else cout<<"这三条边组不成三角形!"< return 0; } 2、定义一个学生类,其中有3 个数据成员:学号、姓名、年龄,以及若干成员函数。同时编写main 函数 使用这个类,实现对学生数据的赋值和输出。 #include #include using namespace std; class student { int num; string name; int age; public: student(){num=0;name='\0';age=0;} student(int,string,int); void show(); }; student::student(int a,string b,int c):num(a),name(b),age(c){} void student::show() { cout<<"student number:"< cout<<"name:"< cout<<"age:"< } int main() { student s1(200803986,"梅寒芳",23); s1.show(); return 0; } 3、从键盘输入若干个学生成绩,统计并输出最高成绩和最低成绩,当输入负数时结束输入。 #include using namespace std; int main() { double a[100]; double max=0,min=100,t; int i; for(i=0;i<100;i++) { cin>>a[i]; if(a[i]<0) break; else { if(a[i]>max) max=a[i]; if(a[i] min=a[i]; } } cout<<"最大值是:"< cout<<"最小值是:"< return 0; } 4、编写一个程序,从键盘输入半径和高,输出圆柱体的底面积和体积。 #include using namespace std; int main() { double a,h,s,v; cout<<"半径为:"< cin>>a; cout<<"高为:"< cin>>h; s=3.14159*a*a; v=s*h; cout<<"底面积为:"< cout<<"体积为:"< return 0; } 5、编写一个程序,输入年、月,打印出该年份该月的天数。 #include main() { int y,m,d; printf("year month=");scanf("%d%d",&y,&m); switch(m){ case 1: case 3: case 5: case 7: case 8: case 10: case 12:d=31;break; case 4: case 6: case 9: case 11:d=30;break; case 2:if (y%4==0 && y%100!=0 || y%400==0) d=29; else d=28; } printf("days=%d\n",d); } 6、编写函数将化氏温度转换为摄氏温度,公式为C=(F-32)*5/9;并在主函数中调用。 #include using namespace std; double fun(double a); int main() { double f=37.5,c; c=fun(f); cout<<"华氏温度为:"< cout<<"摄氏温度为:"< return 0; } double fun(double a) { double b; b=(a-32)*5/9; return b; } 7、声明一个Tree(树)类,有成员ages(树龄),成员函数grow(int years)用以对ages 加上years, showage( )用以显示tree对象的ages值。在主函数中定义Tree类对象,并调用成员函数(学生自行指定实参数 #include using namespace std; class Tree { private: int ages; public: int grow(int years) { ages=ages+years; return ages; } void getage() { cout<<"输入树的树龄:"< cin>>ages; } void showage() {cout<<"该树的年龄是:"< }; int main() { Tree ages,years; ages.getage(); ages.grow(5); ages.showage(); return 0; } 8、定义一个复数类,用友元函数实现对双目运算符“+”的运算符重载,使其适用于复数运算。 #include class Complex { private: double real; double imag; public: Complex(){real=0;imag=0;} Complex(double r,double i):real(r),imag(i){} friend Complex operator+(Complex &c1,Complex &c2); void display(); }; void Complex::display() { cout< } Complex operator+(Complex &c1,Complex &c2) { return Complex(c1.real+c2.real,c1.imag+c2.imag); } int main() { Complex c1(3,4); Complex c2(4,2.3); Complex c3; c3=c1+c2; c3.display(); return 0; } 9、有一个函数如下: x (x<5) y= x+6 (5<=x<15) x-6 (x>=15) 输入x的值,计算出相应的y值。 #include using namespace std; int main() { int x,y; cin>>x; if(x<5) y=5; if(x>=5&&x<15) y=x+6; if(x>=15) y=x-6; cout< return 0; } 10、14、17、使用函数重载的方法定义两个重名函数,分别求出整型数的两数之和和浮点数的两数之和,并在主函数中调用。 #include using namespace std; template T add(T a,T b) { T c; c=a+b; return c; } int main() { int a,b,c; float x,y,z; cout<<"请输入两个整型数:"< cin>>a>>b; cout<<"请输入两个浮点数:"< cin>>x>>y; c=add(a,b); z=add(x,y); cout<<"整型数之和是:"< cout<<"浮点数之和是:"< return 0; } 11、定义一个抽象类shape用以计算面积,从中派生出计算长方形、梯形、圆形面积的派生类。程序中通过基类指针来调用派生类中的虚函数,计算不同形状的面积。 #include #define PI 3.1415926 using namespace std; class Shape { public: void show(); protected: double s; }; void Shape::show() { cout<<"面积:"< } class Circle :public Shape { public: void GetArea(); Circle(double); private: double r; }; Circle::Circle(double a) { r=a; } void Circle::GetArea() { s=r*r*PI; } int main() { Circle C(6); C.GetArea(); C.show(); return 0; } 12、定义计数器类Counter。要求具有以下成员:计数器值;可进行增值和减值记数;可提供记数值。 #include using namespace std; class Counter { public: Counter(int ); Counter operator ++(); Counter operator --(); void display(); private: int i; }; Counter::Counter(int a) { i=a; } void Counter::display() { cout< } Counter Counter:: operator ++() { return Counter (++i); } Counter Counter:: operator --() { return Counter(--i); } int main() { Counter C1(5); cout<<"C1=";C1.display(); ++C1; cout< --C1; cout< cout< return 0; } 13、输入一个自然数,输出其各因子的连乘形式,如输入12,则输出12=1*2*2*3。 #include using namespace std; int main() { int i,n; cin>>n; cout<<"n="<<"1"; for(i=2;i<=n;) { if(n%i==0) { n=n/i; cout<<"*"< continue; } i++; } cout< return 0; 15、定义一个基类:点类,包括x坐标和y坐标,从它派生一个圆类,增加数据成员r(半径),圆类成员 函数包括构造函数、求面积的函数和输出显示圆心坐标及圆半径的函数。 #include using namespace std; class Dot { public: int x; int y; Dot(){} }; class Circle:public Dot { private: int r; public: Circle():Dot(){} void get(); void area(); void show(); }; void Circle::get() { cout<<"输入圆心坐标、圆的半径:"< cin>>x>>y>>r; } void Circle::area() { double s=0; s=3.14159*r*r; cout<<"圆的面积是:"< } void Circle::show() { cout<<"圆心坐标是:("< cout<<"圆的半径是:"< } int main() { Circle d; d.get(); d.area(); d.show(); return 0; } 16、N个整数从小到大排列,输入一个新数插入其中,使N+1个整数仍然有序。#include using namespace std; int main() { int a[11]={10,20,30,40,50,60,70,80,90,100}; int i,j,n; cout<<"原顺序为:"< for(i=0;i<10;i++) cout< cout< cout<<"请输入一个新数:"< cin>>n; if(a[0]>n) { for(i=9;i>=0;i--) a[i+1]=a[i]; a[0]=n; } if(a[9] if(n>a[0]&&n { for(i=1;i<9;i++) if(n>a[i]) j=i+1; for(i=9;i>=j;i--) a[i+1]=a[i]; a[j]=n; } for(i=0;i<11;i++) cout< cout< return 0; } 18、编写一个矩形rectangle类,有数据成员长len和宽wid,构造函数retange(int,int).和友元函数 int area(rectangle T)和int fun(rectangle T) 分别计算给定长方形的周长和面积。 #include using namespace std; class rectangle { public: rectangle(int ,int ); friend int area(rectangle T); friend int fun (rectangle T); private: int len,wid; }; rectangle::rectangle(int a, int b) { len=a; wid=b; } int area(rectangle T) { return (T.len*T.wid); } int fun(rectangle T) { return (2*(T.len+T.wid)); } int main() { rectangle R(20,30); cout<<"面积:"< cout<<"周长:"< return 0; } 19、定义一个复数类,用友元函数实现对双目运算符+和*的运算符重载,使其适用于复数运算。 #include #include class CComplex { #define err 0.00000001 double _x,_y; public: CComplex(double x=0,double y=0):_x(x),_y(y){} CComplex operator+(const CComplex&z); CComplex operator*(const CComplex&z); friend ostream&operator <<(ostream&os,const CComplex&z); friend istream&operator >>(istream&is,CComplex&z); }; int main() { CComplex z1(0,1.2); CComplex z2(1,1.2); cout< cout< cin>>z1; cout< return 0; } CComplex CComplex::operator+(const CComplex&z) { CComplex c; c._x=_x+z._x; c._y=_y+z._y; return c; } CComplex CComplex::operator*(const CComplex&z) { CComplex c; c._x=_x*z._x-_y*z._y; c._y=_x*z._y+_y*z._x; return c; } ostream&operator <<(ostream&os,const CComplex&z) { if(fabs(z._x)>err) cout< if(fabs(z._y-1) cout<<(fabs(z._x)>err?"+i":"i"); else if(fabs(z._y+1) cout<<"-i"; else if(z._y >err) cout<<(fabs(z._x)>err?"+":"")< else if(z._y<-err) cout< return os; } istream&operator >>(istream&is,CComplex&z) { is>>z._x>>z._y; return is; } 20、输入10 个同学的成绩,统计80分以上和不及格的人数,并输出平均值。#include using namespace std; int main() { double a[10],sum=0,var; int i,j=0,k=0; cout<<"请输入10个学生成绩:"< for(i=0;i<10;i++) { cin>>a[i]; sum=sum+a[i]; if(a[i]>=80) j++; if(a[i]<60) k++; } var=sum/10; cout<<"80分以上的人数是:"< cout<<"不及格的人数是:"< cout<<"平均分是:"< return 0; } 21、声明一个类String1,其数据成员为char head[100],构造函数String(char *Head)实现head的初始化,成员函数void Reverse()实现head内字符串的逆序存放,成员函数void Print()实现head 内字符串的输出。 #include #include using namespace std; class String { public: String (char *Head); void Reverse(); void Print (); private: char head[100]; }; String::String(char *Head) { int i=0; char *p=Head; while(*p!='\0') { head[i]=*p; i++; p++; } head[i]='\0'; } void String::Reverse() { int i=0; char h[100],*p=head; while(*p!='\0') p++; while(*p!=head[0]) { p--; h[i]=*p; i++; } h[i]='\0'; while(i>=0) { head[i]=h[i]; i--; } } void String::Print() { char *p=head; for(;(*p)!='\0';p++) cout<<(*p); cout< } int main() { char *Head="abcdefgh"; String s(Head); cout<<"正序:"; s.Print(); s.Reverse(); cout<<"逆序:"; s.Print(); return 0; } 22编写程序形成如下矩阵。 A=??????? ?????? ???12345 11234 111231********* #include using namespace std; int main() { int a[5][5]; int ii,jj; for (ii = 0; ii< 5; ii++) { for (jj = 0; jj < 5; jj++) { if (ii - jj < 1) { a[ii][jj] = 1; } else { a[ii][jj] = ii+1 - jj; } } } for(ii=0;ii<5;ii++) { for(jj=0;jj<5;jj++) cout< cout< } return 0; } 23、定义盒子Box 类,要求具有以下成员:可设置盒子形状;可计算盒子体积;可计算盒子的表面积。 #include using namespace std; class Box { public: Box(double,double,double); double area(); double v(); private: double x,y,z; }; Box::Box(double x1,double y1,double z1) { x=x1; y=y1; z=z1; } double Box::area() { return (2*(x*y+y*z+x*z)); } double Box::v() { return (x*y*z); } int main() { Box B(2,3,4); cout<<"表面积:"< cout<<"体积:"< return 0; } 24、33、声明一个哺乳动物Mammal 类,再由此派生出狗Dog 类,声明一个Dog 类的对象,观察基类与派 生类的构造函数与析构函数的调用顺序。 #include using namespace std; class mammal { public: mammal()//默认构造函数 { cout<<"mammal's constructor"< } ~mammal() { cout<<"mammal'a destucor"< } }; class dog :public mammal { public: dog() { cout<<"dog's constructor"< } ~dog() { cout<<"dog's destructor"< } }; int main() { dog a; return 0; } 25、30、定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类,教师类增加工号、职称和 工资,学生类增加学号、班级、专业和入学成绩。 #include #include using namespace std; class person { public: person(string ,string,int); protected: string name; string sex; int age; }; person::person(string n, string s, int a) { name=n; sex=s; age=a; } class teacher :public person { public: teacher(string ,string,int,string,string,double); void display(); private: string work_num; string title; double wage; }; teacher::teacher(string n, string s, int a, string wo, string t, double wa):person( n,s,a) { work_num=wo; title=t; wage=wa; } void teacher::display() { cout<<"姓名:"< cout<<"工号:"< } class student: public person { public: student(string,string,int,string,string,string,double); void show(); private: string num; string grade; string major; double score; }; student::student(string n, string s, int a, string nu, string g, string m, double sc):person(n,s,a) //注意:只写 n,不是string n { num=nu; grade=g; major=m; score=sc; } void student::show() { cout<<"姓名:"< cout<<"学号:"< } int main() { teacher t("侯传旺","男",20,"200901001010","教授",2100); t.display(); student s("秦洪敏","女",21,"200902002026","大二","应用心理学",540); s.show(); return 0; } 26、写一个Complex类,将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外, 作为Complex类的友元函数。如例题10.3 #include class complex { private: double a; double b; public: complex(); complex(double i,double j); friend complex operator +(complex&,complex); void setreal(double x); void setimag(double y); void getreal(); void getimag(); friend ostream& operator <<(ostream& out,complex& x); friend istream& operator >>(istream& in,complex& x); }; complex::complex():a(0),b(0){} complex::complex(double i,double j):a(i),b(j){} complex operator +(complex&x,complex y) { y.a+=x.a; y.b+=x.b; return complex(y.a,y.b); } ostream& operator<<(ostream& out,complex &x) { out< return out; } istream& operator >>(istream& in,complex& x) { in>>x.a>>x.b; return in; } void complex::setreal(double x){a=x;} void complex::setimag(double y){b=y;} void complex::getreal() {cin>>a;} void complex::getimag() {cin>>b;} int main() { complex a; complex b; cin>>a>>b; //cout< //cout< cout<