/** Copyright (c) 2013, School of Computer Science, Yantai University * All rights reserved. * Author: Ma Guangming * Completion Date: July 15, April 9, 2014 * Problem description: Template Class-addition, subtraction, multiplication, division * version number: v1.0 */# include
Using namespace std; template
Class Complex {public: Complex () {real = 0; imag = 0;} Complex (T r, T I) {real = r; imag = I ;} complex complex_add (Complex & c2); Complex complex_minus (Complex & c2); Complex complex_multiply (Complex & c2); Complex complex_divide (Complex & c2); void display (); private: T real; T imag;}; template
Complex
Complex
: Complex_add (Complex
& C2) {Complex
C; c. real = real + c2.real; c. imag = imag + c2.imag; return c;} template
Complex
Complex
: Complex_minus (Complex
& C2) {Complex
C; c. real = real-c2.real; c. imag = imag-c2.imag; return c;} template
Complex
Complex
: Complex_multiply (Complex
& C2) {Complex
C; c. real = real * c2.real-imag * c2.imag; c. imag = imag * c2.real + real * c2.imag; return c;} template
Complex
Complex
: Complex_divide (Complex
& C2) {Complex
C; T d = c2.real * c2.real + c2.imag * c2.imag; c. real = (real * c2.real + imag * c2.imag)/d; c. imag = (imag * c2.real-real * c2.imag)/d; return c;} template
Void Complex
: Display () {cout <"(" <
C1 (3, 4), c2 (5,-10), c3; c3 = c1.complex _ add (c2); cout <"c1 + c2 ="; c3.display (); Complex
C4 (3.1, 4.4), c5 (5.34,-10.21), c6; c6 = c4.complex _ add (c5); cout <"c4 + c5 ="; c6.display (); c6 = c4.complex _ minus (c5); cout <"C4-C5 ="; c6.display (); c6 = c4.complex _ multiply (c5); cout <"c4 * c5 = "; c6.display (); c6 = c4.complex _ divide (c5); cout <"c4/c5 ="; c6.display (); return 0 ;}