[Cpp]
/*
* Copyright and version Declaration of the program
* Copyright (c) 2012, a student from the computer College of Yantai University
* All rightsreserved.
* File name: x. cpp
* Author: Xu Benxi
* Completion date: January 1, March 24, 2013
* Version: v1.0
* Input description:
* Problem description:
* Program output:
*/
// My code:
# Include <iostream>
Using namespace std;
Class Complex
{Public:
Complex () {real = 0; imag = 0 ;}
Complex (double r, double I) {real = r; imag = I ;}
Friend Complex operator + (Complex & c1, Complex & c2 );
Friend Complex operator-(Complex & c1, Complex & c2 );
Friend Complex operator * (Complex & c1, Complex & c2 );
Friend Complex operator/(Complex & c1, Complex & c2 );
Void display ();
Private:
Double real;
Double imag;
};
Complex operator + (Complex & c1, Complex & c2)
{
Return Complex (c1.real + c2.real, c1.imag + c2.imag );
}
Complex operator-(Complex & c1, Complex & c2)
{
Return Complex (c1.real-c2.real, c2.imag-c2.imag );
}
Complex operator * (Complex & c1, Complex & c2)
{
Complex c;
C. real = c1.real * c2.real-c1.imag * c2.imag;
C. imag = c1.imag * c2.real + c1.imag * c2.imag;
Return c;
}
Complex operator/(Complex & c1, Complex & c2)
{
Complex c;
C. real = (c1.real * c2.real + c1.imag * c2.imag)/(c2.real * c2.real + c2.imag * c2.imag );
C. imag = (c1.imag * c2.real-c1.real * c2.imag)/(c2.real * c2.real + c2.imag * c2.imag );
Return c;
}
Void Complex: display ()
{
Cout <"(" <real <"," <imag <"I)" <endl;
}
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 ();
Int aa;
Cin> aa;
Return 0;
}
/*
* Copyright and version Declaration of the program
* Copyright (c) 2012, a student from the computer College of Yantai University
* All rightsreserved.
* File name: x. cpp
* Author: Xu Benxi
* Completion date: January 1, March 24, 2013
* Version: v1.0
* Input description:
* Problem description:
* Program output:
*/
// My code:
# Include <iostream>
Using namespace std;
Class Complex
{Public:
Complex () {real = 0; imag = 0 ;}
Complex (double r, double I) {real = r; imag = I ;}
Friend Complex operator + (Complex & c1, Complex & c2 );
Friend Complex operator-(Complex & c1, Complex & c2 );
Friend Complex operator * (Complex & c1, Complex & c2 );
Friend Complex operator/(Complex & c1, Complex & c2 );
Void display ();
Private:
Double real;
Double imag;
};
Complex operator + (Complex & c1, Complex & c2)
{
Return Complex (c1.real + c2.real, c1.imag + c2.imag );
}
Complex operator-(Complex & c1, Complex & c2)
{
Return Complex (c1.real-c2.real, c2.imag-c2.imag );
}
Complex operator * (Complex & c1, Complex & c2)
{
Complex c;
C. real = c1.real * c2.real-c1.imag * c2.imag;
C. imag = c1.imag * c2.real + c1.imag * c2.imag;
Return c;
}
Complex operator/(Complex & c1, Complex & c2)
{
Complex c;
C. real = (c1.real * c2.real + c1.imag * c2.imag)/(c2.real * c2.real + c2.imag * c2.imag );
C. imag = (c1.imag * c2.real-c1.real * c2.imag)/(c2.real * c2.real + c2.imag * c2.imag );
Return c;
}
Void Complex: display ()
{
Cout <"(" <real <"," <imag <"I)" <endl;
}
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 ();
Int aa;
Cin> aa;
Return 0;
}