4月26日 C++day4

 

#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
    int rel;
    int vir;
public:
    Complex(){}
    Complex(int rel,int vir):rel(rel),vir(vir)
    {}
    Complex operator+(const Complex c1);
    Complex operator/(const Complex c1);
    Complex operator%(const Complex c1);
    bool operator>(const Complex c1);
    bool operator<(const Complex c1);
    bool operator==(const Complex c1);
    bool operator&&(const Complex c1);
    Complex operator++();
    Complex operator--();
    Complex operator^(const Complex c1);
    Complex operator~();
    operator int()
    {
        return this->rel;
    }
    void show()
    {
        cout << "rel=" << rel << " vir=" << vir << endl;
 
    }
    int operator()(int a,int b)
    {
        this ->show();
        return a>b?a:b;
    }
    friend Complex operator-(const Complex c1,const Complex c2);
    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);
    friend bool operator||(const Complex c1,const Complex c2);
    friend Complex operator++(Complex &c1,int);
    friend Complex operator--(Complex &c1,int);
    friend ostream &operator<<(ostream &out,Complex c1);
    friend istream &operator>>(istream &in,Complex &c1);
    friend Complex operator>>(Complex &c1,int pos);
    friend Complex operator<<(Complex &c1,int pos);
};
Complex ret(0,0);
 
