Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. class Complex
  7. {
  8. private:
  9. double real, imag;
  10.  
  11. public:
  12. Complex(double, double);
  13. void print();
  14.  
  15. double abs();
  16.  
  17. friend Complex add(Complex, Complex);
  18. friend Complex sub(Complex, Complex);
  19. friend Complex mul(Complex, Complex);
  20. friend Complex div(Complex, Complex);
  21. };
  22.  
  23. Complex::Complex(double real, double imag)
  24. {
  25. this->real = real;
  26. this->imag = imag;
  27. }
  28.  
  29. void Complex::print()
  30. {
  31. cout << real << " + " << imag << "*i" << endl;
  32. }
  33.  
  34. double Complex::abs()
  35. {
  36. return(sqrt(real*real + imag * imag));
  37. }
  38.  
  39. Complex add(Complex c1, Complex c2)
  40. {
  41. Complex c3(c1.real + c2.real, c1.imag + c2.imag);
  42. return c3;
  43. }
  44.  
  45. Complex sub(Complex c1, Complex c2)
  46. {
  47. Complex c3(c1.real - c2.real, c1.imag - c2.imag);
  48. return c3;
  49. }
  50.  
  51. Complex mul(Complex c1, Complex c2)
  52. {
  53. Complex c3(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real);
  54. return c3;
  55. }
  56.  
  57. Complex div(Complex c1, Complex c2)
  58. {
  59. double c = c2.real * c2.real + c2.imag * c2.imag;
  60.  
  61. Complex c3((c1.real * c2.real + c1.imag * c2.imag) / c, (c1.imag * c2.real - c1.real * c2.imag) / c);
  62. return c3;
  63. }
  64.  
  65. int main()
  66. {
  67. double r, i;
  68.  
  69. cout << "Enter the real and imaginary parts of the first complex number: ";
  70. cin >> r >> i;
  71.  
  72. Complex c1(r, i);
  73. cout << "You have entered the following complex number: ";
  74. c1.print();
  75. cout << "Its absolute value is " << c1.abs() << endl << endl;
  76.  
  77. cout << "Enter the real and imaginary parts of the second complex number: ";
  78. cin >> r >> i;
  79. Complex c2(r, i);
  80. cout << "You have entered the following complex number: ";
  81. c2.print();
  82. cout << "Its absolute value is " << c2.abs() << endl << endl;
  83.  
  84. Complex c3 = add(c1, c2);
  85. cout << "Their sum is: ";
  86. c3.print();
  87. cout << "The absolute value of the sum is " << c3.abs() << endl << endl;
  88.  
  89. Complex c4 = sub(c1, c2);
  90. cout << "Their difference is: ";
  91. c4.print();
  92. cout << "The absolute value of the sum is " << c4.abs() << endl << endl;
  93.  
  94. Complex c5 = mul(c1, c2);
  95. cout << "Their product is: ";
  96. c5.print();
  97. cout << "The absolute value of the product is " << c5.abs() << endl << endl;
  98.  
  99. Complex c6 = div(c1, c2);
  100. cout << "Their quotient is: ";
  101. c6.print();
  102. cout << "The absolute value of the quotient is " << c6.abs() << endl << endl;
  103.  
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement