C++编程题 联系客服

发布时间 : 星期六 文章C++编程题更新完毕开始阅读91e4b2ea102de2bd960588dd

{

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<>z1;

cout<

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?\ else if(fabs(z._y+1)

else if(z._y >err)

cout<<(fabs(z._x)>err?\ else if(z._y<-err) cout<

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<<\分以上的人数是:\ 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=\ String s(Head); cout<<\正序:\ s.Print(); s.Reverse(); cout<<\逆序:\ s.Print(); return 0; }

22编写程序形成如下矩阵。

?1?2?A=?3??4??5

1111?1111??2111?

?3211?4321??#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<

}

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)); }