Error[8]: Undefined offset: 39, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

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

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

输入描述本题无输入

输出描述参考输出样例

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

样例输入 

样例输出

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

#include
#include
#pragma warning(disable:4996)
#include
 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];
}
 
 

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
oj 贵州大学 第四次作业(c++)(运算符重载)第五题_C_内存溢出

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

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

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

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

输入描述本题无输入

输出描述参考输出样例

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

样例输入 

样例输出

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

#include
#include
#pragma warning(disable:4996)
#include
 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];
}
 
 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1295806.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存