C++作业5

1、思维导图

2、类Complex中实现 >, <, != , !, || ,前自增、自减,后自增、自减运算符的重载

cpp 复制代码
#include <iostream>
using namespace std;
class Complex
{
    int rel;
    int vir;

public:
    Complex(){}
    Complex(int rel,int vir):rel(rel),vir(vir)
    {
    }
    Complex operator+(const Complex other);
    Complex operator-(const Complex other);
    Complex operator/(const Complex other);
    Complex operator%(const Complex other);
    Complex operator*(const Complex other);

    friend Complex operator+(const Complex c1,const Complex c2);
    friend  bool operator>(const Complex c1,const Complex c2);
    friend  bool operator<(const Complex c1,const Complex c2);
    friend  bool operator==(const Complex c1,const Complex c2);
    friend  bool operator!=(const Complex c1,const Complex c2);


    friend  bool operator&&(const Complex c1,const Complex c2);
    friend  bool operator||(const Complex c1,const Complex c2);
    friend  bool operator!(const Complex c2);


    Complex operator++();//前自增++
    friend Complex operator++(Complex &c1,int);//后自增++重载全局函数
    Complex operator--();//前自--
    friend Complex operator--(Complex &c1,int);//后自--重载全局函数
    void show();
};

//**************************算数运算符************************************
//成员函数版本的加法运算符重载
Complex Complex::operator+(const Complex other)
{
    Complex temp;
    temp.rel=this->rel+other.rel;
    temp.vir=this->vir+other.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-(const Complex other)
{
    Complex temp;
    temp.rel=this->rel-other.rel;
    temp.vir=this->vir-other.vir;
    return temp;
}
//除法
Complex Complex::operator/(const Complex other)
{
    Complex temp;
    temp.rel=this->rel/other.rel;
    temp.vir=this->vir/other.vir;
    return temp;
}
//%
Complex Complex::operator%(const Complex other)
{
    Complex temp;
    temp.rel=this->rel%other.rel;
    temp.vir=this->vir%other.vir;
    return temp;
}//*
Complex Complex::operator*(const Complex other)
{
    Complex temp;
    temp.rel=this->rel*other.rel;
    temp.vir=this->vir*other.vir;
    return temp;
}

//***********************关系运算符*******************************
//全局函数>运算符重载
bool operator>(const Complex c1,const Complex c2)
{
    if(c1.rel>c2.rel)
    {
        return 1;
    }else if(c1.rel==c2.rel)
    {
        if(c1.vir>c2.vir)
        {
            return 1;
        }
    }else
    {
        return 0;
    }
}

//全局函数<运算符重载
bool operator<(const Complex c1,const Complex c2)
{
    if(c1.rel<c2.rel)
    {
        return 1;
    }else if(c1.rel==c2.rel)
    {
        if(c1.vir<c2.vir)
        {
            return 1;
        }
    }else
    {
        return 0;
    }
}
//全局函数!=运算符重载
bool operator!=(const Complex c1,const Complex c2)
{
    if(c1.rel!=c1.rel||c1.vir!=c2.vir)
    {
        return 1;
    }else
    {
        return 0;
    }
}

//***********************逻辑运算符*******************************

//全局函数的&&运算符
bool operator&&(const Complex c1,const Complex c2)
{
    return (c1.rel||c1.vir)&&(c2.rel||c2.vir);
}
//全局函数的||运算符
bool operator||(const Complex c1,const Complex c2)
{
    return (c1.rel||c1.vir)||(c2.rel||c2.vir);
}
//全局函数的!运算符
bool operator!(const Complex c2)
{
    return !(c2.rel||c2.vir);
}

//***********************自增自减运算符*******************************
//前自++重载函数
Complex Complex::operator++()
{
    Complex temp;
    temp.rel=++this->rel;
    temp.vir=++this->vir;
    return temp;

}
//后自增++重载全局函数
Complex temp1;
Complex operator++(Complex &c1,int)
{

    temp1=c1;
    c1.rel++;
    c1.vir++;
    return temp1;

}
//前自--重载函数
Complex Complex::operator--()
{
    Complex temp;
    --this->rel;
    --this->vir;
    return *this;

}
//后自--重载全局函数
Complex temp2;
Complex operator--(Complex &c1,int)
{
    temp2=c1;
    c1.rel--;
    c1.vir--;
    return temp2;

}

void Complex::show()
{
    cout<<rel<<"+"<<vir<<"i"<<endl;
}


int main()
{
//   Complex s1(1,3);
//   Complex s2(9,7);
//   Complex s3=s1.operator+(s2);//Complex s3=s1.operator+(s2)
//   operator+(s2,s3);
//   Complex s3,s4,s5,s6,s7;
//   s3=s1.operator+(s2);
//   s3.show();
//   s4=s1.operator*(s2);
//   s4.show();

    Complex s1(11,30);
    Complex s2(9,7);
    Complex s3,s4;
   ++s1;
   s1.show();
   --s1;
   s1.show();
   cout<<endl;
   s3=s2++;
   s3.show();
   s4=s2--;
   s4.show();
   cout<<endl;

   cout<<boolalpha<<"s2>s1 -->"<<(s2>s1)<<endl;
   cout<<boolalpha<<"s2!=s1 -->"<<(s2!=s1)<<endl;
   cout<<boolalpha<<"s2||s1 -->"<<(s2||s1)<<endl;
   cout<<boolalpha<<"!s1 -->"<<!s1<<endl;


    return 0;
}

3、My_string类中实现+ ,== ,>运算符重载

cpp 复制代码
#include <iostream>
#include <cstring>
using namespace std;
char c = '\0';
class My_string
{
    char *str;     //记录C风格的字符串
    int size;      //记录字符串长度
public:
     //无参构造
    My_string():str(new char[20]{0}),size(20)
    {
        cout<<"无参构造"<<endl;
    }
    //有参构造
    My_string(const char *str)
    {
        size=strlen(str);
        this->str=new char[size+1];
        strcpy(this->str,str);
       // cout<<"有参构造"<<endl;
    }
    //拷贝构造
    My_string(const My_string &other)
    {
        this->size=other.size;
        delete []this->str;
        this->str=new char [other.size+1];
        strcpy(this->str,other.str);
        //cout<<"拷贝构造"<<endl;
    }
    //拷贝赋值
    My_string &operator=(const My_string &other)
    {
        this->size=other.size;
        delete []this->str;
        this->str=new char [other.size+1];
        strcpy(this->str,other.str);
        cout<<"拷贝赋值"<<endl;
        return *this;
    }
    //析构函数
    ~My_string()
    {
        delete []str;
    }
    //at函数
    char &my_at(int num);
    //重构运算符+
    My_string operator+(const My_string other);
    //重构运算符>
    bool operator>(const My_string other);
    //重构运算符==
    bool operator==(const My_string other);

    void show();

};

 char &My_string::my_at(int num)
{

     return *(str+num-1);
}
 //重构运算符+
My_string My_string::operator+(const My_string other)
{
    My_string s2;
    s2.size=this->size+other.size;
    strcpy(s2.str,this->str);
    strcat(s2.str,other.str);
    return s2;
}
//重构运算符>
 bool My_string::operator>(const My_string other)
{
     int i=0;
     while(this->str[i]!='\0'&&other.str[i]!='\0')
     {
        if(this->str[i]>other.str[i])
        {
            return 1;
        }
        if(this->str[i]<other.str[i])
        {
            return 0;
        }
        i++;
     }
     if(this->size>other.size)
     {
        return 1;
     }else
     {
        return 0;
     }

}
//重构运算符==
 bool My_string::operator==(const My_string other)
{
     int i=0;
     while(this->str[i]!='\0'&&other.str[i]!='\0')
     {
        if(this->str[i]>other.str[i])
        {
            return 0;
        }
        if(this->str[i]<other.str[i])
        {
            return 0;
        }
        i++;
     }
     if(this->size==other.size)
     {
        return 1;
     }else
     {
        return 0;
     }

}
void My_string::show()
 {
     cout<<"str="<<str<<endl;
 }
int main()
{
    My_string s1("helloworld");
    s1.show();
    cout<<endl;
    cout<<s1.my_at(6)<<endl;
    My_string s2("ertyui");
    My_string s3;
    s3=s1+s2;
    s3.show();


    My_string s4="helloworld";
    cout<<"s1>s2 -->"<<boolalpha<<(s1>s2)<<endl;
    cout<<"s1==s4 -->"<<boolalpha<<(s1==s4)<<endl;


}
相关推荐
DKPT14 分钟前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
好奇的菜鸟2 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
tan180°2 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
大千AI助手2 小时前
DTW模版匹配:弹性对齐的时间序列相似度度量算法
人工智能·算法·机器学习·数据挖掘·模版匹配·dtw模版匹配
DuelCode3 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
优创学社23 小时前
基于springboot的社区生鲜团购系统
java·spring boot·后端
幽络源小助理3 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
猴哥源码3 小时前
基于Java+springboot 的车险理赔信息管理系统
java·spring boot
彭祥.3 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类