第四章习题
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;
}
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;
}
相关文章推荐
- 学习opencv 习题答案-第四章
- 计算机操作系统第四版第四章课后习题答案
- PERL语言入门:第四章:子程序(函数):习题2:求1-100总和
- C++第四章 习题
- 具体数学第二版第四章习题(2)
- 《C Primer Plus 》第六版 习题 第四章
- java程序设计基础_陈国君版第五版_第四章习题
- 【DX11习题学习一】第四章练习 Direct3D Foundations
- 第四章部分习题
- 全国计算机等级二级C++第四章习题编程题6 :编写一个函数 char *strfind(char *s, char *t),用于查找字符串t在字符串s中最右边出现的位置,如果没有找到则返回NULL.
- 教材第四章课后习题
- 算法竞赛入门经典第四章 【uvaoj在线习题(二)】
- How to think like a computer scientist: 第四章 课后习题 5
- python核心编程第四章习题
- Java语言程序设计(基础篇)原书第十版 课后习题 第四章
- Scala函数式编程课后习题答案(第四章)(更新ing)
- 算法竞赛入门经典(第二版)-刘汝佳-第四章 函数与递归 例题+习题(15/16)
- 《学习opencv》第四章课后习题1
- [Erlang危机](4.5)第四章习题
- 第四章习题答案