第九周 任务1

来源:互联网 发布:海澜之家男装淘宝网 编辑:程序博客网 时间:2024/05/17 23:18
#include <iostream>using namespace std;class Complex{public:friend ostream &operator << ( ostream&out, Complex &t );Complex ( ) { real = 0; imag = 0; }Complex ( double r, double i ) { real = r; imag = i ; }Complex operator + ( Complex &c2 );Complex operator - ( Complex &c2 );Complex operator * ( Complex &c2 );Complex operator / ( Complex &c2 );private:double real;double imag;};//下面定义成员函数ostream &operator << ( ostream&out, Complex &c ){out << "(" << c.real;if ( c.imag >= 0 )  out << "+";out << c.imag << "i)" << endl;return out;}Complex Complex ::operator + ( Complex &c2 ){Complex t;t.real = real + c2.real;t.imag = imag + c2.imag;return t;}Complex Complex ::operator - ( Complex &c2 ){Complex t;t.real = real - c2.real;t.imag = imag - c2.imag;return t;}Complex Complex ::operator * ( Complex &c2 ){Complex t;t.real = real * c2.real;t.imag = imag * c2.imag;return t;}Complex Complex :: operator / ( Complex &c2 ){Complex t;t.real = real / c2.real;t.imag = imag / c2.imag;return t;}int main( ){Complex c1 ( 3 , 4 ) , c2 ( 5, -10 ), c3;cout << "c1=" << c1 << endl;cout << "c2=" << c2 << endl;c3 = c1 + c2;cout << "c1+c2=" << c3 << endl;c3 = c1 - c2;cout << "c1-c2=" << c3 << endl;c3 = c1 * c2;cout << "c1*c2=" << c3 << endl;c3 = c1 / c2;cout << "c1/c2=" << c3 << endl;system ("pause");return 0;}

原创粉丝点击