C++ day5

#include <iostream>

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 <<p1 <<endl;
//    --p1;
//    cout <<p1 <<endl;
//    p1--;
//    cout <<p1 <<endl;
//    Complex p6;
//    cin >> p5>>p6;
//    cout << p5 <<p6;
//    ~p1;
//    cout << p1 << endl;
//    Complex p3;
//    p3=p1||p2;
//    cout << p3 <<endl;
    if (p1 != p2) {
        std::cout << "p1 is not equal to p2" << std::endl;
    }
    else
    {
        std::cout << "p1 is  equal to p2" << std::endl;
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值