c++day4

  1. 整理思维导图
  2. 在Complex类的基础上,完成^,>,~运算符的重载
  3. 在昨天作业myString类的基础上,完成+、关系运算符、逻辑运算符、输入输出运算符的重载

#include <iostream>
using namespace std;
class complex
{
    int rel;
    int vir;
public:
    complex(){}
    complex(int rel,int vir):rel(rel),vir(vir){}
    void show()
    {
        cout << rel << "+" << vir << "i" << endl;
    }
    //算数运算符重载
    //+
    //complex operator+(complex c1);
    friend complex operator+(const complex c1,const complex c2);
    complex operator-(complex c1);
    friend complex operator-(const complex c1,const complex c2);
    //*
    complex operator*(complex c1);
    friend complex operator*(const complex c1,const complex c2);
    //%
    complex operator%(complex c1);
    friend complex operator%(const complex c1,const complex c2);
    // 关系运算符
    //>
    //bool operator>(complex c1);
    friend bool operator>(const complex c1,const complex c2);
    //<
//    bool operator<(complex c1);
    friend bool operator<(const complex c1,const complex c2);
    //>=
    bool operator>=(complex c1);
    //friend operator>=(const complex c1,const complex c2);
    // <=
    //bool operator<=(complex c1);
    friend bool operator<=(const complex c1,const complex c2);
    // !=
    bool operator!=(complex c1);
    //逻辑运算符
    //&&
    bool operator&&(complex c1);
    //++
    friend complex operator++(complex c1,int);
    //<<
    friend ostream &operator<<(ostream &out,complex c1);
    //>>
    friend istream &operator >>(istream &in,complex &c1);
    //
    friend bool operator^(const complex c1,const complex c2);
    //~
    friend int operator~(const complex c1);
};
//算术运算符重载
//+
//complex complex::operator+(complex c1)
//{
//    complex temp;
//    temp.rel=this->rel+c1.rel;
//    temp.vir=this->vir+c1.vir;
//    return  temp;
//}
complex operator+(const complex c1,const complex c2)
{
    complex temp;
    temp.rel=c1.rel+c2.rel;
    temp.vir=c1.vir+c2.vir;
    return  temp;
}
//-
complex complex::operator-(complex c1)
{
    complex temp;
    temp.rel=this->rel-c1.rel;
    temp.vir=this->vir-c1.vir;
    return  temp;
}
complex operator-(const complex c1,const complex c2)
{
    complex temp;
    temp.rel=c1.rel-c2.rel;
    temp.vir=c2.vir-c2.vir;
    return temp;
}
//*
complex complex::operator*(complex c1)
{
    complex temp;
    temp.rel=this->rel*c1.rel;
    temp.vir=this->vir*c1.vir;
    return temp;
}
complex operator*(const complex c1,const complex c2)
{
    complex temp;
    temp.rel=c1.rel*c2.rel;
    temp.vir=c2.vir*c2.vir;
    return  temp;
}
//%
complex complex::operator%(complex c1)
{
    complex temp;
    if(c1.rel==0||c1.vir==0)
    {
        cout << "分母错误" << endl;
    }
    temp.rel=this->rel%c1.rel;
    temp.vir=this->vir%c1.vir;
    return temp;
}
complex operator%(const complex c1,const complex c2)
{
    complex temp;
    if(c2.rel==0||c2.vir==0)
    {
        cout << "分母错误"  << endl;
    }
    temp.rel=c1.rel%c2.rel;
    temp.vir=c1.vir%c2.vir;
    return  temp;
}
//关系运算符
// >
//bool complex::operator>(complex c1)
//{
//    return this->rel>c1.rel;
//}
bool operator>(const complex c1,const complex c2)
{
    return c1.rel>c2.rel;
}
// <
//bool complex::operator<(complex c1)
//{
//    return this->rel<c1.rel;
//}
bool operator<(const complex c1,const complex c2)
{
    return c1.rel<c2.rel;
}
//>=
bool complex::operator>=(complex c1)
{
    if(this->rel>=c1.rel)
    {
        if(this->rel==c1.rel)
        {
            return this->vir==c1.vir;
        }
        else
        {
            return this->rel>c1.rel;
        }
    }
    else
    {
        return this->rel>c1.rel;
    }
}
// <=
bool operator<=(const complex c1,const complex c2)
{
    if(c1.rel<=c2.rel)
    {
        if(c1.rel==c2.rel)
        {
            return c1.vir==c2.vir;
        }
        else
        {
            return  c1.rel<=c2.rel;
        }
    }
    else
    {
        return  c1.rel<=c2.rel;
    }
}
//!=
bool complex::operator!=(complex c1)
{
    return this->rel!=c1.rel||this->vir!=c1.vir;
}
//逻辑运算符
//&&
bool complex ::operator&&(complex c1)
{
    return (this->rel&&c1.rel)||(this->vir&&c1.vir);
}
//++
complex operator++(complex c1,int)
{
    complex temp;
    temp.rel=c1.rel++;
    temp.vir=c1.vir++;
    return  temp;
}
//<<
ostream &operator<<(ostream &out,complex c1)
{
    out<< c1.rel << "+" << c1.vir << "i" <<endl;
    return out;
}
//>>
istream &operator >>(istream &in,complex &c1)
{
    in >> c1.rel >> c1.vir;
    return  in;
}
//^
bool operator^(const complex c1,const complex c2)
{
    if(c1.rel == c2.rel)
        return false;
    else
        return  true;
}
//~
int operator~(const complex c1)
{
    int temp(~c1.rel);
    return temp;
}
int main()
{
    complex a1(1,5),a2(3,5);
   // complex a3=a1+a2;
   // a3.show();
    complex a3=operator+(a1,a2);
    a3.show();
    //complex a4=a1-a2;
   // a4.show();
    complex a4=operator-(a1,a2);
    a4.show();
    //*
//    complex a5=a1*a2;
//    a5.show();
    complex a5=operator*(a1,a2);
    a5.show();
    //%
//    complex a6=a1%a2;
//    a6.show();
    complex a6=operator%(a1,a2);
    a6.show();
    //关系运算符
    // >
    cout << (a1>a2) << endl;
    // <
    cout << (a1<a2) << endl;
    cout << (a1>=a2) << endl;
    cout << (a1<=a2) << endl;
    cout << (a1&&a2) <<endl;
    complex a7=a1++;
    a7.show();
    cout << "out=" << a1 << endl;
    cin >> a5 >>a6;
    cout << (a1^a2) << endl;
    int b1=10;
    int b2=~b1;
    cout << "~b1" << b2 << endl;
    cout << "Hello World!" << endl;
    return 0;
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值