clase compleja sobrecarga el operador de suma, resta y multiplicación

La siguiente definición de clase de un complejo y ha logrado, en parte, ahora se exige al constructor de la clase, y los operadores +, - y * asignación completa sobrecarga de funciones.

definición de clase plural:

在这里描述复数类定义。具体如下:
class complex {
	public:
		complex(float r=0,float i=0);                   // 构造函数
		complex operator+(const complex &op2) const;    //重载运算符 +
		complex operator-(const complex &op2) const;    //重载运算符 -
		complex operator*(const complex &op2) const;    //重载运算符 *
		void display() const;                           // 按数学写法输出复数
	private:
		float real;
		float imag;
};
 

programa de prueba Árbitro Ejemplo:

在这里给出复数类测试的例子。例如:
#include <iostream>
using namespace std;
class complex {
	public:
		complex(float r=0,float i=0);                   // 构造函数
		complex operator+(const complex &op2) const;    //重载运算符 +
		complex operator-(const complex &op2) const;    //重载运算符 -
		complex operator*(const complex &op2) const;    //重载运算符 *
		void display() const;                           // 按数学写法输出复数
	private:
		float real;
		float imag;
};

/* ------------------- 请在这里填写答案-------------------- */

void complex::display() const {
if(real&&imag)
if(imag1||imag-1)
cout<<real<<(imag>0?"+":"-")<<"i"<<endl;
else
cout<<real<<(imag>0?"+":"")<<imag<<"i"<<endl;
else if(real)
cout<<real<<endl;
else if (imag)
if(imag1||imag-1)
cout<<(imag>0?"":"-")<<"i"<<endl;
else
cout<<imag<<"i"<<endl;
else
cout<<0<<endl;
}

int main() {
double real,imag;
complex c,d,e;

<span class="hljs-built_in">cin</span>&gt;&gt;real&gt;&gt;imag;
<span class="hljs-function"><span class="hljs-built_in">complex</span> <span class="hljs-title">c1</span><span class="hljs-params">(real,imag)</span></span>;
<span class="hljs-built_in">cin</span>&gt;&gt;real&gt;&gt;imag;
<span class="hljs-function"><span class="hljs-built_in">complex</span> <span class="hljs-title">c2</span><span class="hljs-params">(real,imag)</span></span>;

c=c1+c2;
d=c1-c2;
e=c1*c2;
c.<span class="hljs-built_in">display</span>() ;
d.<span class="hljs-built_in">display</span>() ;
e.<span class="hljs-built_in">display</span>();

<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;

}

de entrada de la muestra:

Aquí se nos da un conjunto de entradas. Por ejemplo:

2 3
-4 -5
 

Resultado de muestra:

Teniendo en cuenta aquí de salida correspondiente. Por ejemplo:

-2-2i
6+8i
7-22i
 

códigos correctos:

complex::complex(float r,float i){
    real = r;
    imag = i;
}
complex complex::operator+(const complex &op2) const{
    complex temp;
    temp.imag = this->imag + op2.imag;
    temp.real = this->real + op2.real;
    return temp;
}
complex complex::operator-(const complex &op2) const{
    complex temp;
    temp.imag = this->imag - op2.imag;
    temp.real = this->real - op2.real;
    return temp;
}
complex complex::operator*(const complex &op2) const{
    complex temp;
    temp.imag = this->imag * op2.real+this->real*op2.imag;
    temp.real = this->real * op2.real-this->imag*op2.imag;
    return temp;
}   

Supongo que te gusta

Origin www.cnblogs.com/cwy545/p/12670559.html
Recomendado
Clasificación