C++ day5

#include 

using namespace std;
class Complex
{
private:
    int real;
    int vir;
public:
    Complex(){}
    Complex(int a,int b):real(a),vir(b){}
    Complex operator+(Complex &a)
    {
        Complex temp;
        temp.vir = this->vir+a.vir;
        temp.real  = this->real + a.real;
        return temp;
    }
    void show()
    {
        cout << real << "+" << vir << "i" << endl;
    }
    friend Complex operator-(Complex &a1,Complex &b1);
    friend Complex operator*(Complex &a1,Complex &b1);
    friend Complex operator+=(Complex &a,Complex &b);
    friend bool operator > (Complex &a,Complex &b);
    friend Complex &operator++(Complex &a);
    friend ostream &operator<<(ostream &out,Complex &a);
    friend istream &operator >> (istream &in,Complex &a);
    friend Complex &operator++(Complex &a,int);
    friend Complex &operator--(Complex &a);
    friend Complex &operator--(Complex &a,int);
    friend Complex operator-=(Complex &a,Complex &b);
    friend Complex operator*=(Complex &a,Complex &b);
    friend Complex operator/=(Complex &a,Complex &b);
    friend Complex operator%=(Complex &a,Complex &b);
    friend Complex operator+(Complex &a,Complex &b);
    friend Complex operator/(Complex &a,Complex &b);
    friend Complex &operator !(Complex &a);
    friend Complex &operator ~(Complex &a);
    friend Complex operator &&(Complex &a,Complex &b);
    friend Complex operator ||(Complex &a,Complex &b);
    friend bool operator ==(Complex &a,Complex &b);
    friend bool operator !=(Complex &a,Complex &b);
    friend bool operator >=(Complex &a,Complex &b);
    friend bool operator <=(Complex &a,Complex &b);
};

//-运算符
Complex operator-(Complex &a1,Complex &b1)
{
    Complex temp;
    temp.vir = a1.vir - b1.vir;
    temp.real = a1.real - b1.real;
    return temp;
}

//+运算符
Complex operator+(Complex &a1,Complex &b1)
{
    Complex temp;
    temp.vir = a1.vir + b1.vir;
    temp.real = a1.real + b1.real;
    return temp;
}

//*运算符
Complex operator*(Complex &a1,Complex &b1)
{
        Complex temp;
        temp.vir = a1.vir*b1.vir;
        temp.real = a1.real*b1.real;
        return temp;
}

// /运算符
Complex operator/(Complex &a1,Complex &b1)
{
        Complex temp;
        temp.vir = a1.vir/b1.vir;
        temp.real = a1.real/b1.real;
        return temp;
}

//+=运算符
Complex operator+=(Complex &a,Complex &b)
{
    a.vir += b.vir;
    a.real += b.real;
    return a;
}

//-=运算符
Complex operator-=(Complex &a,Complex &b)
{
    a.vir -= b.vir;
    a.real -= b.real;
    return a;
}

//*=运算符
Complex operator*=(Complex &a,Complex &b)
{
    a.vir *= b.vir;
    a.real *= b.real;
    return a;
}

// /=运算符
Complex operator/=(Complex &a,Complex &b)
{
    a.vir /= b.vir;
    a.real /= b.real;
    return a;
}

//%=运算符
Complex operator%=(Complex &a,Complex &b)
{
    a.vir %= b.vir;
    a.real %= b.real;
    return a;
}
//bool大于运算符
bool operator > (Complex &a,Complex &b)
{
    if(a.real == b.real)
    {
        return a.vir > b.vir;
    }
    else
    {
        return a.real > b.real;
    }
}

//前自增运算符
Complex &operator++(Complex &a)
{
    ++a.vir;
    ++a.real;
    return a;
}

//前自减运算符
Complex &operator--(Complex &a)
{
    --a.vir;
    --a.real;
    return a;
}

//后自增运算符
Complex &operator++(Complex &a,int)
{
    a.vir++;
    a.real++;
    return a;
}

//后自减运算符
Complex &operator--(Complex &a,int)
{
    a.vir--;
    a.real--;
    return a;
}

//提取运算符
ostream &operator<<(ostream &out,Complex &a)
{
    out << a.real << "+" << a.vir << "i" << endl;
    return out;
}

//插入运算符
istream &operator >> (istream &in,Complex &a)
{
    in >> a.real ;
    in >> a.vir;
    return in;
}

//!运算符
Complex &operator !(Complex &a)
{
    a.vir = !a.vir;
    a.real = !a.real;
    return a;
}

//~运算符
Complex &operator ~(Complex &a)
{
    a.vir = ~a.vir;
    a.real = ~a.real;
    return a;
}

//&&运算符
Complex operator &&(Complex &a,Complex &b)
{
    Complex temp;
    temp.vir = a.vir && b.vir;
    temp.real = a.real && b.real;
    return temp;
}

//||运算符
Complex operator ||(Complex &a,Complex &b)
{
    Complex temp;
    temp.vir = a.vir || b.vir;
    temp.real = a.real || b.real;
    return temp;
}

// ==重载
bool operator ==(Complex &a,Complex &b)
{
    return (a.real == b.real)&&(a.vir == b.vir);
}

// !=重载
bool operator !=(Complex &a,Complex &b)
{
    return (a.real == b.real)&&(a.vir == b.vir);
}

// >=重载
bool operator >=(Complex &a,Complex &b)
{
    return (a.real >= b.real)&&(a.vir >= b.vir);
}

// <=重载
bool operator <=(Complex &a,Complex &b)
{
    return (a.real <= b.real)&&(a.vir <= b.vir);
}
int main()
{
    Complex p1(1,2);
    Complex p2(1,3);
//    Complex p3;
//    p3=p1+p2;
//    p3.show();

//    p3 = p1 -p2;
//    p3.show();

//    Complex p4;
//    p4 = p1*p2;
//    p4.show();
//    p1+=p2;
//    p1.show();

//    ++p1;
//    p1.show();
    //cout << p1 << p2;
//    p1++;
//    cout <> p5>>p6;
//    cout << p5 <

C++ day5_第1张图片

 

你可能感兴趣的:(c++,java,开发语言)