C++运算符编写CMyCString类

来源:互联网 发布:2017网络热词及解释 编辑:程序博客网 时间:2024/06/06 01:58
#include <string>
#include <iostream>

using namespace std;

struct Node
{
    char c;
    Node* pNext;
};

Node* phead = NULL;
Node* pEnd = NULL;

void Push_Back(char c)   // 在链表的尾部添加
{
    //申请一个节点
    Node* temp = new Node;
    temp->c = c;
    temp->pNext = NULL;

    // 把这个节点放到链表上
    if (!phead)
    {
        phead = temp;
    }
    else
    {
        pEnd->pNext = temp;
    }
    pEnd = temp;
}

char Pop_Front()   //从链表上去掉
{
    if (!phead)
    {
        return 0;
    }
    else
    {
        //  记住节点中字符
        char c = phead->c;
        //  记住要删除的这个头节点
        Node* bj = phead;
        phead = phead->pNext;
        //  删除bj
        delete bj;
        return c;
    }
}

class CMyString
{
private:
    char* m_pStr;
public:
    friend ostream& operator<<(ostream& os,CMyString& c_str);//重载<<,比较常用
    friend CMyString operator+(char* str,CMyString& c_str);//char*变量+CMyString 对象
    friend istream& operator>>(istream& is,CMyString& c_str);//重载>>,比较常用
public:
    CMyString()
    {
        m_pStr = new char[1];
        *m_pStr = 0;
    }
    CMyString(char* str)//将字符串给对象赋值
    {
        m_pStr = new char[strlen(str)+1];
        strcpy(this->m_pStr,str);
    }
    CMyString(const CMyString& c_str)//拷贝构造函数
    {
        m_pStr = new char[strlen(c_str.m_pStr)+1];
        strcpy(this->m_pStr,c_str.m_pStr);
    }
    ~CMyString()
    {
        delete[] m_pStr;
        m_pStr = NULL;
    }
    CMyString& operator=(char* str)//重载赋值运算符
    {
        if (this->m_pStr != NULL)//先释放已经申请的空间
        {
            delete[] this->m_pStr;
            this->m_pStr = NULL;
        }
        
        this->m_pStr = new char[strlen(str)+1];//重新申请空间
        strcpy(this->m_pStr,str);

        return *this;
    }
    CMyString& operator=(CMyString& c_str)//重载=,对象给对象赋值
    {
        if ( this == &c_str)
        {
            return *this;
        }

        char* str = new char[strlen(c_str.m_pStr)+1];

        if (this->m_pStr != NULL)
        {
            delete[] this->m_pStr;
            this->m_pStr = NULL;
        }

        this->m_pStr = str;

        strcpy(this->m_pStr,c_str.m_pStr);

        return *this;
    }
    CMyString operator+(char* str)//CMyString对象+char*字符串
    {
        CMyString temp;
        temp.m_pStr = new char[strlen(str)+strlen(this->m_pStr)+1];
        strcpy(temp.m_pStr,this->m_pStr);
        strcat(temp.m_pStr,str);
        return temp;
    }
    CMyString operator+(CMyString& c_str)
    {
        CMyString temp;
        temp.m_pStr = new char[strlen(c_str.m_pStr)+strlen(this->m_pStr)+1];
        strcpy(temp.m_pStr,this->m_pStr);
        strcat(temp.m_pStr,c_str.m_pStr);
        return temp;
    }
    bool operator==(char* str)//CMyString和char*字符串是否相等
    {
        if (!strcmp(this->m_pStr,str))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    char operator[](int index)
    {
        if (strlen(this->m_pStr) > index && index >= 0)
        {
            return this->m_pStr[index];
        }
        return -1;
    }
};

CMyString operator+(char* str,CMyString& c_str)//类外重载操作符
{
    CMyString temp;
    temp.m_pStr = new char[strlen(c_str.m_pStr)+strlen(str)+1];
    strcpy(temp.m_pStr,str);
    strcat(temp.m_pStr,c_str.m_pStr);
    return temp;
}
ostream& operator<<(ostream& os,CMyString& c_str)
{
    os << c_str.m_pStr ;
    return os;
}
istream& operator>>(istream& is,CMyString& c_str)
{
    if (c_str.m_pStr != NULL)
    {
        delete[] c_str.m_pStr;
    }

    char c;
    int nLength = 0;
    while( (c=getchar()) != '\n')
    {
        //  把这个字符放到链表里
        Push_Back(c);
        //统计输入字符的个数
        nLength++;
    }
    c_str.m_pStr = new char[nLength+1];
    //   把链表的 字符 放到 数组里
    for (int i=0;i<nLength+1;i++)
    {
        c_str.m_pStr[i] = Pop_Front();
    }
    return is;
}

int main()
{

     CMyString str;
     
    cout << "asdasdasd" << endl;

     CMyString str1("Asdasd");//   = "Asdasd";
 
     CMyString str2(str);
    
    CMyString str3 = "asdasd";

    cout << str << endl;
    cout << str1 << endl;
    cout << str2 << endl;
    cout << str3 << endl;
    cout << "------------------------" << endl;

    str = "ccccccc";
    str3 = str2 = str;

    cout << str << endl;
    cout << str1 << endl;
    cout << str2 << endl;
    cout << str3 << endl;
    cout << "------------------------" << endl;
    
    str3 = str1+"LLLLLL";
    str2 = str3+str1;
    str1 = "uuuuuu"+str2;

    cout << str << endl;
    cout << str1 << endl;
    cout << str2 << endl;
    cout << str3 << endl;
    cout << "------------------------" << endl;  

    cout << (str=="cccccc") << endl;

    cout << str1[10] << endl;
    cin >> str1;
    cout << str1 << endl;

    system("pause");
    return 0;
}
0 0
原创粉丝点击