C++-4

  1. 在Complex类的基础上,完成^,>,~运算符的重载
    #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 c1);
        Complex operator-(const Complex c1);
        Complex operator*(const Complex c1);
        Complex operator/(const Complex c1);*/
        friend Complex operator+(const Complex c1,const Complex c2);
        friend Complex operator-(const Complex c1,const Complex c2);
        friend Complex operator*(const Complex c1,const Complex c2);
        friend Complex operator/(const Complex c1,const Complex c2);
        bool operator>(const Complex c1);
        bool operator<(const Complex c1);
        bool operator>=(const Complex c1);
        bool operator<=(const Complex c1);
        bool operator==(const Complex c1);
        bool operator!=(const Complex c1);
        bool operator!();
        bool operator&&(const Complex c1);
        bool operator||(const Complex c1);
        Complex operator++();
        Complex operator--();
        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);
        Complex operator^(Complex c1);
        Complex operator<<(int a);
        Complex operator>>(int a);
        Complex operator~();
        Complex show()
        {cout << rel << "+"<< vir << "i" << endl;}
        operator int()
        {return this->rel;}
        operator double()
        {return this->rel;}
        operator char()
        {return 'a';}
        int operator()(int a,int b )
        {return a>b?a:b;}
    };
    //^,<<,>>,~
    Complex Complex::operator^(Complex c1)
    {
        Complex temp;
        temp.rel=(this->rel)^c1.rel;
        temp.vir=(this->vir)^c1.vir;
        return temp;
    }
    Complex Complex::operator<<(int a)
    {
        rel=rel<<a;
        vir=vir<<a;
        return *this;
    }
    Complex Complex::operator>>(int a)
    {
        rel=rel>>a;
        vir=vir>>a;
        return *this;
    }
    Complex Complex::operator~()
    {
        rel=~rel;
        vir=~vir;
        return *this;
    }
    //自增自减
    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;
        c1.rel++;
        c1.vir++;
        return temp;
    }
    Complex operator--(Complex &c1,int )
    {
        Complex temp=c1;
        c1.rel--;
        c1.vir--;
        return temp;
    }
    
    //关系
    bool Complex::operator!()
    {
        return !(this->rel || this->vir);
    }
    bool Complex::operator&&(const Complex c1)
    {
        return this->rel && c1.rel && this->vir && c1.vir;
    }
    bool Complex::operator||(const Complex c1)
    {
        return this->rel || c1.rel || this->vir || c1.vir;
    }
    bool Complex::operator>(const Complex c1)
    {
        return this->rel>c1.rel;
    }
    bool Complex::operator<(const Complex c1)
    {
        return this->rel<c1.rel;
    }
    bool Complex::operator>=(const Complex c1)
    {
        return this->rel>=c1.rel;
    }
    bool Complex::operator<=(const Complex c1)
    {
        return this->rel<=c1.rel;
    }
    bool Complex::operator==(const Complex c1)
    {
        return this->rel==c1.rel&&this->vir==c1.vir;
    }
    bool Complex::operator!=(const Complex c1)
    {
        return this->rel!=c1.rel||this->vir!=c1.vir;
    }
    
    //加减乘除
    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 operator-(const Complex c1,const Complex c2)
    {
        Complex temp;
        temp.rel =  c1.rel - c2.rel;
        temp.vir =  c1.vir - c2.vir;
        return temp;
    }
    Complex operator*(const Complex c1,const Complex c2)
    {
        Complex temp;
        temp.rel =  c1.rel * c2.rel-c1.vir * c2.vir;
        temp.vir =  c1.vir * c2.rel+c1.rel * c2.vir;
        return temp;
    }
    Complex operator/(const Complex c1,const Complex c2)
    {
        Complex temp;
        int a = c1.rel;
        int b = c1.vir;
        int c = c2.rel;
        int d = c2.vir;
        temp.rel =  (a*c+b*d)/(c*c-d*d);
        temp.vir =  (b*c-a*d)/(c*c+d*d);
        return temp;
    }
    /*
    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;
        temp.rel = this->rel - c1.rel;
        temp.vir = this->vir - c1.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;
    }
    
    Complex Complex::operator/(const Complex c1)
    {
        Complex temp;
        temp.rel = this->rel / c1.rel;
        temp.vir = this->vir / c1.vir;
        return temp;
    }*/
    
    //输出
    ostream &operator<<(ostream &out,Complex c1)
    {
        out << c1.rel << "+" << c1.vir << "i" ;
        return out;
    }
    //输出
    istream &operator>>(istream &in,Complex &c1)
    {
        in >> c1.rel >> c1.vir;
        return in;
    }
    
    int main()
    {
        Complex c1(4,6),c2(4,0),c3(1,2),c4;
       // cin >> c3 >> c4 ;
       // cout << c3 << endl << c4 << endl;
        cout << (c1<<1) << endl << (c2>>1) << endl;
        cout << ~c3 <<endl;
        c4 =  c1^c2 ;
        cout << c4 <<endl;
    
    
       return 0;
    }
    

    myString类的基础上,完成+、关系运算符、逻辑运算符、输入输出运算符的重载

#include <iostream>
#include <cstring>
using namespace std;
char c = '\0';
class myString
{
    private:
        char *str;          //记录c风格的字符串
        int size;           //记录字符串的实际长度
    public:
        //无参构造
        myString():str(new char[20]),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(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];
        }
        myString operator+(const myString s1);
        bool operator>(const myString s1);
        bool operator&&(const myString s1);
        bool operator||(const myString s1);
        friend ostream &operator<<(ostream &out,myString s1);
        friend istream &operator>>(istream &in,myString &s1);
};
ostream &operator<<(ostream &out,myString s1)
{
    out << s1.str;
    return  out;
}
istream &operator>>(istream &in,myString &s1)
{
    in >> s1.str;
    return  in;
}
myString myString::operator+( myString s1)
{
    strcat(this->str,s1.str);
    return *this;
}
bool myString::operator>( myString s1)
{
    return strcmp(this->str,s1.str);
}
bool myString::operator&&( myString s1)
{
    return this->str&&s1.str;
}
bool myString::operator||( myString s1)
{
    return this->str||s1.str;
}
int main()
{
    myString s1,s2;
    cin >> s1 >> s2;
    cout <<"s1 = "<< s1 << endl << "s2 = " <<  s2 << endl;
    cout << "s1+s2 = " << s1+s2 << endl;
    cout << "s1>s2 = " << (s1>s2) <<endl;
    cout << "s1&&s2 = " << (s1&&s2) <<endl;


    return 0;
}

 

 

  • 27
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值