3.28C++

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

cpp 复制代码
#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);

    bool operator!();

    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)
{
    return (this->rel==n1.rel)&&(this->vir==n1.vir);
}
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);
}

bool Num::operator!()
{
    return !(this->rel||this->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;
}
相关推荐
iqay4 分钟前
【C语言】填空题/程序填空题1
c语言·开发语言·数据结构·c++·算法·c#
程序猿编码4 分钟前
自定义命令执行器:C++中命令封装的深度探索(C/C++实现)
linux·c语言·c++·网络安全·shell·命令行
wen__xvn1 小时前
每日一题洛谷B3865 [GESP202309 二级] 小杨的 X 字矩阵c++
c++·算法·矩阵
makabaka_T_T2 小时前
25寒假算法刷题 | Day1 | LeetCode 240. 搜索二维矩阵 II,148. 排序链表
数据结构·c++·算法·leetcode·链表·矩阵
学游戏开发的2 小时前
UE求职Demo开发日志#19 给物品找图标,实现装备增加属性,背包栏UI显示装备
c++·笔记·游戏引擎·unreal engine
c-c-developer3 小时前
C++ Primer 标准库类型string
开发语言·c++
宁静致远20214 小时前
Ubuntu下的Doxygen+VScode实现C/C++接口文档自动生成
c++·vscode·ubuntu
Bluesonli4 小时前
第 2 天:创建你的第一个 UE5 C++ 项目!
c++·学习·ue5·虚幻·虚幻引擎·unreal engine
比特在路上4 小时前
蓝桥杯之c++入门(四)【循环】
c++·职场和发展·蓝桥杯
pay顿4 小时前
C++基础day1
c++·学习·笔试