01./* 02 .* Program Copyright and version description section 03. * copyright (c) 2013, Yantai University Computer college student 04. * All rightsreserved. 5. * file name: complex. CPP 06. * Author: Zhao guanzhe 07. * Completion Date: April 8, April 19, 2013. * version: V1.0 09. * input Description: 10. * Problem description: 11. */# 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); friend complex operator + (const double &, complex & C2); friend complex operator + (complex & C1, const double & A); friend complex operator-(complex & C1); void display (); Private: Double real; double imag;}; complex operator + (complex & C1, complex & C2) {Complex C; C. real = c1.real + c2.real; C. imag = c1.imag + c2.imag; return C;} complex operator-(complex & C1, complex & C2) {Complex C; C. real = c1.real-c2.real; C. imag = c1.imag-c2.imag; return C;} 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.real * 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;} complex operator + (const double & A, complex & C2) {return complex (a + c2.real, c2.imag);} complex operator + (complex & C1, const double & A) {return complex (c1.real + A, c1.imag );} complex operator-(complex & C1) {return complex (-c1.real,-c1.imag);} 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 (); C3 = 1.66 + C2; cout <"1.66 + C2 ="; c3.display (); C3 = C1 + 1.57; cout <"C1 + 1.66 ="; c3.display (); cout <"C3 =-C1 ="; C3 =-C1; c3.display (); Return 0 ;}
Running result;