急用!!求大神用C++编写如下程序:复数类模版程序设计.

 我来答
john_lee1969
2014-07-06 · TA获得超过1700个赞
知道小有建树答主
回答量:419
采纳率:100%
帮助的人:529万
展开全部
#include <iostream>
class Complex {
friend const Complex operator + (const Complex& a, const Complex& b);
friend const Complex operator - (const Complex& a, const Complex& b);
friend const Complex operator * (const Complex& a, const Complex& b);
friend std::ostream& operator << (std::ostream& os, const Complex& a);
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}

private:
double real, imag;
};
const Complex operator + (const Complex& a, const Complex& b)
{
Complex temp;
temp.real = a.real + b.real;
temp.imag = a.imag + b.imag;
return temp;
}
const Complex operator - (const Complex& a, const Complex& b)
{
Complex temp;
temp.real = a.real - b.real;
temp.imag = a.imag - b.imag;
return temp;
}
const Complex operator * (const Complex& a, const Complex& b)
{
Complex temp;
temp.real = a.real * b.real - a.imag * b.imag;
temp.imag = a.real * b.imag + a.imag * b.real;
return temp;
}
std::ostream& operator << (std::ostream& os, const Complex& a)
{
os << a.real;
if (a.imag > 0)
os << '+';
if (a.imag != 0)
os << a.imag << 'i';
return os;
}
int main()
{
using namespace std;
Complex a(1.1, 2.2), b(3.3, 4.4), c(5.5, 6.6), d(7.7, 8.8);
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << a * b + c - d << endl;
return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式