Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Complex {
- friend ostream &operator<<(ostream&, Complex&);
- friend istream &operator>>(istream&, Complex&);
- friend Complex &operator++(Complex&); // Prefix increment
- friend Complex &operator++(Complex&, int); // Postfix increment
- public:
- Complex(int = 0, int = 0);
- ~Complex();
- void cube(Complex&);
- bool operator==(const Complex &rhs) const;
- bool operator!=(const Complex &rhs) const;
- Complex operator+(const Complex &rhs)
- {
- float realSum = r + rhs.r;
- float imagSum = i + rhs.i;
- return Complex(realSum, imagSum);
- }
- Complex operator-(const Complex &rhs)
- {
- float realDif = r - rhs.r;
- float imagDif = i - rhs.i;
- return Complex(realDif, imagDif);
- }
- Complex operator*(const Complex &rhs)
- {
- float realProd = (r * rhs.r) + (-1 * i * rhs.i);
- float imagProd = (r * rhs.i) + (i * rhs.r);
- return Complex(realProd, imagProd);
- }
- private:
- float r, i;
- bool f;
- };
- Complex::Complex(int a, int b)
- : r(a), i(b) {}
- Complex::~Complex()
- {
- }
- void Complex::cube(Complex &a)
- {
- float tmp = a.r;
- float real1 = (a.r * a.r) + (-1 * a.i * a.i); //real part after FOIL
- float imag1 = (a.r * a.i) + (a.i * a.r); //imaginary part after FOIL
- a.r = (real1 * a.r) + (-1 * imag1 * a.i); //real part of the cube
- a.i = (real1 * a.i) + (imag1 * tmp); //imaginary part of the cube
- }
- Complex &operator++(Complex &a) //Pre
- {
- a.cube(a);
- return a;
- }
- Complex &operator++(Complex &a, int) //Post
- {
- Complex temp(a.r, a.i);
- a.cube(a);
- return temp;
- }
- ostream &operator<<(ostream &out, Complex &a)
- {
- if (a.i >= 0)
- {
- a.f = true;
- }
- else
- {
- a.f = false;
- }
- if (a.f)
- {
- out << "(" << a.r << " + " << a.i << "i)" << endl;
- }
- else
- {
- a.i *= -1;
- out << "(" << a.r << " - " << a.i << "i)" << endl;
- a.i *= -1;
- }
- return out;
- }
- istream &operator>>(istream &in, Complex &a)
- {
- cout << "Enter the real value: ";
- in >> a.r;
- cout << "Enter the imgaginary value: ";
- in >> a.i;
- cout << "Original values are " << a.r << " and " << a.i << endl;
- if (a.i >= 0)
- {
- a.f = true;
- }
- else
- {
- a.f = false;
- }
- if (a.f)
- {
- cout << "Equation is: (" << a.r << " + " << a.i << "i)" << endl << endl;
- }
- else
- {
- a.i *= -1;
- cout << "Equation is: (" << a.r << " - " << a.i << "i)" << endl << endl;
- a.i *= -1;
- }
- return in;
- }
- bool Complex::operator==(const Complex &rhs) const
- {
- return (r == rhs.r && i == rhs.i);
- }
- bool Complex::operator!=(const Complex &rhs) const
- {
- return (r != rhs.r || i != rhs.i);
- }
- int main()
- {
- Complex one, two, s, d, p;
- cin >> one;
- cin >> two;
- s = one + two;
- cout << "The sum is:" << endl << s;
- d = one - two;
- cout << "The difference is:" << endl << d;
- p = one * two;
- cout << "The product is:" << endl << p;
- if (one == two)
- {
- cout << endl << "Expressions are equal to each other!" << endl;
- }
- if (one != two)
- {
- cout << endl << "Expressions are not equal to each other!" << endl;
- }
- cout << endl << "Preincrement expressions:" << endl << ++one << ++two;
- cout << endl << "Postincrement expressions:" << endl << one++ << two++;
- cout << endl << "After postincrement expressions:" << endl << one << two;
- char c;
- cin >> c;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement