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;


}
相关推荐
三川6981 小时前
排序算法介绍
数据结构·算法·排序算法
laplace01232 小时前
Java八股—MySQL
java·mysql·oracle
熙客3 小时前
TiDB:分布式关系型数据库
java·数据库·分布式·tidb
2301_795167204 小时前
玩转Rust高级应用 如何避免对空指针做“解引用”操作,在C/C++ 里面就是未定义行为
c语言·c++·rust
你想考研啊4 小时前
linux安装jdk和tomcat和并自启动
java·linux·tomcat
智驱力人工智能5 小时前
基于视觉分析的人脸联动使用手机检测系统 智能安全管理新突破 人脸与手机行为联动检测 多模态融合人脸与手机行为分析模型
算法·安全·目标检测·计算机视觉·智能手机·视觉检测·边缘计算
悟能不能悟6 小时前
java的java.sql.Date和java.util.Date的区别,应该怎么使用
java·开发语言
2301_764441336 小时前
水星热演化核幔耦合数值模拟
python·算法·数学建模
循环过三天6 小时前
3.4、Python-集合
开发语言·笔记·python·学习·算法
高山上有一只小老虎7 小时前
java 正则表达式大全
java·正则表达式