建立一个复数类,解C++题

建立一个复数类imaginary,其私有数据成员x和y表示复数的实部和虚部,构造函数imaginary用于对复数的实部和虚部初始化,友员函数add,sub,mul,div... 建立一个复数类imaginary,其私有数据成员x和y表示复数的实部和虚部,构造函数imaginary用于对复数的实部和虚部初始化,友员函数add,sub,mul,div分别用于进行复数的加,减,乘和除运算,静态函数show用于现实运算结果。在主函数中,实例化两个复数,并输入一个运算符,按运算符选择相应的友员函数进行复数运算,然后输出运算结果。 展开
 我来答
lilete
2009-04-08 · 超过23用户采纳过TA的回答
知道答主
回答量:42
采纳率:0%
帮助的人:65.9万
展开全部
#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);
}
撩影剧透侠
2020-04-13 · TA获得超过3927个赞
知道大有可为答主
回答量:3102
采纳率:29%
帮助的人:208万
展开全部
#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);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式