Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. class Complex
  8. {
  9. private:
  10.     double real, imag;
  11.  
  12. public:
  13.     Complex(double, double);
  14.     void print();
  15.  
  16.     double abs();
  17.  
  18.     friend Complex add(Complex, Complex);
  19.     friend Complex sub(Complex, Complex);
  20.     friend Complex mul(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);
  48. }
  49.  
  50. Complex mul(Complex c1, Complex c2)
  51. {
  52.  
  53. }
  54.  
  55. int main()
  56. {
  57.     double r, i;
  58.  
  59.     cout << "Enter the real and imaginary parts of the first complex number: ";
  60.     cin >> r >> i;
  61.  
  62.     Complex c1(r, i);
  63.     cout << "You have entered the following complex number: ";
  64.     c1.print();
  65.     cout << "Its absolute value is " << c1.abs() << endl << endl;
  66.  
  67.     cout << "Enter the real and imaginary parts of the second complex number: ";
  68.     cin >> r >> i;
  69.     Complex c2(r, i);
  70.     cout << "You have entered the following complex number: ";
  71.     c2.print();
  72.     cout << "Its absolute value is " << c2.abs() << endl << endl;
  73.  
  74.     Complex c3 = add(c1, c2);
  75.     cout << "Their sum is: ";
  76.     c3.print();
  77.     cout << "The absolute value of the sum is " << c3.abs() << endl << endl;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement