第八周实验报告3

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称: 复数类中的运算符重载 
* 作 者: 于宸 
* 完成日期: 2012 年 04 月 09日
* 版 本 号: V.1.0

* 对任务及求解方法的描述部分 
* 问题描述:实现复数类中的运算符重载,定义一个复数类重载运算符+、-、*、/,使之能用于复数的加减乘除。

在方案二的基础上,扩展+、-、*、/运算符的功能,使之能与double型数据进行运算。设Complex c; double d; c?d和d?c的结果为将d视为实部为d的复数同c运算的结果(其中?为+、-、*、/之一)。另外,定义一目运算符-,-c相当于0-c。
* 程序头部的注释结束*/

 
 
[cpp]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class Complex  
  5. {  
  6. public:  
  7.     Complex(){real = 0;imag = 0;}  
  8.     Complex(double r,double i){real = r;imag = i;}  
  9.     friend Complex operator + (Complex &c1,Complex &c2);//复数加法  
  10.     friend Complex operator - (Complex &c1,Complex &c2);//复数减法  
  11.     friend Complex operator * (Complex &c1,Complex &c2);//复数乘法  
  12.     friend Complex operator / (Complex &c1,Complex &c2);//复数除法  
  13.     friend Complex operator + (Complex &c1,const double &i);//复数加非复数  
  14.     friend Complex operator + (const double &i,Complex &c1);//非复数加复数  
  15.     friend Complex operator - (Complex &c1);//定义一目运算符“-”  
  16.     void display();  
  17. private:  
  18.     double real;  
  19.     double imag;  
  20. };  
  21.   
  22. //下面定义成员函数  
  23. void Complex::display()  
  24. {  
  25.     cout << "(" << real << ","  << imag << "i)" << endl;  
  26. }  
  27. //复数加法  
  28. Complex operator + (Complex &c1,Complex &c2)  
  29.  {  
  30.      return Complex(c1.real + c2.real,c1.imag + c2.imag);  
  31.  }  
  32. //复数减法  
  33. Complex operator - (Complex &c1,Complex &c2)  
  34.  {  
  35.      return Complex(c1.real - c2.real,c1.imag - c2.imag);  
  36.  }  
  37. //复数乘法  
  38. Complex operator * (Complex &c1,Complex &c2)  
  39.  {  
  40.      return Complex(c1.real * c2.real - c1.imag * c2.imag,c1.imag * c2.real + c1.real * c2.imag);  
  41.  }  
  42. //复数除法  
  43. Complex operator /  (Complex &c1,Complex &c2)  
  44.  {  
  45.      return Complex((c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),   
  46.     (c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));    
  47.   
  48.  }  
  49. //复数与double型相加  
  50. Complex operator+(Complex &c1,const double &i)    
  51. {    
  52.     return Complex(c1.real + i,c1.imag);    
  53. }    
  54. //double型与复数相加  
  55. Complex operator+(const double &i,Complex &c1)    
  56. {    
  57.     return Complex(c1.real + i,c1.imag);    
  58. }    
  59. //定义一目运算符“-”  
  60. Complex operator - (Complex &c1)  
  61. {  
  62.     return Complex(-c1.real,-c1.imag);  
  63. }  
  64.   
  65.  void main()  
  66.  {  
  67.      Complex c1(3,4),c2(5,-10),c3;  
  68.      cout<<"c1 = ";    
  69.      c1.display();    
  70.      cout<<"c2 = ";    
  71.      c2.display();    
  72.      c3 = c1 + c2;  
  73.      cout << "c1 + c2 = ";  
  74.      c3.display();  
  75.      c3 = c1 - c2;  
  76.      cout << "c1 - c2 = ";  
  77.      c3.display();  
  78.      c3 = c1 * c2;   
  79.      cout << "c1 * c2 = ";  
  80.      c3.display();  
  81.      c3 = c1 / c2;  
  82.      cout << "c1 / c2 = ";  
  83.      c3.display();  
  84.      c3 = c1 + 1.11;  
  85.      cout << "c1 + 1.11 = ";  
  86.      c3.display();  
  87.      c3 = 1.11 + c1;  
  88.      cout << "1.11 + c1 = ";  
  89.      c3.display();  
  90.      c3 = -c1;  
  91.      cout<< "-c1 = ";  
  92.      c3.display();  
  93.      system("pause");  
  94.  }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值