Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include<fstream>
  3. using namespace std;
  4. class Complex {
  5. public:
  6. Complex(int a, int b);
  7. Complex();
  8. friend ostream&operator<<(ostream&os, const Complex & z);
  9. friend istream&operator>>(istream&is, Complex & z);
  10. Complex operator+(const Complex&z);
  11. Complex operator-(const Complex&z);
  12. Complex operator*(const Complex&z);
  13. Complex operator/(const Complex&z);
  14. private:
  15. int a;
  16. int b;
  17. };
  18. Complex::Complex(int a, int b) {
  19. this->a = a;
  20. this->b = b;
  21. };
  22. Complex::Complex() {
  23. this->a = 0;
  24. this->b = 0;
  25. };
  26. Complex Complex::operator+(const Complex&z) {
  27. Complex zz;
  28. zz.a = a + z.a;
  29. zz.b = b + z.b;
  30. return zz;
  31. }
  32.  
  33. Complex Complex::operator*(const Complex&z) {
  34. Complex zz;
  35. //(a+ib)(c+id)=ac+iad+ibc-bd=(ac-bd)+i(ad+bc)
  36. zz.a =(a*z.a)-(b*z.b) ;
  37. zz.b = (a*z.b)+(b*z.a);
  38. return zz;
  39. }
  40. Complex Complex::operator/(const Complex&z) {
  41. Complex zz;
  42. //(a+ib)/(c+id)=((a+ib)(c-id))/(c*c+d*d)=(ac+bd)/(c^2+d^2)+i(bc-ad)/(c^2+d^2);
  43. zz.a = (a*z.a+b*z.b)/(z.a*z.a+z.b*z.b);
  44. zz.b = (b*z.a-a*z.b)/ (z.a*z.a + z.b*z.b);
  45. return zz;
  46. }
  47. Complex Complex::operator-(const Complex&z) {
  48. Complex zz;
  49. zz.a = a - z.a;
  50. zz.b = b - z.b;
  51. return zz;
  52. }
  53.  
  54. istream&operator>>(istream&is, Complex & z) {
  55. cout << "z=a+i*b" << "\nEnter a:";
  56. is >> z.a;
  57. cout << "\nEnter b:";
  58. is >> z.b;
  59. system("cls");
  60. return is;
  61. };
  62. ostream&operator<<(ostream&os, const Complex & z) {
  63. if (z.b > 0) {
  64. os << z.a << "+" << z.b<<"i"<< endl;
  65. }
  66. else {
  67. os << z.a << "-" << abs(z.b) <<"i"<< endl;
  68. }
  69. return os;
  70. };
  71.  
  72.  
  73. int main()
  74. {
  75. Complex z1;
  76. Complex z2;
  77. cin >> z1;
  78. cin >> z2;
  79. cout << z1;
  80. cout << z2;
  81. Complex z3 = z1 + z2;
  82. cout <<"+ "<< z3;
  83. z3 = z1 - z2;
  84. cout <<"- "<< z3;
  85. z3 = z1 * z2;
  86. cout <<"* "<< z3;
  87. z3 = z1 / z2;
  88. cout <<"/ "<< z3;
  89. return 0;
  90. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement