#include <iostream>
using namespace Std;
Class Complex
{
Public
Complex () {real = 0; imag = 0;}
Complex (Double R, double i) {real = r; imag = i;}
Complex operator+ (const Complex &C2);
Complex operator-(const Complex &C2);
Complex operator* (const Complex &C2);
Complex operator/(const Complex &C2);
void display ();
Private
Double Real;
Double imag;
};
The following defines the member functions
Complex complex::operator+ (const Complex &C2)
{
Return Complex (real + c2.real, imag + c2.imag);
}
Complex complex::operator-(const Complex &C2)
{
Return Complex (Real-c2.real, Imag-c2.imag);
}
Complex complex::operator* (const Complex &C2)
{
Return Complex (Real*c2.real-imag*c2.imag, Real*c2.imag + imag*c2.real);
}
Complex complex::operator/(const Complex &C2)//(AC+BD)/(c^2+d^2) +((bc-ad)/(c^2+d^2))i
{
Return Complex ((real*c2.real + imag*c2.imag)/(C2.real*c2.real + C2.imag*c2.imag), (IMAG*C2.REAL-REAL*C2.IMAG)/(c2.re Al*c2.real + C2.imag*c2.imag));
}
void Complex::d isplay ()
{
if (Imag = = 0)
cout << Real << Endl;
else if (Imag < 0)
cout << Real << "-" <<-imag << "i" << Endl;
Else
cout << Real << "+" << imag << "i" << Endl;
}
The following defines the main () function for testing
int main ()
{
Complex C1 (3, 4), C2 (5, -10), C3;
cout << "c1=";
C1.display ();
cout << "c2=";
C2.display ();
C3 = C1 + C2;
cout << "c1+c2=";
C3.display ();
C3 = C1-c2;
cout << "c1-c2=";
C3.display ();
C3 = C1*c2;
cout << "c1*c2=";
C3.display ();
C3 = C1/c2;
cout << "c1/c2=";
C3.display ();
while (1) {}
return 0;
}
Eighth Week item-1.1