minh_tran_782

Complex Number LTNC

Feb 9th, 2022 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. #include <iostream> //
  2. #include <string>
  3. #include <math.h>
  4. using namespace std; //
  5. class Complex{
  6. protected:
  7.     double real;
  8.     double img;
  9. public:
  10.     Complex(double real,double img)
  11.     {
  12.         this->real = real;
  13.         this->img = img;
  14.     }
  15.     Complex()
  16.     {
  17.         this->real = 0.0;
  18.         this->img = 0.0;
  19.     }
  20.     Complex operator+(const Complex& rhs)
  21.     {
  22.         Complex result;
  23.         result.real = this->real + rhs.real;
  24.         result.img = this->img + rhs.img;
  25.         return result;
  26.     }
  27.      Complex operator+(double realNum)
  28.     {
  29.         Complex result;
  30.         result.real = this->real + realNum;
  31.         result.img = this->img;
  32.         return result;
  33.     }
  34.     Complex operator-(const Complex& rhs)
  35.     {
  36.         Complex result;
  37.         result.real = this->real - rhs.real;
  38.         result.img = this->img - rhs.img;
  39.         return result;
  40.     }
  41.      Complex operator-(double rhs)
  42.     {
  43.         Complex result;
  44.         result.real = this->real - rhs;
  45.         result.img = this->img;
  46.         return result;
  47.     }
  48.     Complex operator*(const Complex& rhs)
  49.     {
  50.         Complex result;
  51.         result.real = this->real * rhs.real * 1.0 - this->img * rhs.img * 1.0;
  52.         result.img = this->real * rhs.img * 1.0 + this->img * rhs.real * 1.0;
  53.         return result;  
  54.     }
  55.     Complex operator * (double rhs)
  56.     {
  57.         Complex result;
  58.         result.real = this->real * rhs * 1.0;
  59.         result.img = this->img * rhs * 1.0;
  60.         return result;  
  61.     }
  62.     Complex operator / (const Complex& rhs)
  63.     {
  64.         if (rhs.real == 0 && rhs.img == 0)
  65.         {
  66.             throw ("Error! Divide by zero\n");
  67.         }
  68.         Complex result;
  69.         result.real = this->real * rhs.real * 1.0 - this->img * rhs.img * 1.0;
  70.         result.real = 1.0 * result.real / (rhs.real * rhs.real + rhs.img * rhs.img);
  71.         result.img = this->real * rhs.img * 1.0 + this->img * rhs.real * 1.0;
  72.         result.img = 1.0 * result.img / (rhs.real * rhs.real + rhs.img * rhs.img);
  73.         return result;  
  74.     }
  75.     Complex operator / (double rhs)
  76.     {
  77.         Complex result;
  78.         if (rhs == 0)
  79.         {
  80.             throw ("Error! Divide by zero\n");
  81.    
  82.         }
  83.         result.real = 1.0 * this->real / rhs;
  84.         result.img = 1.0 * this->img / rhs;
  85.         return result;
  86.     }
  87.     friend ostream& operator<<(ostream& os, const Complex& rhs)
  88.     {
  89.         if (rhs.img > 0) os << rhs.real << " +" << rhs.img << "i";
  90.         else os << rhs.real << " -" << -rhs.img << "i";
  91.         return os;
  92.     }
  93.       friend istream& operator>>(istream& in,Complex& rhs)
  94.     {
  95.        cout << "Nhap phan thuc \n";
  96.        in >> rhs.real;
  97.        cout << "Nhap phan ao\n";
  98.        in >> rhs.img;
  99.        return in;
  100.     }
  101.     friend void ptb2(double a, double b, double c);
  102. };
  103.  
  104. void ptb2(double a = 0, double b = 0, double c = 0)
  105.     {
  106.         if (a == 0)
  107.         {
  108.             if (b == 0)
  109.             {
  110.                 if (c != 0) cout << "Phuong trinh vo nghiem\n";
  111.                 else cout << "Phuong trinh co mot nghiem x = 0\n";  
  112.                 return;
  113.             }
  114.             else
  115.             {
  116.                 double res = -1.0*c/b;
  117.                 cout << "Phuong trinh co mot nghiem la x = " << res << endl;
  118.                 return;
  119.             }
  120.         }      
  121.         double delta = b*b - 4 * a *c;
  122.         if (delta == 0)
  123.         {
  124.             double res = -1.0*b/(2*a);
  125.                cout << "Phuong trinh co mot nghiem la x = " << res << endl;
  126.                 return;
  127.         }
  128.         if (delta > 0)
  129.         {
  130.             double res1 = -b + sqrt(delta);
  131.             res1 = 1.0*res1/(2*a);
  132.             double res2 = -b - sqrt(delta);
  133.             res2 = 1.0*res2/(2/a);
  134.             cout << "Phuong trinh co hai nghiem la x1 = " << res1 << ", x2 = " << res2 << endl;
  135.             return;  
  136.         }
  137.         if (delta < 0)
  138.         {
  139.             Complex res1;
  140.             res1.real = -b/(2*a);
  141.             res1.img = sqrt(-delta)/(2*a);
  142.             Complex res2;
  143.             res2.real = res1.real;
  144.             res2.img = -res1.img;
  145.        cout << "Phuong trinh co hai nghiem la x1 = " << res1 << ", x2 = " << res2 << endl;
  146.             // cout << "Phuong trinh co hai nghiem ao la x1 = " << res1.real << " +" << res1.img << "i"
  147.             // << ", x2 = " << res2.real << " +" << res2.img << endl;
  148.             return;  
  149.         }
  150.     }
  151. int main ()
  152. {
  153.     ptb2(1,2,2);
  154. }
Add Comment
Please, Sign In to add comment