Advertisement
Guest User

Untitled

a guest
Oct 9th, 2013
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Complex
  5. {
  6.   public:
  7.     Complex& operator+=(const Complex& rhs)
  8.     {
  9.         this->r += rhs.r;
  10.         this->i += rhs.i;
  11.         return *this;
  12.     }
  13.  
  14.     Complex& operator-=(const Complex& rhs)
  15.     {
  16.         this->r -= rhs.r;
  17.         this->i -= rhs.i;
  18.         return *this;
  19.     }
  20.  
  21.     Complex& operator*=(const Complex& rhs)
  22.     {
  23.         this->r = (this->r * rhs.r) - (this->i * rhs.i);
  24.         this->i = (this->r * rhs.i) + (this->i * rhs.r);
  25.         return *this;
  26.     }
  27.  
  28.     Complex() : r(0), i(0) {};
  29.     Complex(int r_) : r(r_), i(0) {}
  30.     Complex(int r_, int i_) : r(r_), i(i_) {}
  31.     Complex(const Complex& rhs) : r(rhs.r), i(rhs.i) {}
  32.  
  33.     friend std::ostream& operator<<(std::ostream& lhs, const Complex& rhs);
  34.     friend bool operator == (const Complex& lhs, const Complex& rhs);
  35.   private:
  36.     int r;
  37.     int i;
  38. };
  39.  
  40. std::ostream& operator<<(std::ostream& lhs, const Complex& rhs)
  41. {
  42.     return lhs << '(' << rhs.r << " + " << rhs.i << "i)";
  43. }
  44.  
  45. bool operator == (const Complex& lhs, const Complex& rhs)
  46. {
  47.     return lhs.r == rhs.r && lhs.i == rhs.i;
  48. }
  49.  
  50. Complex operator+(const Complex& lhs, const Complex& rhs)
  51. {
  52.     Complex temp(lhs);
  53.     temp += rhs;
  54.     return temp;
  55. }
  56.  
  57. Complex operator-(const Complex& lhs, const Complex& rhs)
  58. {
  59.     Complex temp(lhs);
  60.     temp -= rhs;
  61.     return temp;
  62. }
  63.  
  64. Complex operator*(const Complex& lhs, const Complex& rhs)
  65. {
  66.     Complex temp(lhs);
  67.     temp *= rhs;
  68.     return temp;
  69. }
  70.  
  71.  
  72. int main()
  73. {
  74.     Complex first;
  75.     Complex second(-4);
  76.     Complex third(1,-2);
  77.     Complex fourth(-1,2);
  78.     Complex fifth(3,0);
  79.     Complex sixth(0,3);
  80.     Complex seventh(5,3);
  81.     Complex eighth(6,10);
  82.     Complex ninth(7,4);
  83.     Complex tenth(-14,8);
  84.  
  85.     cout << endl;
  86.     if (third == fourth)
  87.       cout << "The complex numbers are equal." << endl;
  88.     else
  89.       cout << "The complex numbers are not equal." << endl;
  90.     if (first == first)
  91.       cout << "The complex numbers are equal." << endl;
  92.     else
  93.       cout << "The complex numbers are not equal." << endl;
  94.     if (first == third + fourth)
  95.       cout << "The complex numbers are equal." << endl;
  96.     else
  97.       cout << "The complex numbers are not equal." << endl;
  98.     cout << endl;
  99.     cout << first << " + " << second << " = " << first + second << endl;
  100.     cout << first << " - " << second << " = " << first - second << endl;
  101.     cout << first << " * " << second << " = " << first * second << endl;
  102. //    cout <<  "-" << second << " = " << -second << endl;
  103.  
  104.     cout << endl;
  105.     cout << third << " + " << fourth << " = " << third + fourth << endl;
  106.     cout << third << " - " << fourth << " = " << third - fourth << endl;
  107.     cout << third << " * " << fourth << " = " << third * fourth << endl;
  108. //    cout << "-" << third << " = " << -third << endl;
  109.  
  110.     cout << endl;
  111.     cout << fifth << " + " << sixth << " = " << fifth + sixth << endl;
  112.     cout << fifth << " - " << sixth << " = " << fifth - sixth << endl;
  113.     cout << fifth << " * " << sixth << " = " << fifth * sixth << endl;
  114. //    cout << "-" << fifth - sixth << " = " << -(fifth - sixth) << endl;
  115.  
  116.     cout << endl;
  117.     cout << seventh << " + " << eighth << " = " << seventh + eighth << endl;
  118.     cout << seventh << " - " << eighth << " = " << seventh - eighth << endl;
  119.     cout << seventh << " * " << eighth << " = " << seventh * eighth << endl;
  120. //    cout << "-" << seventh * eighth << " = " << -(seventh * eighth) << endl;
  121.  
  122.     cout << endl;
  123.     cout << ninth << " + " << tenth << " = " << ninth + tenth << endl;
  124.     cout << ninth << " - " << tenth << " = " << ninth - tenth << endl;
  125.     cout << ninth << " * " << tenth << " = " << ninth * tenth << endl;
  126. //    cout << "-" << ninth + tenth << " = " << -(ninth + eighth) << endl;
  127.     cout << endl;
  128.  
  129.     system("pause");
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement