朱倩倩zqq 2021-05-02 16:25 采纳率: 75%
浏览 59
已采纳

关于strcpy,strcat,开辟空间问题(求大佬指点)

#pragma warning (disable:4996)
#include<iostream>
using namespace std;
class String
{
protected:
    char* str;
public:
    String();
    String(const char* content);
    String(const String& cstr);
    ~String();
    void set(const char* content);
    int length() const;
    void print() const;
    String& operator=(const String& cstr);
    String& operator=(const char* cstr);
    char& operator[](int index);
    operator char* ();
    friend    String operator+(const String& cstr1, const String& cstr2);
    friend String operator+(const String& cstr1, const char* cstr2);
    friend String operator+(const char* cstr1, const String& cstr2);
    friend  String& operator++(String& cstr);
    friend  String operator++(String& cstr, int);
};
String::String()
{
    str = 0;
}
String::String(const char* content)
{
    int len = strlen(content);
    str = new char[len + 1];
    strcpy(str, content);
}
String::String(const String& cstr)
{
    int len = strlen(cstr.str);
    str = new char[len + 1];
    strcpy(str, cstr.str);
}
String::~String()
{
    if (str != 0)
        delete[]str;
}
void String::set(const char* content)
{
    int len = strlen(content);
    str = new char[len + 1];
    strcpy(str, content);
}
int String::length() const
{
    int len = strlen(str);
    return len;
}
void String::print() const
{
    cout << "字符串为:" << str << endl;
}
String& String::operator=(const String& cstr)
{
    if (str != 0)
        delete[]str;
    int len = strlen(cstr.str);
    str = new char[len + 1];
    strcpy(str, cstr.str);
    return *this;
}
String& String::operator=(const char* cstr)
{
    if (str != 0)
        delete[]str;
    int len = strlen(cstr);
    str = new char[len + 1];
    strcpy(str, cstr);
    return *this;
}
char& String::operator[](int index)
{
    return str[index];
}
String::operator char* ()
{
    return str;
}
String operator+(const String& cstr1, const String& cstr2)
{
    String cp1;
    int len = strlen(cstr1.str) + strlen(cstr1.str);
    cp1.str = new char[len+1];
    strcpy(cp1.str, cstr1.str);
    strcat(cp1.str, cstr2.str);
    return cp1;
}
String operator+(const String& cstr1, const char* cstr2)
{
    String cp2;
    int len = strlen(cstr1.str) + strlen(cstr2);
    cp2.str = new char[len+1 ];
    strcpy(cp2.str, cstr1.str);
    strcat(cp2.str, cstr2);
    return cp2;
}
String operator+(const char* cstr1, const String& cstr2)
{
    String cp3;
    int len = strlen(cstr1) + strlen(cstr2.str);
    cp3.str = new char[len + 1];
    strcpy(cp3.str, cstr1);
    strcat(cp3.str, cstr2.str);//先复制用strcpy,将\0复制最后面,然后strcat的内容将\0覆盖最后结尾加了一个\0.(理论感觉可以,但实际操作却有的成功,有的失败了)

    return cp3;
}

String& operator++(String& cstr)
{
    int len = strlen(cstr.str);
    for (int i = 0; i < len; i++)
    {
        if (cstr.str[i] >= 'a' && cstr.str[i] <= 'z')
            cstr.str[i] = cstr.str[i] - 32;
        else
            cstr.str[i] = cstr.str[i];
    }
    return cstr;
}
String operator++(String& cstr, int)
{
    String cp4(cstr.str);
    int len = strlen(cstr.str);
    for (int i = 0; i < len; i++)
    {
        if (cstr.str[i] >= 'a' && cstr.str[i] <= 'z')
            cstr.str[i] = cstr.str[i] - 32;
        else
            cstr.str[i] = cstr.str[i];
    }
    return cp4;

}
int main()
{
    char* nstr;
    char zemb[7] = "hdjj";
    nstr = zemb;
    String temp1("Sgd");
    String temp2("bhd");//把这里改成“gdhfjkvb”则无法运行成功
    String temp7, temp8;
    temp7 = temp1;
    temp8 = nstr;
    temp7.print();
    temp8.print();
    String temp3 = temp1 + temp2;
    temp3.print();
    String temp4;
    temp4 = temp1 + nstr;
    temp4.print();
    String temp5;
    temp5 = nstr + temp1;
    temp5.print();
    ++temp1;
    temp1.print();
    String temp6;
    temp6 = temp2++;
    temp6.print();
    return 0;
}

  • 写回答

9条回答 默认 最新

  • CSDN专家-Time 2021-05-02 16:39
    关注

    改完就对了。

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

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败