oj 贵州大学 第四次作业(c++)(运算符重载)第五题

定义一个MyString类,该类的声明如下:

 
class MyString{
char *ptr;
public:
int size()const{
return strlen(ptr);
}
MyString(){
ptr=new char[1];
ptr[0]='\0';
}
MyString(const char *p){
ptr=new char[strlen(p)+1];
strcpy(ptr,p);
}
MyString(const MyString &str){
ptr=new char[str.size()+1];
strcpy(ptr,str.ptr);
}
~MyString(){
delete []ptr;
}
friend ostream & operator<<(ostream & out,const MyString &str); 
friend MyString operator+(const MyString &s1,const MyString &s2); 
friend int operator==(const MyString &s1,const MyString &s2);
friend int operator!=(const MyString &s1,const MyString &s2);
friend int operator<(const MyString &s1,const MyString &s2);
friend int operator<=(const MyString &s1,const MyString &s2);
friend int operator>(const MyString &s1,const MyString &s2);
friend int operator>=(const MyString &s1,const MyString &s2);
MyString & operator=(const MyString &s);
MyString & operator+=(const MyString &s);
char operator[](int i);
}; 
对该类重载教材233页表6-1中列出的那些运算符(这些运算符的含义和string类一致)。
main函数也写好了,请根据上面的类声明部分和main函数的内容,将未实现的运算符函数进行实现。main函数如下:
int main(){
MyString s1("Hello"),s2("World"),s3("China");
cout<<(s1>s2)<<" "<<(s1>=s2)<<" "<<(s1<=s2)<<" "<<(s2<s3)<<endl;
cout<<(s1==s2)<<" "<<(s1!=s3)<<endl;
MyString s4=s1+s2;
cout<<s4<<endl;
s1=s2=s3;
cout<<s1<<" "<<s2<<" "<<s3<<endl;
s4+=s3;
cout<<s4<<endl;
for(int i=s4.size()-1;i>=0;i--)
cout<<s4[i];
cout<<endl;
return 0; 
}

输入描述本题无输入

输出描述参考输出样例

提示只需要提交需要实现的运算符函数的代码,类的声明和主函数不需要提交

样例输入 

样例输出

0 0 1 0
0 1
HelloWorld
China China China
HelloWorldChina
anihCdlroWolleH
 

#include<stdio.h>
#include <iostream>
#pragma warning(disable:4996)
#include <string>
 ostream& operator<<(ostream& out, const MyString& str)
{
    out << str.ptr;
    return out;
}
int operator==(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) == 0)return 1;
    else return 0;
}
int operator!=(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) == 0)return 0;
    else return 1;
}
int operator<(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) < 0)return 1;
    else return 0;
}
int operator<=(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) > 0)return 0;
    else return 1;
}
int operator>(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) > 0)return 1;
    else return 0;
}
int operator>=(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) < 0)return 0;
    else return 1;

MyString operator+(const MyString& s1, const MyString& s2)
{
    MyString s;
    s.ptr = new char[strlen(s1.ptr) + strlen(s2.ptr) + 1];
    strcpy(s.ptr, s1.ptr);
    strcat(s.ptr, s2.ptr);
    return s;
}
MyString &MyString :: operator=(const MyString& s)
{
    ptr = new char[strlen(s.ptr) + 1];
    strcpy(ptr, s.ptr);
    return *this;
}
MyString &MyString :: operator+=(const MyString& s)
{
    char* temp;
    temp = ptr;
    ptr = new char[strlen(temp) + strlen(s.ptr) + 1];
    strcpy(ptr, temp);
    delete[]temp;
    strcat(ptr, s.ptr);
    return *this;
}
 char MyString::operator[](int i)
{
    return this->ptr[i];
}
 
 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值