运算符重载的函数(3)

/*
 *copyright(c) 2014,烟台大学计算机学院
 *All rights reserved
 *文件名称:test.cpp
 *作者:吴雨凡
 *版本:v6.0
 *
 *问题描述:运算符重载的函数(3)

 *输入描述:
 *程序输出:判断相关问题
*/

  1. #include <iostream>  
  2. using namespace std;  
  3. class Complex  
  4. {  
  5. public:  
  6.     Complex()  
  7.     {  
  8.         real=0;  
  9.         imag=0;  
  10.     }  
  11.     Complex(double r,double i)  
  12.     {  
  13.         real=r;  
  14.         imag=i;  
  15.     }  
  16.     friend Complex operator+(Complex &c1, Complex &c2);  
  17.     friend Complex operator+(double d1, Complex &c2);  
  18.     friend Complex operator+(Complex &c1, double d2);  
  19.     friend Complex operator-(Complex &c1, Complex &c2);  
  20.     friend Complex operator-(double d1, Complex &c2);  
  21.     friend Complex operator-(Complex &c1, double d2);  
  22.     friend Complex operator*(Complex &c1, Complex &c2);  
  23.     friend Complex operator*(double d1, Complex &c2);  
  24.     friend Complex operator*(Complex &c1, double d2);  
  25.     friend Complex operator/(Complex &c1, Complex &c2);  
  26.     friend Complex operator/(double d1, Complex &c2);  
  27.     friend Complex operator/(Complex &c1, double d2);  
  28.     void display();  
  29. private:  
  30.     double real;  
  31.     double imag;  
  32. };  
  33.   
  34.  
  35. Complex operator+(Complex &c1, Complex &c2)  
  36. {  
  37.     Complex c;  
  38.     c.real=c1.real+c2.real;  
  39.     c.imag=c1.imag+c2.imag;  
  40.     return c;  
  41. }  
  42. Complex operator+(double d1, Complex &c2)  
  43. {  
  44.     Complex c(d1,0);  
  45.     return c+c2;
  46. }  
  47. Complex operator+(Complex &c1, double d2)  
  48. {  
  49.     Complex c(d2,0);  
  50.     return c1+c;  
  51. }  
  52.  
  53. Complex operator-(Complex &c1, Complex &c2)  
  54. {  
  55.     Complex c;  
  56.     c.real=c1.real-c2.real;  
  57.     c.imag=c1.imag-c2.imag;  
  58.     return c;  
  59. }  
  60. Complex operator-(double d1, Complex &c2)  
  61. {  
  62.     Complex c(d1,0);  
  63.     return c-c2;  
  64. }  
  65. Complex operator-(Complex &c1, double d2)  
  66. {  
  67.     Complex c(d2,0);  
  68.     return c1-c;  
  69. }  
  70.   
  71.   
  72. Complex operator*(Complex &c1, Complex &c2)  
  73. {  
  74.     Complex c;  
  75.     c.real=c1.real*c2.real-c1.imag*c2.imag;  
  76.     c.imag=c1.imag*c2.real+c1.real*c2.imag;  
  77.     return c;  
  78. }  
  79. Complex operator*(double d1, Complex &c2)  
  80. {  
  81.     Complex c(d1,0);  
  82.     return c*c2;  
  83. }  
  84. Complex operator*(Complex &c1, double d2)  
  85. {  
  86.     Complex c(d2,0);  
  87.     return c1*c;  
  88. }  
  89.   
  90.  
  91. Complex operator/(Complex &c1, Complex &c2)  
  92. {  
  93.     Complex c;  
  94.     c.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);  
  95.     c.imag=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);  
  96.     return c;  
  97. }  
  98. Complex operator/(double d1, Complex &c2)  
  99. {  
  100.     Complex c(d1,0);  
  101.     return c/c2;  
  102. }  
  103. Complex operator/(Complex &c1, double d2)  
  104. {  
  105.     Complex c(d2,0);  
  106.     return c1/c;  
  107. }  
  108.   
  109. void Complex::display()  
  110. {  
  111.     cout<<"("<<real<<","<<imag<<"i)"<<endl;  
  112. }  
  113.   
  114. int main()  
  115. {  
  116.     Complex c1(3,4),c2(5,-10),c3;  
  117.     double d=11;  
  118.     cout<<"c1=";  
  119.     c1.display();  
  120.     cout<<"c2=";  
  121.     c2.display();  
  122.     cout<<"d="<<d<<endl<<endl;  
  123.     cout<<"下面是重载运算符的计算结果: "<<endl;  
  124.     c3=c1+c2;  
  125.     cout<<"c1+c2=";  
  126.     c3.display();  
  127.     cout<<"c1+d=";  
  128.     (c1+d).display();  
  129.     cout<<"d+c1=";  
  130.     (d+c1).display();  
  131.     c3=c1-c2;  
  132.     cout<<"c1-c2=";  
  133.     c3.display();  
  134.     cout<<"c1-d=";  
  135.     (c1-d).display();  
  136.     cout<<"d-c1=";  
  137.     (d-c1).display();  
  138.     c3=c1*c2;  
  139.     cout<<"c1*c2=";  
  140.     c3.display();  
  141.     cout<<"c1*d=";  
  142.     (c1*d).display();  
  143.     cout<<"d*c1=";  
  144.     (d*c1).display();  
  145.     c3=c1/c2;  
  146.     cout<<"c1/c2=";  
  147.     c3.display();  
  148.     cout<<"c1/d=";  
  149.     (c1/d).display();  
  150.     cout<<"d/c1=";  
  151.     (d/c1).display();  
  152.     return 0;  
  153. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值