3.28C++

复数类的实现,写出三种构造函数,算术运算符、关系运算符、逻辑运算符重载尝试实现自增、自减运算符的重载 

#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);
    Num operator-(Num &other);
    friend Num operator*(const Num n1,const Num n2);
    Num operator/(const Num n1);
    friend Num operator%(const Num n1,const Num n2);
    friend bool operator>(const Num n1,const Num n2);
    bool operator<(const Num n1);
    friend bool operator>=(const Num n1,const Num n2);
    bool operator==(const Num n1);
    friend bool operator<=(const Num n1,const Num n2);

    friend bool operator&&(const Num n1,const Num n2);
    bool operator||(const Num n1);

    friend Num operator++(Num &n1);
    Num operator++(int);
    friend Num operator--(Num &n1);
    Num operator--(int);

    void show();
};
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 Num::operator-(Num &other)
{
    Num temp;
    temp.rel = this->rel-other.rel;
    temp.vir = this->vir-other.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 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;
}
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 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.vir>n2.vir;
}
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 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.vir<n2.vir;
}

bool operator&&(const Num n1,const Num n2)
{
    return (n1.rel&&n2.rel)||(n1.vir&&n2.vir);
}
bool Num::operator||(const Num n1)
{
    return (this->rel||n1.rel)&&(this->vir||n1.vir);
}
Num operator++(Num &n1)
{
    ++(n1.rel);
    ++(n1.vir);
    return n1;
}
Num Num::operator++(int)
{
    Num temp;
    temp.rel=this->rel++;
    temp.vir=this->vir++;
    return temp;
}
Num operator--(Num &n1)
{
    --(n1.rel);
    --(n1.vir);
    return n1;
}
Num Num::operator--(int)
{
    Num temp;
    temp.rel=this->rel--;
    temp.vir=this->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();
    Num n4;
    n4=n1-n2;
    n4.show();
    n4++;
    n4.show();
    --n4;
    n4.show();
    cout<<(n4>=n3)<<endl;
    Num n5;
    n5--;
    n5.show();
    ++n5;
    n5.show();
    cout<<(n2&&n3)<<endl;
    cout<<(n2||n5)<<endl;
    return 0;
}

相关推荐

  1. AT_abc348_c [ABC348C] Colorful Beans 题解

    2024-03-29 11:20:02       9 阅读
  2. LeetCode //C - 338. Counting Bits

    2024-03-29 11:20:02       34 阅读
  3. CS32 C++ programming

    2024-03-29 11:20:02       12 阅读
  4. Atcoder ABC338 C - Leftover Recipes

    2024-03-29 11:20:02       35 阅读
  5. 38. C++ 引用的本质

    2024-03-29 11:20:02       27 阅读
  6. STM<span style='color:red;'>32</span> I2<span style='color:red;'>C</span>

    STM32 I2C

    2024-03-29 11:20:02      31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-29 11:20:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-29 11:20:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-29 11:20:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-29 11:20:02       20 阅读

热门阅读

  1. PHP radis 处理缓存简单示例

    2024-03-29 11:20:02       17 阅读
  2. vue状态管理

    2024-03-29 11:20:02       20 阅读
  3. chromium 源码学习笔记

    2024-03-29 11:20:02       18 阅读
  4. 自动化组高度件切割计算

    2024-03-29 11:20:02       23 阅读
  5. Docker学习指南

    2024-03-29 11:20:02       21 阅读
  6. 简述机器视觉技术在自动化行业中的典型应用

    2024-03-29 11:20:02       21 阅读
  7. 自动化更新包文件--shell脚本

    2024-03-29 11:20:02       20 阅读
  8. Go打造REST Server【四】:Graphql进阶

    2024-03-29 11:20:02       17 阅读