运算符重载实现复数的加减乘除
代码写的有点乱了,嘿嘿;
中间用到了复数的除法参考网站http://baike.baidu.com/view/1596407.htm
下面是代码:
1 #include<iostream.h> 2 class plural 3 { 4 private: //定义私有变量——实部,虚部; 5 double real; 6 double imag; 7 public: 8 plural(double r=0.0,double i=0.0){real=r;imag=i;}//初始化; 9 double showreal(){return real;} 10 double showimag(){return imag;} 11 void show(){cout<<'('<<real<<','<<imag<<')'<<endl;}//输出显示 12 void show1(){cout<<'('<<real<<','<<imag<<')';}//输出显示 13 14 ////////////////////////////////////////////////////定义各种重载函数; 15 plural operator+(plural p); 16 plural operator-(plural p); 17 plural operator*(plural p); 18 plural operator/(plural p); 19 plural operator+=(plural p); 20 plural operator-=(plural p); 21 plural operator*=(plural p); 22 plural operator/=(plural p); 23 }; 24 //////////////////////////////////////////////////////加法1; 25 plural plural::operator+(plural p) 26 { 27 plural c; 28 c.real=real+p.real; 29 c.imag=imag+p.imag; 30 return plural(c.real,c.imag); 31 } 32 //////////////////////////////////////////////////////减法1; 33 plural plural::operator-(plural p) 34 { 35 plural c; 36 c.real=real-p.real; 37 c.imag=imag-p.imag; 38 return plural(c.real,c.imag); 39 } 40 //////////////////////////////////////////////////////乘法1; 41 plural plural::operator*(plural p) 42 { 43 plural c; 44 c.real=real*p.real-imag*p.imag; 45 c.imag=real*p.imag+imag*p.real; 46 return plural(c.real,c.imag); 47 } 48 //////////////////////////////////////////////////////除法1; 49 plural plural::operator/(plural p) 50 { 51 plural c; 52 double t=(p.imag*p.imag+p.real*p.real); 53 c.real=(real*p.real+imag*p.imag)/t; 54 c.imag=(imag*p.real-real*p.imag)/t; 55 return plural(c.real,c.imag); 56 } 57 //////////////////////////////////////////////////////加法2; 58 plural plural::operator+=(plural p) 59 { 60 real+=p.real; 61 imag+=p.imag; 62 return *this; 63 } 64 //////////////////////////////////////////////////////减法2; 65 plural plural::operator-=(plural p) 66 { 67 real-=p.real; 68 imag-=p.imag; 69 return *this; 70 } 71 //////////////////////////////////////////////////////乘法2; 72 plural plural::operator*=(plural p) 73 { 74 real=real*p.real-imag*p.imag; 75 imag=real*p.imag+imag*p.real; 76 return *this; 77 } 78 //////////////////////////////////////////////////////除法2; 79 plural plural::operator/=(plural p) 80 { 81 double t=(p.imag*p.imag+p.real*p.real); 82 real=(real*p.real+imag*p.imag)/t; 83 imag=(real*p.real+imag*p.imag)/t; 84 return *this; 85 } 86 int main() 87 { 88 //////////////////////////////////////////////////////定义三个对象 89 plural c1(1,2); 90 plural c2(2,3); 91 plural c3; 92 //////////////////////////////////////////////////////输出初始化的三个对象值 93 c1.show(); 94 c2.show(); 95 c3.show(); 96 //////////////////////////////////////////////////////输出c1,c2加减乘除的值c3; 97 c3=c1+c2; cout<<"c3"<<'=';c1.show1();cout<<'+';c2.show1();cout<<'=';c3.show(); 98 c3=c1-c2; cout<<"c3"<<'=';c1.show1();cout<<'-';c2.show1();cout<<'=';c3.show(); 99 c3=c1*c2; cout<<"c3"<<'=';c1.show1();cout<<'*';c2.show1();cout<<'=';c3.show(); 100 c3=c1/c2; cout<<"c3"<<'=';c1.show1();cout<<'/';c2.show1();cout<<'=';c3.show(); 101 //////////////////////////////////////////////////////加减乘除 102 cout<<"改变前c1的值";c1.show1();cout<<"此时c2的值为";c2.show1();cout<<"加法"<<endl;c1+=c2;cout<<"改变后c1的值";c1.show(); 103 cout<<"改变前c1的值";c1.show1();cout<<"此时c2的值为";c2.show1();cout<<"减法"<<endl;c1-=c2;cout<<"改变后c1的值";c1.show(); 104 cout<<"改变前c1的值";c1.show1();cout<<"此时c2的值为";c2.show1();cout<<"乘法"<<endl;c1*=c2;cout<<"改变后c1的值";c1.show(); 105 cout<<"改变前c1的值";c1.show1();cout<<"此时c2的值为";c2.show1();cout<<"除法"<<endl;c1/=c2;cout<<"改变后c1的值";c1.show(); 106 //////////////////////////////////////////////////////加减乘除 107 cout<<"改变前c2的值";c2.show1();cout<<"此时c1的值为";c1.show1();cout<<"加法"<<endl;c2+=c1;cout<<"改变后c2的值";c2.show(); 108 cout<<"改变前c2的值";c2.show1();cout<<"此时c1的值为";c1.show1();cout<<"减法"<<endl;c2-=c1;cout<<"改变后c2的值";c2.show(); 109 cout<<"改变前c2的值";c2.show1();cout<<"此时c1的值为";c1.show1();cout<<"乘法"<<endl;c2*=c1;cout<<"改变后c2的值";c2.show(); 110 cout<<"改变前c2的值";c2.show1();cout<<"此时c1的值为";c1.show1();cout<<"除法"<<endl;c2/=c1;cout<<"改变后c2的值";c2.show(); 111 ////////////////////////////////////////////////////// 112 return 0; 113 }
posted on 2012-12-17 13:56 LinuxPanda 阅读(3670) 评论(0) 编辑 收藏 举报