Complex Complex::operator+(const Complex c1)
{
    Complex temp;
    temp.rel=this->rel+c1.rel;
    temp.vir=this->vir+c1.vir;
    return temp;
}
Complex Complex::operator/(const Complex c1)
{
      Complex temp(this->rel/c1.rel,this->vir/c1.vir);
      return  temp;
}
Complex operator-(const Complex c1,const Complex c2)
{
    Complex temp(c1.rel-c2.rel,c2.vir-c2.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 c1)
{
    Complex temp;
    temp.rel=this->rel%c1.rel;
    temp.vir=this->vir%c1.vir;
    return temp;
}
bool Complex::operator>(const Complex c1)
{
    return this->rel>c1.rel;
}
bool Complex::operator<(const Complex c1)
{
    return this->rel<c1.rel;
}
bool operator>=(const Complex c1,const Complex c2)
{
    return c1.rel >= c2.rel;
}
bool operator<=(const Complex c1,const Complex c2)
{
    return c1.rel <= c2.rel;
}
bool operator!=(const Complex c1,const Complex c2)
{
    return c1.rel != c2.rel;
}
bool Complex::operator==(const Complex c1)
{
    return this->rel==c1.rel;
}
bool operator!(const Complex c1)
{
    return !(c1.rel||c1.vir);
}
bool Complex::operator&&(const Complex c1)
{
    return this->rel&&c1.rel&&this->vir&&c1.vir;
}
bool operator||(const Complex c1,const Complex c2)
{
    return c1.rel||c2.rel||c1.vir||c2.vir;
}
Complex Complex::operator++()
{
    ++this->rel;
    ++this->vir;
    return *this;
}
Complex Complex::operator--()
{
    --this->rel;
    --this->vir;
    return *this;
}
Complex operator++(Complex &c1,int)
{
    Complex temp(c1.rel++,c1.vir++);
    return temp;
}
Complex operator--(Complex &c1,int)
{
    Complex temp(c1.rel--,c1.vir--);
    return temp;
}
ostream &operator<<(ostream &out,Complex c1)
{
    out << c1.rel << "+" << c1.vir << "i" << endl;
    return out;
}
istream &operator>>(istream &in,Complex &c1)
{
    in >> c1.rel >> c1.vir;
    return in;
}
Complex Complex::operator^(const Complex c1)
{
    Complex temp;
    temp.rel=this->rel^c1.rel;
    temp.vir=this->vir^c1.vir;
    return temp;
}
Complex operator>>(Complex &c1,int pos)
{
 
    int m=sizeof(c1.rel)*8;
    if(pos>m)
    {
        cout << "位置不合理" << endl;
        return ret;
    }
    c1.rel/=pow(2,pos);
    c1.vir/=pow(2,pos);
 
    Complex temp(c1.rel,c1.vir);
    return temp;
}
Complex operator<<(Complex &c1,int pos)
{
 
    int m=sizeof(c1.rel)*8;
    if(pos>m)
    {
        cout << "位置不合理" << endl;
        return ret;
    }
    c1.rel*=pow(2,pos);
    c1.vir*=pow(2,pos);
 
    Complex temp(c1.rel,c1.vir);
    return temp;
}
Complex Complex::operator~()
{
    this->rel=~this->rel;
    this->vir=~this->vir;
    return *this;
}
int main()
{
    Complex c1(4,10),c2(10,4);
    cout << c1 << c2;
    Complex s1=c1.operator^(c2);
    cout << s1;
    operator>>(s1,2);
    cout << s1;
    operator<<(s1,2);
    cout << s1;
    s1.operator~();
    cout << s1;
    s1.operator~();
    cout << s1;
 
    return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
char c = '\0';
class myString
{
    private:
        char *str;          //记录c风格的字符串
        int size;           //记录字符串的实际长度
    public:
        //无参构造
        myString():str(new char),size(0)/*:str(new char),size(0)*/
        {}
        //有参构造
        myString(char *p,int size):str(new char[size+1]),size(size)
        {
            strcpy(str,p);
        }
        myString(string s1):str(new char[s1.size()+1]),size(s1.length())
        {
            strcpy(str,s1.c_str());
        }
        //拷贝构造
        myString(const myString &other):str(new char[other.size+1]),size(other.size)
        {
            strcpy(str,other.str);
        }
        //拷贝赋值函数
        myString &operator=(const myString &other)
        {
            if(&other!=this)
            {
                delete []str;
                str = new char[other.size+1];
                strcpy(str,other.str);
                this->size = other.size;
            }
            return *this;
        }
        //析构函数
        ~myString()
        {
            delete []str;
        }
        //判空函数
        bool empty()
        {
            return size==0;
        }
        //size函数
        int size_()
        {
            return size;
        }
        //c_str函数
        const char *c_str()
        {
            return str;
        }
        //at函数
        char &at(int pos)
        {
            //判断位置合理性
            if(pos<0||pos>=size)
            {
                cout << "位置不合理" << endl;
                return c;
            }
            return str[pos];
        }
        void show()
        {
            cout << "str=" << str << " size=" << size << endl;
        }
        myString operator+(const myString s1);
        bool operator>(const myString s1);
        bool operator==(const myString s1);
        myString operator&&(const myString s1);
        friend ostream &operator<<(ostream &out,myString s1);
        friend istream &operator>>(istream &in,myString &s1);
 
};
bool myString::operator==(const myString s1)
{
     if(strcmp(this->str,s1.str)==0)
     {
         return 1;
     }
     else
         return 0;
}
bool myString::operator>(const myString s1)
{
    if(strcmp(this->str,s1.str)==1)
    {
        return 1;
    }
    else
        return 0;
}
myString myString::operator+(const myString s1)
{
   myString temp;
   strcpy(temp.str,this->str);
   strcat(temp.str,s1.str);
   temp.size=this->size+s1.size;
   return temp;
}

ostream &operator<<(ostream &out,myString s1)
{
    out << "str=" << s1.str << " size=" << s1.size << endl;
    return out;
}
istream &operator>>(istream &in,myString &s1)
{
    s1.str=new char[32];
    in>>s1.str;
    s1.size=strlen(s1.str);
    return in;
}
int main()
{
    char str1[]="hello";
    myString s1(str1,5);
    char str2[]="world";
    myString s2(str2,5);
    s1.show();
    s2.show();
    myString s3=s1.operator+(s2);
    s3.show();
    cout << s1.operator>(s2) << endl;
    cout << s1.operator==(s1) << endl;
    cout << s1 << s2;
    myString s6;
    cin >> s6 ;
    cout << s6;
    return 0;
}

 

相关推荐

  1. 420,每日信息差

    2024-04-28 16:50:01       13 阅读
  2. 423加油站+分发糖果

    2024-04-28 16:50:01       14 阅读
  3. 424,每日信息差

    2024-04-28 16:50:01       17 阅读
  4. 426划分字母区间+合并区间

    2024-04-28 16:50:01       14 阅读
  5. 每日新闻掌握【2024年422 星期一】

    2024-04-28 16:50:01       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-28 16:50:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-28 16:50:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-28 16:50:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-28 16:50:01       20 阅读

热门阅读

  1. 镜像:数字时代的自我呈现与虚拟重构

    2024-04-28 16:50:01       11 阅读
  2. 字节跳动高频题目(1)

    2024-04-28 16:50:01       13 阅读
  3. Go语言结构体

    2024-04-28 16:50:01       10 阅读
  4. go 映射(Map)使用注意事项

    2024-04-28 16:50:01       13 阅读
  5. 【MySQL】——用户和权限管理(一)

    2024-04-28 16:50:01       10 阅读
  6. 「PHP系列」PHP 发送电子邮件详解

    2024-04-28 16:50:01       15 阅读
  7. uniapp 基础阿里云点播 使用

    2024-04-28 16:50:01       12 阅读