39.字符串类

字符串类.cpp

#pragma warning(disable:4996)
#define _CRT_SECURE_NO_WARNINGS 1
//2022年10月14日21:22:09
#include<iostream>
using namespace std;
#include "MyString.h"

void test()
{
    MyString s1(10, 'a');
    cout << s1 << endl;

    MyString s2(3, 'b');
    cout << s2 << endl;

    MyString s3 = s1 + s2;
    cout << s3 << endl;
    MyString s4 = s3 + "hello";
    cout << s4 << endl;

    for( int i = 0; i < s4.Size(); i++ )
    {
        cout << s4[i] << " ";
    }

    cout << endl;

    MyString s5(5, 'c');
    s4 += s5;
    cout << s4 << endl;

    s4 += "world";
    cout << s4 << endl;

    cin >> s4;
    cout << s4;
}  
int main()
{
    test();
    system("pause");
    return EXIT_SUCCESS;
}

MyString.cpp

#include "MyString.h"
#define _CRT_SECURE_NO_WARNINGS 1
MyString::MyString()
{
    this->pM = new char[1];
    this->pM[0] = '\0';
    this->mSize = 0;
}

MyString::~MyString()
{
    if( this->pM != NULL )
    {
        delete[]this->pM;
        this->pM = NULL;
    }
}


MyString::MyString(int n, char c)//用户可以设定初始化字符串,n个c组成的字符串
{
    this->pM = new char[n + 1];
    for( int i = 0; i < n; i++ )
    {
        this->pM[i] = c;
    }
    this->pM[n] = '\0';
    this->mSize = n;
}

MyString::MyString(const MyString &str)
{
    this->pM = new char[strlen(str.pM) + 1];
    strcpy(this->pM, str.pM);
    this->mSize = str.mSize;
}

MyString &MyString::operator=(const MyString &str)
{
    //1.释放原来的空间
    if( this->pM != NULL )
    {
        delete[] this->pM;
        this->pM = NULL;
    }
    //2.申请空间
    this->pM = new char[strlen(str.pM) + 1];
    //3.拷贝数据
    strcpy(this->pM, str.pM);
    this->mSize = str.mSize;

    return *this;
}

MyString MyString::operator+(const MyString &str)
{
    //MyString s3 = s1 + s2;
    //this是s1,str是s2;
    //获取s3要开辟空间的大小
    int newlen = this->mSize + str.mSize + 1;

    //1.定义一个临时变量
    MyString tmp;
    //2.释放原来的空间
    if( tmp.pM != NULL )
    {
        delete[] tmp.pM;
        tmp.pM = NULL;
    }
    //3.申请新的空间
    tmp.pM = new char[newlen];
    memset(tmp.pM, 0, newlen);
    tmp.mSize = this->mSize + str.mSize;

    //4.追加字符到空间中
    strcat(tmp.pM, this->pM);
    strcat(tmp.pM, str.pM);

    return tmp;
}

MyString MyString::operator+(const char *s)
{
    int newlen = this->mSize + strlen(s);
    //开辟空间
    char *newspace = new char[newlen + 1];
    memset(newspace, 0, newlen + 1);

    //追加数据到空间
    strcat(newspace, this->pM);
    strcat(newspace, s);

    MyString tmp;
    if( tmp.pM != NULL )
    {
        delete[] tmp.pM;
        tmp.pM = NULL;
    }

    tmp.pM = newspace;

    tmp.mSize = newlen;

    return tmp;
}

MyString &MyString::operator+=(const MyString &str)
{
    /*
    s4+=s3; ==> s4=s4+s3;

    this是s4,str是s3
    */

    //1.获取两个字符串的总字符个数
    int newlen = this->mSize + str.mSize;
    //2.申请新空间
    char *newspace = new char[newlen + 1];
    memset(newspace, 0, newlen + 1);

    //3.追加数据
    strcat(newspace, this->pM);
    strcat(newspace, str.pM);

    //4.释放本身的空间
    if( this->pM != NULL )
    {
        delete[] this->pM;
        this->pM = NULL;
    }

    this->pM = newspace;
    this->mSize = newlen;

    return *this;
}

MyString &MyString::operator+=(const char *s)
{
    /*
    s4+=s3; ==> s4=s4+s3;

    this是s4,str是s3
    */

    //1.获取两个字符串的总字符个数
    int newlen = this->mSize + strlen(s);
    //2.申请新空间
    char *newspace = new char[newlen + 1];
    memset(newspace, 0, newlen + 1);

    //3.追加数据
    strcat(newspace, this->pM);
    strcat(newspace, s);

    //4.释放本身的空间
    if( this->pM != NULL )
    {
        delete[] this->pM;
        this->pM = NULL;
    }

    this->pM = newspace;
    this->mSize = newlen;

    return *this;
}

int MyString::Size()
{
    return this->mSize;
}

char &MyString::operator[](int index)
{
    return this->pM[index];
}

ostream &operator<<(ostream &out, MyString &str)
{
    out << str.pM;
    return out;
}

istream &operator>>(istream &in, MyString &str)
{
    //cin >> s4;
    //用户输入的字符串要存储到s4.pM指向的堆区空间
    //1.定义临时空间
    char tmp[1024] = {0};
    //2.获取用户输入的信息
    in >> tmp;

    //3.释放s4的空间
    if( str.pM != NULL )
    {
        delete[] str.pM;
        str.pM = NULL;
    }

    //4.申请新的空间
    str.pM = new char[strlen(tmp) + 1];
    memset(str.pM, 0, strlen(tmp) + 1);

    //5.拷贝用户输入的信息到堆区空间
    strcpy(str.pM, tmp);
    str.mSize = strlen(tmp);

    return in;
}

MyString.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

class MyString
{
    friend ostream &operator<<(ostream &out, MyString &str);
    friend istream &operator>>(istream &in, MyString &str);
public:
    MyString();
    MyString(int n, char c);//用户可以设定初始化字符串,n个c组成的字符串
    MyString(const MyString &str);
    MyString &operator=(const MyString &str);
    ~MyString();

    MyString operator+(const MyString &str);
    MyString operator+(const char *str);
    
    MyString &operator+=(const MyString &str);
    MyString &operator+=(const char *s);

    int Size();

    char &operator[](int index);


private:
    char *pM;//指向堆区空间
    int mSize;
    /*
    MyString s1;
    MyString s2;
    MyString s3 = s1 + s2;
    MyString s4 = s3 + "hello";

    s4 += s3;
    s4 += "hello";

    cout << s4 <<endl;
    cin >> s4;
    */
};

输出结果:

aaaaaaaaaa
bbb
aaaaaaaaaabbb
aaaaaaaaaabbbhello
a a a a a a a a a a b b b h e l l o
aaaaaaaaaabbbhelloccccc
aaaaaaaaaabbbhellocccccworld
posted @ 2022-10-15 18:56  CodeMagicianT  阅读(23)  评论(0编辑  收藏  举报