C++高手来!!编写一个Complex类,需要完成的运算符重载有: + :重载+,用来完成两个复数的加法

编写一个Complex类,需要完成的运算符重载有:
(1) + :重载+,用来完成两个复数的加法;
(2) - :重载 - ,用来完成两个复数的减法;
(3) *:重载*,用来完成两个复数的乘法;
(4) <<:重载<<,用来输出一个复数,输出的格式为(a + b * i),其中a为实部,b为虚部。
还有一个 程序的输入输出和测试程序
编写一个驱动程序来测试Complex类的运算符重载,测试程序要测试所有的运算符重载,测试程序的样例程序请参见给出的代码。

#include <iostream>
#include <assert.h>
class Complex
{
public:
Complex(float a,float b)
:m_real(a)
,m_imaginary(b)
{
}
Complex()
:m_real(0)
,m_imaginary(0)
{
}
const Complex operator+(const Complex& other)
{
Complex c(m_real+other.m_real,m_imaginary+other.m_imaginary);
return c;
}
const Complex operator-(const Complex& other)
{
Complex c(m_real-other.m_real,m_imaginary-other.m_imaginary);
return c;
}
const Complex operator*(const Complex& other)
{
Complex c(m_real*other.m_real-m_imaginary*other.m_imaginary,m_imaginary*other.m_real+m_real*other.m_imaginary);
return c;
}
float m_real,m_imaginary;
};
std::ostream& operator<<(std::ostream& o,const Complex& c)
{
o<<c.m_real<<'+'<<c.m_imaginary<<"*i";
return o;
}

//测试

int main(int argc, char *argv[])
{
Complex a(3.5,2.0);
Complex b(2.0,8.5);
Complex c;
c=a+b;
assert(c.m_real==5.5);
assert(c.m_imaginary==10.5);
c=a-b;
assert(c.m_real==1.5);
assert(c.m_imaginary==-6.5);
c=a*b;
assert(c.m_real==-10.0);
assert(c.m_imaginary==33.75);
std::cout<<c<<std::endl;
return 0;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-12-08
请问:你那个给出的代码是什么?

(C++)定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算...
friend ostream& operator << (ostream&,Complex&); \/\/声明重载运算符“<<” friend istream& operator >> (istream&,Complex&); \/\/声明重载运算符“>>”private: double real; double imag;};Complex operator +(Complex &c1,Complex &c2){ return Complex(c1.real+c2.real,c1.imag+c2.i...

...设计一个完整的复数complex类,要求利用运算符重载实现复数的加法(+...
Complex( double r=0.0,double i=0.0 )\/\/构造 :real(r),image(i)\/\/初始化列表 {} Complex operator+( const Complex& cc )const\/\/两个复数相加 { return Complex(real+cc.real,image+cc.image);} Complex operator-( const Complex& cc )const\/\/两个复数相减 { return Complex(real-...

C++ 一个复数类,运算符重载 + ,实现复数和复数的相加。
\/\/实数 float b;\/\/虚数};Complex& Complex::operator+(Complex &x){ this->a += x.a; this->b += x.b; return *this;}int main(){ Complex com1(3,2),com2(2,1),com3(3,0),com4(0,2),com5(0,0); Complex com; com = com1+com2+com3+com4+...

c++编写一个复数类Complex
complex operator + (complex C){ double x,y;x=real+C.real;y=imagi+C.imagi;return complex(x,y);} friend complex operator - (complex C1,complex C2);complex operator * (complex C);friend complex operator \/ (complex C1,complex C2);complex & operator ++ (){ real=real+1;imag...

C++编写复数计算类
Complex::~Complex(){ real = 0;imag = 0;} void Complex::InputData(){ cout<<"Please input Data:"<<endl;cin>>real>>imag;} Complex operator-(Complex & com1,Complex & com2)\/\/重载+ { Complex temp;temp.real = com1.real - com2.real;temp.imag = com1.imag - com2....

一道复数类C++的题帮忙!!!
CComplex operator +(CComplex &c); \/\/两个复数相加 CComplex operator +(double r); \/\/复数和一个实数相加 \/\/重载'-'CComplex operator -(CComplex &c); \/\/同上 CComplex operator -(double r);\/\/重载'*'CComplex operator *(CComplex &c);CComplex operator *(double r);\/...

关于c++中运算符重载的题 谢谢 有一个double的数和一个复数相加 分两种...
Complex(double r){real=r;imag=0;}\/\/转换构造函数 friend Complex operator +(Complex &c1,Complex &c2);\/\/运算符重载 private:double real;double imag;};Complex operator +(Complex &c1,Complex &c2){ Complex temp;temp.real=c1.real +c2.real;temp.imag=c1.imag +c2.imag;return temp...

C++复数中的“++”运算符重载
++不是这么写的 complex & operator++(complex<int> &temp) ;另外函数体里不能这么写,temp.real()是函数,只有输出,不能被改写的,具体怎么改要看complex类提供了什么函数来修改他的实部和虚部,有没有setreal setimage之类的成员函数?

c++问题,编个程序求两个虚数加减,谢谢!
其实就是定义一个类,类里面包含2个元素,一个实数,一个虚数 然后运算符重载 相加的时候 实数与实数相加,虚数与虚数相加 include<iostream.h> include<string.h> class Complex { public:Complex();Complex(double a,double b);friend Complex operator +(Complex a,Complex b);friend Complex ...

定义一个描述复数的类,数据成员包括实部和虚部;成员函数包括输出复数以 ...
class complex{ \/\/复数类 public:complex(double r=0.0, double i=0.0);complex operator+(const complex& p)const;\/\/重载运算符+ friend ostream& operator <<(ostream& out,const complex& pp);\/\/重载操作符<< private:double real,imag;};complex::complex(double r,double i){ real = ...

相似回答