半夏微澜ぺ871 2022-04-27 15:22 采纳率: 100%
浏览 33
已结题

关于#BUG#的问题,如何解决?

#include<iostream>
#include<cstring>
using namespace std;

class String
{
private:
    char* head;
public:
    String();
    String(char* s);
    String(String &s);
    ~String();
    String& operator=(char* s);
    String& operator=(String &s);
    String operator+=(String &s);
    String& operator+(String &s);
    bool operator==(String &s);
    bool operator<(String &s);
    char& operator[](int index);
    int Length();
    friend istream& operator>>(istream &is,String &s);
};
    String::String()
    {
        head=new char[1];
        *head=0;
    }
    String::String(char* s)
    {
        int i=strlen(s);
        head=new char[i+1];
        strcpy(head,s);
    }
    String:: String(String &s)
    {
        int i=strlen(s.head);
        head=new char[i+1];
        strcpy(head,s.head);
    }
    String:: ~String() {delete[]head;}
    String& String::operator=(char* s)
    {
        int len=strlen(s);
        char* newhead=new char[len+1];
        strcpy(newhead,s);
        delete[]head;
        head=newhead;
        return *this;
    }
    String& String::operator=(String &s)
    {
        int len=strlen(s.head);
        char* newhead=new char[len+1];
        strcpy(newhead,s.head);
        delete[]head;
        head=newhead;
        return *this;
    }
    String String::operator+=(String &s)
    {
        int len=strlen(head)+strlen(s.head);
        char* newhead=new char[len+1];
        strcpy(newhead,head);
        strcat(newhead,s.head);
        delete[]head;
        head=newhead;
        return *this;
    }
    String& String::operator+(String &s)
    {
        int len=strlen(head)+strlen(s.head);
        char* newhead=new char[len+1];
        strcpy(newhead,head);
        strcat(newhead,s.head);
        String temp;
        delete[]temp.head;
        temp.head=newhead;
        return temp;
    }
    bool String::operator==(String &s)
    {
        if(strcmp(head,s.head)==0) return true;
        else return false;
    }
    bool String::operator<(String &s)
    {
        if(strcmp(head,s.head)<0) return true;
        else return false;
    }
    char& String::operator[](int index)
    {
        return head[index];
    }
    int String::Length()
    {
        return strlen(head);
    }
    ostream& operator<<(ostream &os,String &s)
    {
        int i;
        for(i=0;s[i];i++) os<<s[i];
        return os;
    }
    istream& operator>>(istream &is,String &s)
    {
        char c[100];
        delete[]s.head;
        s.head=new char[100];
        cin>>c;
        strcpy(s.head,c);
        return is;
    }

int main()
{
    String s1("Help!"),s2("Good!"),s3(s2),s4,s5;
    cout<<"s1="<<s1<<endl;
    s3="Hello!";
    cout<<"s3="<<s3<<endl;
    s3+=s2;
    cout<<"s3="<<s3<<endl;
    cin>>s4;
    cout<<"s4="<<s4<<endl;
    s5=s3+s4;
    cout<<"s5="<<s5<<endl;
    s5[0]='g';
    cout<<"s5="<<s5<<endl;
    cout<<"strlen(s5)="<<s5.Length()<<endl;
    cout<<boolalpha<<(s3==s1)<<endl;
    cout<<boolalpha<<(s3<s1)<<endl;
    return 0;
}

运行结果
s1=Help!
s3=Hello!
s3=Hello!Good!
dgsciuwgi
s4=dgsciuwgi

  • 写回答

2条回答 默认 最新

  • 无夜_ 2022-04-27 19:49
    关注

    检查运算符重载

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月18日
  • 已采纳回答 5月10日
  • 创建了问题 4月27日

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法