daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Complex {
  5. private:
  6.     double re, im;
  7. public:
  8.     // ham tao thiet lap tu hai so
  9.     Complex(double r = 0, double i = 0) : re(r) , im(i) { }
  10.     // ham copy tu mot so phuc
  11.     Complex (Complex &C): re(C.re), im(C.im) { }
  12. public:
  13.     Complex operator + (Complex c);
  14.     Complex operator - (Complex c);
  15.     Complex operator * (Complex c);
  16.     Complex operator / (Complex c);
  17. public:
  18.     friend ostream& operator << (ostream& out, Complex &C) {
  19.         return (out << '(' << C.re << ", " << C.im << "i) " );
  20.     }
  21. };
  22. // Thiet ke cac toan tu cong, tru, nhan, chia
  23. Complex Complex:: operator + (Complex C)
  24. {
  25.     return Complex(this->re + C.re, this -> im + C.im );
  26. }
  27. Complex Complex:: operator - (Complex C)
  28. {
  29.     return Complex(this -> re  C.re, this -> im - C.im );
  30. }
  31. Complex Complex:: operator * (Complex C)
  32. {
  33.     return Complex(this -> re * C.re - this -> im * C.im, this -> re * C.im + this -> im * C.re);
  34. }
  35. Complex Complex:: operator / (Complex C)
  36. {
  37.     return Complex((this -> re * C.re + this -> im * C.im)/(this -> re * this -> re + this -> im * this -> im), (this->re * C.im - this->im * C.re)/(this -> re * this -> re + this -> im * this -> im));
  38. }
  39.  
  40. int main() {
  41.     // khai bao bien
  42.     double a = 2, y, z;
  43.     y = Complex::Complex(1,2);
  44.     z = Complex::Complex(0,6);
  45.     // tinh thu
  46.     cout << y << " + " << z << = << y + z + a << endl;
  47.     cout << y << " - " << z << = << y - z << endl;
  48.     cout << y << " * " << z << = << y * z << endl;
  49.     cout << y << " / " << z << = << y / z << endl;
  50.     system("pause");
  51.     return 0;
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top