C++ 定义一个复数类Complex重载运算符 “+”, “-”, “ * ”, “ / ”........................

定义一个复数类Complex重载运算符 “+”, “-”, “ * ”, “ / ”, 使之能用于复数的加,减,乘,除。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数的和,差,积,商。

第1个回答  2009-12-08
#include <iostream>

using namespace std;

class CComplex
{
private:
float real, image;
public:
CComplex() { }
CComplex( float r, float img ) { real = r; image = img; }
CComplex( CComplex& another )
{
real = another.real;
image = another.image;
}

CComplex &operator = (CComplex &another )
{
real = another.real;
image = another.image;
return *this;
}

CComplex operator +( CComplex & another )
{
return CComplex( real+ another.real, image + another.image );
}

CComplex operator -( CComplex & another )
{
return CComplex( real- another.real, image - another.image );
}

CComplex operator *( CComplex & another )
{
CComplex prod;
//prod = *this;

prod.real = real*another.real - image*another.image;
prod.image = real*another.image + image*another.real;
return prod;

//return CComplex( real+ another.real, image + another.image );
}

CComplex operator /( CComplex & another )
{
CComplex quot;
float sq = another.real*another.real + another.image*another.image;

quot.real = (real*another.real + image*another.image)/sq;
quot.image = (image*another.real - real*another.image)/sq;

return quot;

}

};

void main()
{
CComplex c1( 2, 3 ), c2( 3, 3 );
CComplex c4, c5, c6, c7;
c4 = c1 + c2;
c5 = c1 - c2;
c6 = c1*c2;
c7 = c1/c2;

}本回答被提问者采纳

(C++)定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算...
}Complex operator +(Complex &c,double i){ return Complex(i+c.real,c.imag);}Complex operator +(double i,Complex &c)

定义一个复数类complex。
Complex operator +(Complex &); \/\/重载运算符+ Complex operator -(Complex &); \/\/重载运算符- \/\/实现 Complex Complex::operator +( Complex &c){ Complex t;t.Real = this->Real + c.Real;t.Image = this->Image + c.Image;return t;} Complex Complex::operator -( Complex...

定义一个描述复数的类,数据成员包括实部和虚部;成员函数包括输出复数以 ...
C++实现的复数类,代码如下 \/\/complex.cpp include <iostream> using namespace std;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);\/\/重载操作...

用C++语言设计一个虚数类,要求虚数类中重载运算符加减,主函数定义类...
代码如下:\/*1.复数(实部运算 +虚部运算) 1+2i 1-3i 加:2-i 减 0-5i1 重载实现复数一个输入和输出 普通写法实现输入 调用函数的形式实现输出2. 类重载实现复数的加法和减法 加法:类重载 减法:友元重载*\/#include<iostream>using namespace std;class A{private:int...

...设计一个完整的复数complex类,要求利用运算符重载实现复数的加法(+...
\/ include <iostream> include <cmath> using namespace std;class Complex{\/\/定义复数类 double real;\/\/实部 double image;\/\/虚部 public:Complex( double r=0.0,double i=0.0 )\/\/构造 :real(r),image(i)\/\/初始化列表 {} Complex operator+( const Complex& cc )const\/\/两个复数相加 {...

求C++程序代码: 建立 一个复数类 Complex
include<cmath> using namespace std;class Complex { public:Complex();Complex(float,float);\/\/Complex(Complex &Complex); \/\/这个拷贝构造函数你下面没有定义 对象名不能与类名相同,改成小写 Complex(Complex &complex);void put_real1(float); \/\/获得复数的实部 void put_imaginary1(f...

c++编写一个复数类Complex
{ private:double real,imagi;static int countP;public:complex(){ real=0;imagi=0;countP++;} complex(double x,double y){ real=x;imagi=y;countP++;} complex(complex &C){ real=C.real;imagi=C.imagi;countP++;} ~complex(){countP--;} void SetReal(double x){real=x;} void...

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 operator+(Complex & com1,Complex & com2)\/\/重载- { Complex temp;temp.real = com1.real +com2.real;temp.imag = com1.imag + com2.imag;return temp;} void Complex::DisplayData()\/\/输出Data { cout<< "the Data is:"<<endl;cout<<"("<<real<<","<<imag<<")"...

急!C++定义一个复数类Complex,使下面的代码能够工作?
double re;double im;public:Complex(double re,double im=0){ this->re = re;this->im = im;} void add(const Complex& c){ this->re += c.re;this->im += c.im;} void sub(const Complex& c){ this->re -= c.re;this->im -= c.im;} void show(){ if(this->re==...

相似回答