Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.7 kB
4
Indexable
Never
#include <iostream>

using namespace std;

class Complex{
    public:
    
    Complex(int inreal = 0, int inimag = 0):real(inreal),imag(inimag){}
    
    friend istream &operator>>(istream &input, Complex &C){
        input >> C.real >> C.imag;
        return input;
    }

    friend ostream &operator<<(ostream &output, const Complex &C){
        output << C.real << (C.imag >= 0 ? "+" : "") << C.imag << "i";
        return output;
    }
    
    bool operator==(const Complex &C) const{
        return real == C.real && imag == C.imag; 
    }

    bool operator!=(const Complex &C) const{
        return real != C.real || imag != C.imag;
    }

    Complex operator+(const Complex &C) const{
        return Complex( real + C.real , imag + C.imag );
    }

    Complex operator-(const Complex &C) const{
        return Complex( real - C.real , imag - C.imag );
    }

    Complex operator*(const Complex &C) const{
        return Complex( real * C.real - imag * C.imag , real * C.imag + imag * C.real );
    }

    Complex operator++(int){
        Complex temp = *this;
        real++;
        imag++;
        return temp;
    }

    Complex operator++(){
        real++;
        imag++;
        return *this;
    }

    Complex operator--(int){
        Complex temp = *this;
        real--;
        imag--;
        return temp;
    }

    Complex operator--(){
        real--;
        imag--;
        return *this;
    }

    Complex operator+=(const Complex &C){
        real += C.real;
        imag += C.imag;
        return *this;
    }

    private:
    int real;
    int imag;
};
Leave a Comment