第九周 任务一

一时失言乱红尘 2022-06-13 12:19 263阅读 0赞
  1. /*
  2. 实验内容:定义Complex类中的<<和>>运算符的重载,实现输入和输出。
  3. * 程序的版权和版本声明部分
  4. * Copyright (c) 2011, 烟台大学计算机学院学生
  5. * All rights reserved.
  6. * 文件名称: 定义Complex类中的<<和>>运算符的重载,实现输入和输出
  7. * 作 者: 薛广晨
  8. * 完成日期: 2012 年 4 月 14日
  9. * 版 本号: x1.0
  10. * 对任务及求解方法的描述部分
  11. * 输入描述:
  12. * 程序头部的注释结束(此处也删除了斜杠)
  13. */
  14. //【任务1】接第8周任务1,定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
  15. #include <iostream>
  16. using namespace std;
  17. class Complex
  18. {
  19. public:
  20. Complex(){real = 0; imag = 0;}
  21. Complex(double r){real = r;imag = 0;}//类型转换函数
  22. Complex(double r,double i){real = r; imag = i;}
  23. friend ostream& operator << (ostream &,Complex &);
  24. friend istream& operator >> (istream &,Complex &);
  25. friend Complex operator+ (Complex c1,Complex c2);
  26. friend Complex operator- (Complex c1, Complex c2);
  27. friend Complex operator- (Complex &c);
  28. friend Complex operator* (Complex c1, Complex c2);
  29. friend Complex operator/ (Complex c1, Complex c2);
  30. private:
  31. double real;
  32. double imag;
  33. };
  34. //下面定义成员函数
  35. ostream& operator << (ostream &ouput,Complex &c)
  36. {
  37. ouput << "(" << c.real;
  38. if(c.imag >= 0)ouput << "+";
  39. ouput << c.imag << "i)" << endl;
  40. return ouput;
  41. }
  42. istream& operator >> (istream &input, Complex &c)
  43. {
  44. char c1;
  45. cout << "input real part and imaginary part of complex number:";
  46. input >> c.real >> c.imag >> c1;
  47. return input;
  48. }
  49. Complex operator + (Complex c1, Complex c2)
  50. {return Complex(c1.real + c2.real, c1.imag + c2.imag);}
  51. Complex operator - (Complex c1, Complex c2)
  52. {return Complex(c1.real - c2.real, c1.imag - c2.imag);}
  53. Complex operator - (Complex &c)
  54. { return Complex(-c.real, -c.imag);}
  55. Complex operator * (Complex c1, Complex c2)
  56. {return Complex(c1.real * c2.real - c1.imag * c2.imag, c1.imag * c2.real + c1.real * c2.imag);}
  57. Complex operator / (Complex c1, Complex c2)
  58. {return Complex((c1.real * c2.real + c1.imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag), (c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag));}
  59. int main()
  60. {
  61. double d = 2.5;
  62. Complex c1, c2, c3,c4;
  63. cin >> c1;
  64. cout << "请再输入一个复数:" << endl;
  65. cin >> c2;
  66. cout << "c1 = " << c1;
  67. cout << "c2 = " << c2;
  68. c3 = c1 + c2;
  69. cout << "c1 + c2 = " << c3;
  70. c3 = c1 + d;
  71. cout << "c1 + " << d << "= " << c3;
  72. c3 = Complex(d) + c1;
  73. cout << d << " + c1" << "= " << c3;
  74. c3 = c1 - c2;
  75. cout << "c1 - c2 = " << c3;
  76. c3 = c1 - d;
  77. cout << "c1 - " << d << "= " << c3;
  78. c3 = d - c1;
  79. cout << d << " - c1" << "= " << c3;
  80. c3 = c1 * c2;
  81. cout << "c1 * c2 = " << c3;
  82. c3 = c1 * d;
  83. cout << "c1 * " << d << "= " << c3;
  84. c3 = d * c1;
  85. cout << d << " * c1" << "= " << c3;
  86. c3 = c1 / c2;
  87. cout << "c1 / c2 = " << c3;
  88. c3 = c1 / d;
  89. cout << "c1 / " << d << "= " << c3;
  90. c3 = d / c1;
  91. cout << d << " / c1" << "= " << c3;
  92. c3 = - c1;
  93. cout << " -c1= " << c3;
  94. system("pause");
  95. return 0;
  96. }

1334401637_9205.jpg
上机感言:定义Complex类中的<<和>>运算符的重载,实现输入和输出,使程序简单,可读性强

发表评论

表情:
评论列表 (有 0 条评论,263人围观)

还没有评论,来说两句吧...

相关阅读

    相关 任务

    / 实验内容:实现复数类中的运算符重载定义一个复数类重载运算符+、-、、/,使之能用于复数的加减乘除。 程序的版权和版本声明部分

    相关 任务

    //【任务1】下面的程序存在编译错误。有两种方法可以修改,请给出这两种修改方案,在报告中说明你倾向于用哪一种?为什么?处理此类问题的原则是什么? /实验目的:

    相关 任务

    /【任务1】设计三角形类,通过增加构造函数,使对象在定义时能够进行初始化 特别要求:为全面体会构造函数的各种写法,本任务要求提交多个版本的程序,体现出你已经掌