您的位置:首页 > 其它

第四章习题

2015-04-26 19:04 127 查看

1.

#include<iostream>

using namespace std;

class complex

{

public:

double real;

double imag;

void get();

void display();

complex()

{

real=0;imag=0;

}

};

void complex ::get()

{

cin>>real>>imag;

}

void complex ::display()

{

cout<<"("<<real<<","<<imag<<")";

}

complex operator+(complex&c1,complex&c2)

{

complex c;

c.real=c1.real+c2.real;

c.imag=c1.imag+c2.imag;

return c;

}

int main()

{

complex c1,c2,c3;

cout<<"c1=";

c1.get();

cout<<"c2=";

c2.get();

c3=operator+(c1,c2);

c3.display();

}



2.

#include<iostream>

using namespace std;

class complex

{

public:

void get();

void display();

complex()//构造函数初始化

{ real=0;imag=0;}

complex operator+(complex&c2);

complex operator-(complex&c2);

complex operator*(complex&c2);

complex operator/(complex&c2);

private:

double real;

double imag;

};

void complex ::get()

{

cin>>real>>imag;

}

void complex ::display()

{

cout<<"("<<real<<","<<imag<<")";

}

complex complex:: operator+(complex&c2)

{

complex c;

c.real=real+c2.real;

c.imag=imag+c2.imag;

return c;

}

complex complex:: operator-(complex&c2)

{

complex c;

c.real=real-c2.real;

c.imag=imag-c2.imag;

return c;

}

complex complex:: operator*(complex&c2)

{

complex c;

c.real=real*c2.real;

c.imag=imag*c2.imag;

return c;

}

complex complex:: operator/(complex&c2)

{

complex c;

c.real=real/c2.real;

c.imag=imag/c2.imag;

return c;

}

int main()

{

complex c1,c2,c3;

cout<<"c1=";

c1.get();

cout<<"c2=";

c2.get();

c3=c1+c2;

cout<<"c1+c2=";c3.display();

c3=c1-c2;

cout<<"c1-c2=";c3.display();

c3=c1*c2;

cout<<"c1*c2=";c3.display();

c3=c1/c2;

cout<<"c1/c2=";c3.display();

}



3.

#include<iostream>

using namespace std;

class complex

{

public:

void get();

void display();

complex()

{

real=0;imag=0;

}

complex(double r)//转化构造函数

{

real=r;imag=0;

}

complex operator+(complex&c2);

private:

double real;

double imag;

};

void complex ::get()

{

cin>>real>>imag;

}

void complex ::display()

{

cout<<"("<<real<<","<<imag<<")";

}

complex complex:: operator+(complex&c2)

{

complex c;

c.real=real+c2.real;

c.imag=imag+c2.imag;

return c;

}

int main()

{

complex c1,c2,c3;

cout<<"c1=";

c1.get();

cout<<"c2=";

c2.get();

c3=c1+c2;

c3.display();

int c;

cout<<"d=";

cin>>c;

complex d(c);

c3=c1+d;

c3.display();

}



4.

#include<iostream>

using namespace std;

class Matrix

{

public:

Matrix();//默认构造函数

friend Matrix operator+(Matrix &,Matrix &);

void input();

void display();

private:

int mat[2][3];

};

Matrix::Matrix()

{

for(int i=0;i<2;i++)

for(int j=0;j<3;j++)

mat[i][j]=0;

}

Matrix operator+(Matrix &a,Matrix &b)

{

Matrix c;

for(int i=0;i<2;i++)

for(int j=0;j<3;j++)

{

c.mat[i][j]=a.mat[i][j]+b.mat[i][j];

}

return c;

}

void Matrix::input()

{

cout<<"input value of matrix:"<<endl;

for(int i=0;i<2;i++)

for(int j=0;j<3;j++)

cin>>mat[i][j];

}

void Matrix::display()

{

for (int i=0;i<2;i++)

{

for(int j=0;j<3;j++)

{

cout<<mat[i][j]<<" ";

}

cout<<endl;

}

}

int main()

{

Matrix a,b,c;

a.input();

b.input();

c=a+b;

cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;

c.display();

return 0;

}



5.

#include<iostream>

using namespace std;

class Matrix

{

public:

Matrix();//默认构造函数

friend Matrix operator+(Matrix &,Matrix &);

friend ostream& operator<<(ostream&,Matrix&);

friend istream& operator>>(istream&,Matrix&);

private:

int mat[2][3];

};

Matrix::Matrix()

{

for(int i=0;i<2;i++)

for(int j=0;j<3;j++)

mat[i][j]=0;

}

Matrix operator+(Matrix &a,Matrix &b)

{

Matrix c;

for(int i=0;i<2;i++)

for(int j=0;j<3;j++)

{

c.mat[i][j]=a.mat[i][j]+b.mat[i][j];

}

return c;

}

istream& operator>>(istream&input,Matrix&mat)

{

cout<<"input value of matrix:"<<endl;

for(int i=0;i<2;i++)

for(int j=0;j<3;j++)

input>>mat.mat[i][j];

}

ostream& operator<<(ostream&output,Matrix&mat)

{

for (int i=0;i<2;i++)

{

for(int j=0;j<3;j++)

{

output<<mat.mat[i][j]<<" ";

}

cout<<endl;

}

}

int main()

{

Matrix a,b,c;

cin>>a;

cin>>b;

c=a+b;

cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;

cout<<c;

return 0;

}

6.

#include<iostream>

using namespace std;

class complex

{

public:

complex()//构造函数

{real=0;imag=0;}

complex(double r)//转化构造函数

{real=r;imag=0;}

operator double()//类型转换函数

{return real;}

double real;

double imag;

};

int main()

{

complex c1,c2; double d ,d1;

cout<<"c1=";

cin>>c1.real>>c1.imag;

cout<<"d1=";

cin>>d;

d1=d+c1;

cout<<"d1="<<d1<<endl;

c2=complex(d1);

cout<<"c2=("<<c2.real<<","<<c2.imag<<")";

}



7.

#include<iostream>

using namespace std;

class student

{

public:

char name[10];

char num[5];

char sex[5];

void get();

};

class teacher

{public:

char name[10];

char num[5];

char sex[5];

teacher()

{

}

teacher(student& s)

{

strcpy(name,s.name);

strcpy(num,s.num);

strcpy(sex,s.sex);

}

void display();

};

void teacher::display()

{

cout<<"请输出老师信息:";

cout<<name[10]<<num[5]<<sex[5];

}

void student::get()

{

cout<<"请输入学生信息: ";

cin>>name[10]>>num[5]>>sex[5];

}

int main()

{

student s1;

s1.get();

teacher t1;

t1.display();

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: