第八周实验报告1

来源:互联网 发布:淘宝申请部分金额退款 编辑:程序博客网 时间:2024/05/14 08:52

/* (程序头部注释开始)

* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生

* All rights reserved.

* 文件名称: 实现复数类中的运算符重载

* 作 者:    于宸

* 完成日期: 2012 年04 月08 日

* 版 本 号:1.0

* 对任务及求解方法的描述部分

* 输入描述:

* 问题描述:定义一个复数类重载运算符+、-、*、/,使之能用于复数的加减乘除。

* 程序输出:

* 程序头部的注释结束

*/

[html] view plaincopy
  1. #include<iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. class Complex  
  6. {  
  7. public:  
  8.   
  9.     Complex(){real = 0;imag = 0;}  
  10.   
  11.     Complex(double r, double i){real = rimag =i;}  
  12.   
  13.     Complex operator+(Complex &c2);//声明重载运算符"+"  
  14.   
  15.     Complex operator-(Complex &c2);//声明重载运算符"-"  
  16.   
  17.     Complex operator*(Complex &c2);//声明重载运算符"*"  
  18.   
  19.     Complex operator/(Complex &c2);//声明重载运算符"/"  
  20.   
  21.     void display();  
  22.   
  23. private:  
  24.   
  25.     double real;  
  26.   
  27.     double imag;  
  28. };  
  29.   
  30. Complex Complex::operator+(Complex &c2)//定义重载运算符"+"  
  31. {  
  32.     Complex c;  
  33.   
  34.     c.real = real + c2.real;  
  35.   
  36.     c.imag = imag + c2.imag;  
  37.   
  38.     return c;  
  39. }  
  40.   
  41. Complex Complex::operator-(Complex &c2)//定义重载运算符"-"  
  42. {  
  43.     Complex c;  
  44.   
  45.     c.real = real - c2.real;  
  46.   
  47.     c.imag = imag - c2.imag;  
  48.   
  49.     return c;  
  50. }  
  51.   
  52. Complex Complex::operator*(Complex &c2)//定义重载运算符"*"  
  53. {  
  54.     Complex c;  
  55.   
  56.     c.real = real * c2.real;  
  57.   
  58.     c.imag = imag * c2.imag;  
  59.   
  60.     return c;  
  61. }  
  62.   
  63. Complex Complex::operator/(Complex &c2)//定义重载运算符"/"  
  64. {  
  65.     Complex c;  
  66.   
  67.     c.real = real / c2.real;  
  68.   
  69.     c.imag = imag / c2.imag;  
  70.   
  71.     return c;  
  72. }  
  73.   
  74. void Complex::display()  
  75. {  
  76.     cout << "(" << real << "," << imag << "i)" << endl;  
  77. }  
  78.   
  79. int main()  
  80. {  
  81.     Complex c1(3,4),c2(5,-10),c3;  
  82.   
  83.     cout<<"c1=";  
  84.   
  85.     c1.display();  
  86.   
  87.     cout<<"c2=";  
  88.   
  89.     c2.display();  
  90.   
  91.     c3=c1+c2;  
  92.   
  93.     cout<<"c1+c2=";  
  94.   
  95.     c3.display();  
  96.   
  97.     c3=c1-c2;  
  98.   
  99.     cout<<"c1-c2=";  
  100.   
  101.     c3.display();  
  102.   
  103.     c3=c1*c2;  
  104.   
  105.     cout<<"c1*c2=";  
  106.   
  107.     c3.display();  
  108.   
  109.     c3=c1/c2;  
  110.   
  111.     cout<<"c1/c2=";  
  112.   
  113.     c3.display();  
  114.   
  115.     system("pause");  
  116.   
  117.     return 0;  
  118. }  
  119.   
  120.   
  121. #include<iostream.h>  
  122.   
  123. //using namespace std;  
  124.   
  125. class Complex  
  126. {  
  127. public:  
  128.   
  129.     Complex(){real = 0;imag = 0;}  
  130.   
  131.     Complex(double r, double i){real = rimag =i;}  
  132.   
  133.     friend Complex operator+(Complex &c1, Complex &c2);  
  134.   
  135.     friend Complex operator-(Complex &c1, Complex &c2);  
  136.   
  137.     friend Complex operator*(Complex &c1, Complex &c2);  
  138.   
  139.     friend Complex operator/(Complex &c1, Complex &c2);  
  140.   
  141.     void display();  
  142.   
  143. private:  
  144.   
  145.     double real;  
  146.   
  147.     double imag;  
  148. };  
  149.   
  150. Complex operator+(Complex &c1, Complex &c2)  
  151. {  
  152.     return Complex(c1.real + c2.real, c1.imag + c2.imag);  
  153. }  
  154.   
  155. Complex operator-(Complex &c1, Complex &c2)  
  156. {  
  157.     return Complex(c1.real - c2.real, c1.imag - c2.imag);  
  158. }  
  159.   
  160. Complex operator*(Complex &c1, Complex &c2)  
  161. {  
  162.     return Complex(c1.real * c2.real, c1.imag * c2.imag);  
  163. }  
  164.   
  165. Complex operator/(Complex &c1, Complex &c2)  
  166. {  
  167.     return Complex(c1.real / c2.real, c1.imag / c2.imag);  
  168. }  
  169.   
  170. void Complex::display()  
  171. {  
  172.     cout << "(" << real << "," << imag << "i)" << endl;  
  173. }  
  174.   
  175. int main()  
  176. {  
  177.     Complex c1(3,4),c2(5,-10),c3;  
  178.   
  179.     cout<<"c1=";  
  180.   
  181.     c1.display();  
  182.   
  183.     cout<<"c2=";  
  184.   
  185.     c2.display();  
  186.   
  187.     c3=c1+c2;  
  188.   
  189.     cout<<"c1+c2=";  
  190.   
  191.     c3.display();  
  192.   
  193.     c3=c1-c2;  
  194.   
  195.     cout<<"c1-c2=";  
  196.   
  197.     c3.display();  
  198.   
  199.     c3=c1*c2;  
  200.   
  201.     cout<<"c1*c2=";  
  202.   
  203.     c3.display();  
  204.   
  205.     c3=c1/c2;  
  206.   
  207.     cout<<"c1/c2=";  
  208.   
  209.     c3.display();  
  210.   
  211.     return 0;  
  212. }  
  213.   
  214.   
  215. #include<iostream.h>  
  216.   
  217. //using namespace std;  
  218.   
  219. class Complex  
  220. {  
  221. public:  
  222.   
  223.     Complex(){real = 0;imag = 0;}  
  224.   
  225.     Complex(double r, double i){real = rimag =i;}  
  226.   
  227.     friend Complex operator +(Complex &c, int a);  
  228.   
  229.     friend Complex operator -(Complex &c, int a);  
  230.   
  231.     friend Complex operator *(Complex &c, int a);  
  232.   
  233.     friend Complex operator /(Complex &c, int a);  
  234.   
  235.     void display();  
  236.   
  237. private:  
  238.   
  239.     double real;  
  240.   
  241.     double imag;  
  242. };  
  243.   
  244. Complex operator +(Complex &c, int a)  
  245. {  
  246.     return Complex(c.real + a, c.imag);  
  247. }  
  248.   
  249. Complex operator -(Complex &c, int a)  
  250. {  
  251.     return Complex(c.real - a, c.imag);  
  252. }  
  253.   
  254. Complex operator*(Complex &c, int a)  
  255. {  
  256.     return Complex(a * c.real, a * c.imag);  
  257. }  
  258.   
  259. Complex operator/(Complex &c, int a)  
  260. {  
  261.     return Complex(c.real / a, c.imag / a);  
  262. }  
  263.   
  264. void Complex::display()  
  265. {  
  266.     cout << "(" << real << "," << imag << "i)" << endl;  
  267. }  
  268.   
  269. int main()  
  270. {  
  271.     Complex c1(4.0, 5.0), c2(2.0, 2.0), c3;  
  272.   
  273.     int a = 2;  
  274.   
  275.     cout << "c1 = ";  
  276.   
  277.     c1.display();  
  278.   
  279.     cout << "c2 = ";  
  280.   
  281.     c2.display();  
  282.   
  283.     c3 = c2 + 2;  
  284.   
  285.     cout<< "c2 + 2 = ";  
  286.       
  287.     c3.display();  
  288.   
  289.     c3 = c2 - 2;  
  290.   
  291.     cout<< "c2 - 2 = ";  
  292.   
  293.     c3.display();  
  294.   
  295.     c3 = c2 * 2;  
  296.   
  297.     cout<< "c2 * 2 = ";  
  298.       
  299.     c3.display();  
  300.   
  301.     c3 = c2 / 2;  
  302.   
  303.     cout<< "c2 / 2 = ";  
  304.       
  305.     c3.display();  
  306.   
  307.     return 0;  
  308. }  

原创粉丝点击