帮忙做一道C++的题吧。谢谢哦。

来源:百度知道 编辑:UC知道 时间:2024/06/16 15:01:35
1、定义一个复数类CComplex,
1) 用成员函数的方法重载运算符+、-,实现复数之间的加、减法运算;

2) 用友元函数的方法,重载运算符×、/,实现实数与复数之间的乘、除运算,如20×a(a为复数类对象)。

复数乘除法公式:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i
(a+bi)/(c+di)=(ac+bd)/(c2+d2)+(bc-ad)/(c2+d2)i

你早一天啊!
前一段时间在公司实习时做过!

今天放假!~如果周一还没人答你我再答吧!~~现在懒着写!~

除法没看明白你那公式 你自己改改吧
#include <iostream.h>
class CComplex
{
private:
double r;
double i;
public:
CComplex(double x=0.0,double y=0.0)
{
r=x;
i=y;
}
CComplex operator+(const CComplex &c)
{
CComplex t;
t.r=r+c.r;
t.i=i+c.i;
return t;

}
CComplex operator-(const CComplex &c)
{
CComplex t;
t.r=r-c.r;
t.i=i-c.i;
return t;
}
friend CComplex operator*(const CComplex &c1,const CComplex &c2)
{

CComplex t;
t.r=c1.r*c2.r-c1.i*c2.i;
t.i=c1.r*c2.i+c1.i*c2.r;
return t;
}
}
friend CComplex operator*(const CComplex &c1,const CComplex &c2)
{

CComplex t;
t.r=c1.r*c2.r-c1.i*c2.i;
t.i=c1.r*c2.i+c1.i*c2.r;
return t;
}

};