Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Complex
- {
- public:
- Complex& operator+=(const Complex& rhs)
- {
- this->r += rhs.r;
- this->i += rhs.i;
- return *this;
- }
- Complex& operator-=(const Complex& rhs)
- {
- this->r -= rhs.r;
- this->i -= rhs.i;
- return *this;
- }
- Complex& operator*=(const Complex& rhs)
- {
- this->r = (this->r * rhs.r) - (this->i * rhs.i);
- this->i = (this->r * rhs.i) + (this->i * rhs.r);
- return *this;
- }
- Complex() : r(0), i(0) {};
- Complex(int r_) : r(r_), i(0) {}
- Complex(int r_, int i_) : r(r_), i(i_) {}
- Complex(const Complex& rhs) : r(rhs.r), i(rhs.i) {}
- friend std::ostream& operator<<(std::ostream& lhs, const Complex& rhs);
- friend bool operator == (const Complex& lhs, const Complex& rhs);
- private:
- int r;
- int i;
- };
- std::ostream& operator<<(std::ostream& lhs, const Complex& rhs)
- {
- return lhs << '(' << rhs.r << " + " << rhs.i << "i)";
- }
- bool operator == (const Complex& lhs, const Complex& rhs)
- {
- return lhs.r == rhs.r && lhs.i == rhs.i;
- }
- Complex operator+(const Complex& lhs, const Complex& rhs)
- {
- Complex temp(lhs);
- temp += rhs;
- return temp;
- }
- Complex operator-(const Complex& lhs, const Complex& rhs)
- {
- Complex temp(lhs);
- temp -= rhs;
- return temp;
- }
- Complex operator*(const Complex& lhs, const Complex& rhs)
- {
- Complex temp(lhs);
- temp *= rhs;
- return temp;
- }
- int main()
- {
- Complex first;
- Complex second(-4);
- Complex third(1,-2);
- Complex fourth(-1,2);
- Complex fifth(3,0);
- Complex sixth(0,3);
- Complex seventh(5,3);
- Complex eighth(6,10);
- Complex ninth(7,4);
- Complex tenth(-14,8);
- cout << endl;
- if (third == fourth)
- cout << "The complex numbers are equal." << endl;
- else
- cout << "The complex numbers are not equal." << endl;
- if (first == first)
- cout << "The complex numbers are equal." << endl;
- else
- cout << "The complex numbers are not equal." << endl;
- if (first == third + fourth)
- cout << "The complex numbers are equal." << endl;
- else
- cout << "The complex numbers are not equal." << endl;
- cout << endl;
- cout << first << " + " << second << " = " << first + second << endl;
- cout << first << " - " << second << " = " << first - second << endl;
- cout << first << " * " << second << " = " << first * second << endl;
- // cout << "-" << second << " = " << -second << endl;
- cout << endl;
- cout << third << " + " << fourth << " = " << third + fourth << endl;
- cout << third << " - " << fourth << " = " << third - fourth << endl;
- cout << third << " * " << fourth << " = " << third * fourth << endl;
- // cout << "-" << third << " = " << -third << endl;
- cout << endl;
- cout << fifth << " + " << sixth << " = " << fifth + sixth << endl;
- cout << fifth << " - " << sixth << " = " << fifth - sixth << endl;
- cout << fifth << " * " << sixth << " = " << fifth * sixth << endl;
- // cout << "-" << fifth - sixth << " = " << -(fifth - sixth) << endl;
- cout << endl;
- cout << seventh << " + " << eighth << " = " << seventh + eighth << endl;
- cout << seventh << " - " << eighth << " = " << seventh - eighth << endl;
- cout << seventh << " * " << eighth << " = " << seventh * eighth << endl;
- // cout << "-" << seventh * eighth << " = " << -(seventh * eighth) << endl;
- cout << endl;
- cout << ninth << " + " << tenth << " = " << ninth + tenth << endl;
- cout << ninth << " - " << tenth << " = " << ninth - tenth << endl;
- cout << ninth << " * " << tenth << " = " << ninth * tenth << endl;
- // cout << "-" << ninth + tenth << " = " << -(ninth + eighth) << endl;
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement