Self new textbooks -p176_5 (1)

Source:

#include <iostream>
#include <iomanip>
using namespace std;

class myComplex
{
private:
double real, imag;
public:
myComplex();
myComplex(double r,double i);
~myComplex() {};

friend myComplex operator+(const myComplex &c1, const myComplex &c2);
friend myComplex operator*(const myComplex &c1, const myComplex &c2);

void output();

};
myComplex::myComplex()
{
real = 0;
imag = 0;
}
myComplex::myComplex(double r, double i)
{
real = r;
imag = i;
}

myComplex operator+(const myComplex &c1, const myComplex &c2)
{
return myComplex(c1.real + c2.real, c1.imag + c2.imag);
}

myComplex operator*(const myComplex &c1, const myComplex &c2)
{
return myComplex(c1.real * c2.real-c1.imag*c2.imag, c1.imag*c2.real + c1.real*c2.imag);
}
void myComplex::output()
{
cout << real << setiosflags(ios::showpos)<<imag << "i" << endl;
}

main int ()
{
MyComplex C1 (. 1, 2), C2 (. 3,. 4), RES, RES1;
RES = C1 + C2;
RES1 = C1 * C2;
COUT << "two complex numbers, the result is:" ;
res.output ();
COUT << endl;
COUT << "two complex multiplication, the result is:";
res1.output ();
COUT << endl;
System ( "PAUSE");
return. 1;
}

operation result:

 

Guess you like

Origin www.cnblogs.com/duanqibo/p/12275654.html