建立一个复数类,解C++题
建立一个复数类imaginary,其私有数据成员x和y表示复数的实部和虚部,构造函数imaginary用于对复数的实部和虚部初始化,友员函数add,sub,mul,div...
建立一个复数类imaginary,其私有数据成员x和y表示复数的实部和虚部,构造函数imaginary用于对复数的实部和虚部初始化,友员函数add,sub,mul,div分别用于进行复数的加,减,乘和除运算,静态函数show用于现实运算结果。在主函数中,实例化两个复数,并输入一个运算符,按运算符选择相应的友员函数进行复数运算,然后输出运算结果。
展开
展开全部
#include<iostream.h>
class imaginary
{
public:
imaginary(double real,double image)
{
x = real;
y = image;
}
friend imaginary add(double x1,double y1,double x2,double y2);
friend imaginary sub(double x1,double y1,double x2,double y2);
friend imaginary mul(double x1,double y1,double x2,double y2);
friend imaginary div(double x1,double y1,double x2,double y2);
static void show(double x1,double y1,double x2,double y2,char op)
{
switch(op)
{
case '+':
cout<<"两数相加结果为:"<<add(x1,y1,x2,y2).x<<" + "<<add(x1,y1,x2,y2).y<<"i"<<endl;
break;
case '-':
cout<<"两数相减结果为:"<<sub(x1,y1,x2,y2).x<<" + "<<sub(x1,y1,x2,y2).y<<"i"<<endl;
break;
case '*':
cout<<"两数相乘结果为:"<<mul(x1,y1,x2,y2).x<<" + "<<mul(x1,y1,x2,y2).y<<"i"<<endl;
break;
case '/':
cout<<"两数相除结果为:"<<div(x1,y1,x2,y2).x<<" + "<<div(x1,y1,x2,y2).y<<"i"<<endl;
break;
default:
cout<<"输入错误."<<endl;
}
}
private:
double x,y;
};
imaginary add(double x1,double y1,double x2,double y2)
{
double x = x1 + x2;
double y = y1 + y2;
return imaginary(x,y);
}
imaginary sub(double x1,double y1,double x2,double y2)
{
double x = x1 - x2;
double y = y1 - y2;
return imaginary(x,y);
}
imaginary mul(double x1,double y1,double x2,double y2)
{
double x = x1 * x2 - y1 * y2;
double y = x1 * y2 + x2 * y1;
return imaginary(x,y);
}
imaginary div(double x1,double y1,double x2,double y2)
{
double x = (x1 * x2 + y1 * y2)/(x1 * x1 + x2 * x2);
double y = (x2 * y1 - x1 * y2)/(x1 * x1 + x2 * x2);
return imaginary(x,y);
}
void main()
{
double x1,y1,x2,y2;
char op;
cout<<"第一个复数:\n实部:";
cin>>x1;
cout<<"虚部:";
cin>>y1;
cout<<"第二个复数:\n实部:";
cin>>x2;
cout<<"虚部:";
cin>>y2;
cout<<"选择运算:";
cin>>op;
imaginary::show(x1,y1,x2,y2,op);
}
class imaginary
{
public:
imaginary(double real,double image)
{
x = real;
y = image;
}
friend imaginary add(double x1,double y1,double x2,double y2);
friend imaginary sub(double x1,double y1,double x2,double y2);
friend imaginary mul(double x1,double y1,double x2,double y2);
friend imaginary div(double x1,double y1,double x2,double y2);
static void show(double x1,double y1,double x2,double y2,char op)
{
switch(op)
{
case '+':
cout<<"两数相加结果为:"<<add(x1,y1,x2,y2).x<<" + "<<add(x1,y1,x2,y2).y<<"i"<<endl;
break;
case '-':
cout<<"两数相减结果为:"<<sub(x1,y1,x2,y2).x<<" + "<<sub(x1,y1,x2,y2).y<<"i"<<endl;
break;
case '*':
cout<<"两数相乘结果为:"<<mul(x1,y1,x2,y2).x<<" + "<<mul(x1,y1,x2,y2).y<<"i"<<endl;
break;
case '/':
cout<<"两数相除结果为:"<<div(x1,y1,x2,y2).x<<" + "<<div(x1,y1,x2,y2).y<<"i"<<endl;
break;
default:
cout<<"输入错误."<<endl;
}
}
private:
double x,y;
};
imaginary add(double x1,double y1,double x2,double y2)
{
double x = x1 + x2;
double y = y1 + y2;
return imaginary(x,y);
}
imaginary sub(double x1,double y1,double x2,double y2)
{
double x = x1 - x2;
double y = y1 - y2;
return imaginary(x,y);
}
imaginary mul(double x1,double y1,double x2,double y2)
{
double x = x1 * x2 - y1 * y2;
double y = x1 * y2 + x2 * y1;
return imaginary(x,y);
}
imaginary div(double x1,double y1,double x2,double y2)
{
double x = (x1 * x2 + y1 * y2)/(x1 * x1 + x2 * x2);
double y = (x2 * y1 - x1 * y2)/(x1 * x1 + x2 * x2);
return imaginary(x,y);
}
void main()
{
double x1,y1,x2,y2;
char op;
cout<<"第一个复数:\n实部:";
cin>>x1;
cout<<"虚部:";
cin>>y1;
cout<<"第二个复数:\n实部:";
cin>>x2;
cout<<"虚部:";
cin>>y2;
cout<<"选择运算:";
cin>>op;
imaginary::show(x1,y1,x2,y2,op);
}
展开全部
#include
class
imaginary
{
public:
imaginary(double
real,double
image)
{
x
=
real;
y
=
image;
}
friend
imaginary
add(double
x1,double
y1,double
x2,double
y2);
friend
imaginary
sub(double
x1,double
y1,double
x2,double
y2);
friend
imaginary
mul(double
x1,double
y1,double
x2,double
y2);
friend
imaginary
div(double
x1,double
y1,double
x2,double
y2);
static
void
show(double
x1,double
y1,double
x2,double
y2,char
op)
{
switch(op)
{
case
'+':
cout<<"两数相加结果为:"<
>x1;
cout<<"虚部:";
cin>>y1;
cout<<"第二个复数:\n实部:";
cin>>x2;
cout<<"虚部:";
cin>>y2;
cout<<"选择运算:";
cin>>op;
imaginary::show(x1,y1,x2,y2,op);
}
class
imaginary
{
public:
imaginary(double
real,double
image)
{
x
=
real;
y
=
image;
}
friend
imaginary
add(double
x1,double
y1,double
x2,double
y2);
friend
imaginary
sub(double
x1,double
y1,double
x2,double
y2);
friend
imaginary
mul(double
x1,double
y1,double
x2,double
y2);
friend
imaginary
div(double
x1,double
y1,double
x2,double
y2);
static
void
show(double
x1,double
y1,double
x2,double
y2,char
op)
{
switch(op)
{
case
'+':
cout<<"两数相加结果为:"<
>x1;
cout<<"虚部:";
cin>>y1;
cout<<"第二个复数:\n实部:";
cin>>x2;
cout<<"虚部:";
cin>>y2;
cout<<"选择运算:";
cin>>op;
imaginary::show(x1,y1,x2,y2,op);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询