0
点赞
收藏
分享

微信扫一扫

OmniGraffle Pro for mac 出色的图形设计软件

女侠展昭 04-01 13:30 阅读 1

算数运算符

#include <iostream>
using namespace std;
class Num
{
    int rel;   //实部
    int vir;   //虚部
public:
    Num():rel(2),vir(1){}
    Num(int rel,int vir):rel(rel),vir(vir){}
    Num &operator=(const Num &other)
    {
        cout << "Num的拷贝赋值函数" << endl;
        this->rel = other.rel;
        this->vir = other.vir;
        return *this;
    }
    friend Num operator+(const Num n1,const Num n2);
    friend Num operator-(const Num n1,const Num n2);
    friend Num operator*(const Num n1,const Num n2);
    friend Num operator/(const Num n1,const Num n2);
    friend Num operator%(const Num n1,const Num n2);
     
    Num operator+(const Num n1);
    Num operator-(const Num n1);
    Num operator*(const Num n1);
    Num operator/(const Num n1);
    Num operator%(const Num n1);
    void show();
};

Num Num::operator+(const Num n1 )
{       
    Num temp;            
    temp.rel=this->rel+n1.rel;
    temp.vir=this->vir+n1.vir;
    return temp;
}
Num Num::operator-(const Num n1)
{
    Num temp;
    temp.rel=this->rel-n1.rel;
    temp.vir=this->vir-n1.vir;
    return temp;
}
Num Num::operator*(const Num n1)
{
    Num temp;
    temp.rel=this->rel*n1.rel;
    temp.vir=this->vir*n1.vir;
    return temp;
}
Num Num::operator/(const Num n1)
{
    Num temp;
    temp.rel=this->rel/n1.rel;
    temp.vir=this->vir/n1.vir;
    return temp;
}
Num Num::operator%(const Num n1)
{
    Num temp;
    temp.rel=this->rel%n1.rel;
    temp.vir=this->vir%n1.vir;
    return temp;
}

//全局函数版的加法运算符重载
Num operator+(const Num n1,const Num n2)
{
    Num temp;
    temp.rel = n1.rel+n2.rel;
    temp.vir = n1.vir+n2.vir;
    return temp;
}
//减法
Num operator-(const Num n1,const Num n2)
{
    Num temp;
    temp.rel = n1.rel-n2.rel;
    temp.vir = n1.vir-n2.vir;
    return temp;
}
Num operator*(const Num n1,const Num n2)
{
    Num temp;
    temp.rel = n1.rel*n2.rel;
    temp.vir = n1.vir*n2.vir;
    return temp;
}
Num operator/(const Num n1,const Num n2)
{
    Num temp;
    temp.rel = n1.rel/n2.rel;
    temp.vir = n1.vir/n2.vir;
    return temp;
}
Num operator%(const Num n1,const Num n2)
{
    Num temp;
    temp.rel = n1.rel%n2.rel;
    temp.vir = n1.vir%n2.vir;
    return temp;
}
void Num::show()
{
    cout << rel << "+" << vir << "i" << endl;
}
int main()
{
    Num n1;
    Num n2(1,4);
    Num n3;
    n3 = n1+n2;
    n3.show();
    return 0;
}

关系运算符

#include <iostream>

using namespace std;
class Num
{
    int rel;   //实部
    int vir;   //虚部
public:
    Num():rel(2),vir(1){}
    Num(int rel,int vir):rel(rel),vir(vir){}
    Num &operator=(const Num &other)
    {
        cout << "Num的拷贝赋值函数" << endl;
        this->rel = other.rel;
        this->vir = other.vir;
        return *this;
    }
    friend bool operator>(const Num n1,const Num n2);
    friend bool operator>=(const Num n1,const Num n2);
    friend bool operator<(const Num n1,const Num n2);
    bool operator<=(const Num n1);
    bool operator==(const Num n1);
    bool operator!=(const Num n1);
};
bool operator>(const Num n1,const Num n2)
{
    if(n1.rel>n2.rel)  
    {
        return n1.rel>n2.rel;
    }
    else if(n1.rel==n2.rel)  
    {
        return n1.vir>n2.vir;
    }
    return n1.rel>n2.rel;
}
bool operator>=(const Num n1,const Num n2)
{
    if(n1.rel>n2.rel)  
    {
        return n1.rel>=n2.rel;
    }
    else if(n1.rel==n2.rel)  
    {
        return n1.vir>=n2.vir;
    }
    return n1.rel>=n2.rel;
}
bool operator<(const Num n1,const Num n2)
{
    if(n1.rel<n2.rel)  
    {
        return n1.rel<n2.rel;
    }
    else if(n1.rel==n2.rel)  
    {
        return n1.vir<n2.vir;
    }
    return n1.rel<n2.rel;
}
bool Num::operator<=(const Num n1)
{
    if(this->rel<n1.rel)  
    {
        return this->rel<=n1.rel;
    }
    else if(this->rel==n1.rel)  
    {
        return this->vir<=n1.vir;
    }
    return this->rel<=n1.rel;
}
bool Num::operator==(const Num n1)
{
    if(this->rel==n1.rel)  
    {
        return this->vir==n1.vir;
    }
    return this->rel==n1.rel;
}
bool Num::operator!=(const Num n1)
{
    if(this->rel!=n1.rel)  
    {
        return this->rel!=n1.rel;
    }
    else if(this->rel==n1.rel)  
    {
        return this->vir!=n1.vir;
    }
    return this->rel!=n1.rel;
}
int main()
{
    
    return 0;
}

逻辑运算符

#include <iostream>

using namespace std;
class Num
{
    int rel;   //实部
    int vir;   //虚部
public:
    Num():rel(2),vir(1){}
    Num(int rel,int vir):rel(rel),vir(vir){}
    Num &operator=(const Num &other)
    {
        cout << "Num的拷贝赋值函数" << endl;
        this->rel = other.rel;
        this->vir = other.vir;
        return *this;
    }
    friend bool operator&&(const Num n1,const Num n2);
    friend bool operator||(const Num n1,const Num n2);

};
bool operator&&(const Num n1,const Num n2)
{
    if(n1.rel&&n2.rel)
    {
        return n1.vir&&n2.vir;
    }
    return n1.rel&&n2.rel;
}
bool operator||(const Num n1,const Num n2)
{
    if(n1.rel||n2.rel)
    {
        return n1.vir||n2.vir;
    }
    return n1.rel||n2.rel;
}
int main()
{
    cout << "Hello World!" << endl;
    return 0;
}
举报

相关推荐

0 条评论