定义一个复数类Complex,重载运算符“+”、“-”、“*”和“/”,使之能用于复数的加、减、乘和除。

运算符重载函数作为Complex类的成员函数。编写程序,分别求两个复数的和、差、积和商

第1个回答  推荐于2017-10-05

#include <iostream>

using namespace std;


class Complex

{

public:

Complex(){real=0;imag=0;}

Complex(double r,double i){real=r;imag=i;}

Complex operator +(Complex &c2);

Complex operator -(Complex &c2);

Complex operator *(Complex &c2);

Complex operator /(Complex &c2);

void display();

void set();

private:

double real;

double imag;

};


Complex Complex::operator +(Complex &c2)

{

Complex c;

c.real=real+c2.real;

c.imag=imag+c2.imag;

return c;


Complex Complex::operator -(Complex &c2)

{

Complex c;

c.real=real-c2.real;

c.imag=imag-c2.imag;

return c ;

Complex Complex::operator *(Complex &c2)

{

Complex c;

c.real=real*c2.real-imag*c2.imag;

c.imag=real*c2.imag+imag*c2.real;

return c;

}      

Complex Complex::operator /(Complex &c2)

{

Complex c;

c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);

c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);

return c;


void Complex::set()

{

cout<<"input real=";cin>>real;

cout<<"  imag=";cin>>imag;

}

void Complex::display()

{

    cout<<"("<<real<<","<<imag<<"i)"<<endl;

}  


int main()

{

Complex c1,c2,c3,c4,c5,c6;

c1.set();

c2.set();

c3=c1+c2;

c4=c1-c2;

c5=c1*c2;

c6=c1/c2;

cout<<"c1=";c1.display();

cout<<"c2=";c2.display();

cout<<"c1+c2=";c3.display();

cout<<"c1-c2=";c4.display();

cout<<"c1*c2=";c5.display();

cout<<"c1/c2=";c6.display();

return 0;

}                                                                                   

本回答被提问者和网友采纳
第2个回答  2013-05-09

同学,你只需要在度娘或百度文库上输入“C++中的复数类”后回车,即可找到你要的。

第3个回答  2013-05-09
#include <complex>
^ ^
第4个回答  2019-05-30

#include&lt;iostream&gt;

using namespace std;

class Complex

{

public:

Complex(){real=0;imag=0;}//默认构造函数

Complex(double r,double i){real=r;imag=i;}//构造函数

Complex operator+(Complex&);//定义重载运算符+函数

Complex operator-(Complex&);//定义重载运算符-函数

Complex operator*(Complex&);//定义重载运算符*函数

Complex operator/(Complex&);//定义重载运算符/函数

void display();//定义成员函数displayprivate:,double real;double imag;,};

//以下四个函数分别是重载+、-、*、/

Complex Complex::operator+(Complex&c){

return Complex(real+c.real,imag+c.imag);}

Complex Complex::operator-(Complex&c){

return Complex(real-c.real,imag-c.imag);}

Complex Complex::operator*(Complex&c){

return Complex(real*c.real,imag*c.imag);}

Complex Complex::operator/(Complex&c){

return Complex(real/c.real,imag/c.imag);}

//display函数

void Complex::display(){cout&lt;&lt;"("&lt;&lt;real;if(imag&gt;=0)cout&lt;&lt;"+";

cout&lt;&lt;imag&lt;&lt;"i)"&lt;&lt;endl;}int main(){

Complex c1(4.4,5),c2(4,-10),c3;//定义三个Complex对象c1,c2,c3,c1和c2有默认值

c3=c1+c2;c3.display();c3=c1-c2;c3.display();c3=c1*c2;c3.display();c3=c1/c2;c3.display();

system("pause");return 0;}

扩展资料:

return表示把程序流程从被调函数转向主调函数并把表达式的值带回主调函数,实现函数值的返回,返回时可附带一个返回值,由return后面的参数指定。

return通常是必要的,因为函数调用的时候计算结果通常是通过返回值带出的。如果函数执行不需要返回计算结果,也经常需要返回一个状态码来表示函数执行的顺利与否(-1和0就是最常用的状态码),主调函数可以通过返回值判断被调函数的执行情况。

本回答被网友采纳
第5个回答  2013-05-09
好好看一下,运算符的重载,挺简单的

(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(complex &c); \/\/深度复制构造函数 complex(float r, float i); \/\/普通构造函数 void set(float r, float i); \/\/变量重新赋值 complex(); \/\/无参构造函数 friend complex add(complex &b, complex &c); \/\/相加 friend complex sub(complex &b, complex &c); \/\/相...

用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加...
public Complex add(Complex c) { return new Complex(m + c.m, n + c.n);} \/\/ minus public Complex minus(Complex c) { return new Complex(m - c.m, n - c.n);} \/\/ multiply public Complex multiply(Complex c) { return new Complex(m * c.m - n * c.n, m * c.n...

定义一个复数类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...

1)编写复数类,重载加法+、减法-、乘法*等运算符,其中加法+为友元函数...
{ public:Complex():real(0),imag(0) {} Complex(double r,double i):real(r),imag(i) {} friend Complex operator + (Complex &, Complex &);Complex operator - ( Complex &);Complex operator * ( Complex &);void display();private:double real;double imag;};Complex operator + ...

用c#编写一个复数类ComplexNumber并测试其功能,要求能进行复数间的基本...
public Complex()\/\/构造函数,将复数的实部和虚部都置0。{ this.RealPart = 0;this.ImaginPart = 0;} public Complex(double r, double i)\/\/ 构造函数,形参 r 为实部的初值,i为虚部的初值。{ this.RealPart = r;this.ImaginPart = i;} public double getReal()\/\/获得复数对象的实部;{ ...

用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加...
复数具有实部和虚部两部分,如2+5i就是一个复数,其中2为实部,5i为虚部;两复数相加时, 实部与实部相加, 虚部与虚部相加。两复数相减时,实部与实部相减, 虚部与虚部相减。如(2+5i)+(3-4i)=5+1i (2+5i)-(3-4i)=-1+9i 现在要求定义一个复数类Complex,能够创... 展开 我...

请定义复数抽象数据类型Complex-|||-一个复数的形式: e1+e2i 或(e1...
Complex = (e1, e2)其中,e1 为实部,e2 为虚部,均为实数。可以通过重载加、减、乘、除等算术运算符来实现复数的加减乘除运算。此外,还可以定义一些其他的操作,如求模、取共轭等。具体实现代码如下:```python class Complex:def __init__(self, e1, e2):self.e1 = e1 # 实部 self.e2...

关于C语言的题目:用结构体定义一个复数,并实现复数的加法、减法和乘法...
struct complex { int re;int im;};void add(struct complex a, struct complex b, struct complex *c){ c->re=a.re+b.re;c->im=a.im+b.im;} void minus(struct complex a, struct complex b, struct complex *c){ c->re=a.re-b.re;c->im=a.im-b.im;} int main(){...

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...

相似回答