字串的過載程式

mf1crystal發表於2018-01-14

MyString.h

#ifndef __MYSTRING_H__
#define __MYSTRING_H__

#include <iostream>

using namespace std;

class MyString
{
    // 過載 << 操作符
    friend std::ostream& operator<<(std::ostream &out, MyString &str);

    // 過載 >> 操作符
    friend std::istream& operator>>(std::istream& in, MyString &str);
public:
    MyString();                        // 無參構造
    MyString(const char *s);           // 有參構造
    MyString(int len, char data = 0);  // 有參構造
    MyString(const MyString &s);       // 拷貝構造

    ~MyString();                       // 解構函式

// 過載=、[]操作符
public:
    MyString& operator=(const char *s);       // 普通字串賦值
    MyString& operator=(const MyString &s);   // 類物件之間賦值
    char & operator[](int index);

// 過載 + 運算子
public:
    MyString& operator+(const char *str);
    MyString& operator+(const MyString &s);

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

// 過載 == !=
public:
    bool operator==(const char *str) const;
    bool operator==(const MyString &str) const;

    bool operator!=(const char *str) const;
    bool operator!=(const MyString &str) const;

// 過載 < >
public:
    bool operator>(const char *str) const;
    bool operator>(const MyString &str) const;

    bool operator<(const char *str) const;
    bool operator<(const MyString &str) const;

public:
    const char *c_str()
    {
        return m_p;
    }

    char *c_str2()
    {
        return m_p;
    }

    int leng()
    {
        return m_len;
    }
private:
    int m_len;    // 字串長度
    char *m_p;    // 字串資料
};




#endif // __MYSTRING_H__

MyString.cpp

#include "MyString.h"
#include <string.h>

std::ostream& operator<<(std::ostream &out, MyString &str)
{
    out << str.m_p << " ";

    return out;
}

std::istream& operator>>(std::istream& in, MyString &str)
{
    in >> str.m_p;
    //fgets (str.m_p, str.m_len, stdin);
    return in;
}

MyString::MyString()
{
    m_len = 0;
    m_p = new char[1];
}

MyString::MyString(const char *s)
{
    if (s == NULL)
    {
        m_len = 0;
        m_p = new char[1];
    }
    else
    {
        m_len = strlen(s);
        m_p = new char[m_len + 1];
        strcpy (m_p, s);
    }
}

MyString::MyString(int len, char data)
{
    m_len = len;
    m_p = new char[m_len+1];
    for (int i = 0; i < m_len; i++)
    {
        m_p[i] = data;
    }
}


MyString::MyString(const MyString &s)
{
    m_len = s.m_len;
    m_p = new char[m_len+1];
    strcpy (m_p, s.m_p);
}


MyString::~MyString()
{
    delete[] m_p;
    m_p = NULL;
    m_len = 0;
}


MyString& MyString::operator=(const char *s)
{
    // 先釋放原有空間
    delete [] m_p;
    if (s == NULL)
    {
        m_len = 0;
        m_p = new char[1];
    }
    else
    {
        m_len = strlen(s);
        m_p = new char[m_len+1];
        strcpy(m_p, s);
    }

    return *this;
}

MyString& MyString::operator=(const MyString &s)
{
    // 是本身的情況下,直接返回當前物件
    if (this == &s)
        return *this;

    // 先釋放
    delete[] m_p;

    m_len = s.m_len;
    m_p = new char[m_len + 1];
    strcpy (m_p, s.m_p);

    return *this;
}

MyString& MyString::operator+(const char *str)
{
    if (str == NULL)
    {
        return *this;
    }

    m_len += strlen(str);
    char *tmp = m_p;            // 將原有資料儲存下來
    m_p = new char[m_len + 1];  // 分配新空間
    strcpy (m_p, tmp);          // 將原來的資料匯入到新空間
    strcat (m_p, str);          // 將新資料粘到原有資料後面
    delete [] tmp;              // 釋放原來的空間

    return *this;
}

MyString& MyString::operator+(const MyString &s)
{
    m_len += s.m_len;
    char *tmp = m_p;            // 將原有資料儲存下來
    m_p = new char[m_len + 1];  // 分配新空間
    strcpy (m_p, tmp);          // 將原來的資料匯入到新空間

    if (this == &s)             // 如果是自己,複製原有空間內容
        strcat (m_p, tmp);
    else                        // 複製新字串的內容
        strcat (m_p, s.m_p);

    delete [] tmp;              // 釋放原來的空間

    return *this;
}

MyString& MyString::operator+=(const char *str)
{
    *this = *this + str;

    return *this;
}

MyString& MyString::operator+=(const MyString &s)
{
    *this = *this + s;
    return *this;
}

char & MyString::operator[](int index)
{
    return m_p[index];
}

bool MyString::operator==(const char *str) const
{
    if (str == NULL)
    {
        if (m_len == 0)
            return true;
        else
            return false;
    }
    else
    {
        if (strcmp(m_p, str) == 0)
            return true;
        else
            return false;
    }
}

bool MyString::operator==(const MyString &str) const
{
    return *this == str.m_p;
}

bool MyString::operator!=(const char* str) const
{
    return !(*this == str);
}

bool MyString::operator!=(const MyString &str) const
{
    return !(*this == str.m_p);
}

bool MyString::operator>(const char *str) const
{
    if(str == NULL)
        return true;

    int i = 0;
    int len = (m_len < strlen(str) ? m_len : strlen(str)); // 儲存較短的字串的長度
    while (m_p[i] == str[i] && i<len)
        i++;

    return (m_p[i] > str[i] ? true : false);
}

bool MyString::operator>(const MyString &str) const
{
    return *this > str.m_p;
}

bool MyString::operator<(const char *str) const
{
    if(str == NULL)
        return true;

    int i = 0;
    int len = (m_len < strlen(str) ? m_len : strlen(str)); // 儲存較短的字串的長度
    while (m_p[i] == str[i] && i<len)
        i++;

    return (m_p[i] < str[i] ? true : false);
}

bool MyString::operator<(const MyString &str) const
{
    return *this < str.m_p;
}

main.cpp

#include <iostream>
#include "MyString.h"

int main()
{
    MyString str = "hello";
    char * p = str.c_str2();

    printf ("%s\n", str.c_str());

    return 0;
}


int main7()
{
    MyString str1 = "chelo";
    MyString str2 = "world";

    if (str1 > "bhello")
    {
        printf ("str1 > hello\n");
    }

    if (str1 > str2)
    {
        printf ("str1 > str2\n");
    }
    else
    {
        printf ("str1 < str2\n");
    }

    return 0;
}



int main6()
{
    MyString str1 = "helo";
    MyString str2 = "world";

    if (str1 == "hello")
    {
        printf ("str1 == hello\n");
    }
    else
    {
        printf ("str1 != hello\n");
    }

    if (str1 == str2)
    {
        printf ("str1 == str2\n");
    }
    else
    {
        printf ("str1 != str2\n");
    }

    return 0;
}

int main5()
{
    MyString str1 = "hello";

    std::cout << str1[3] << std::endl;
    str1[3] = 'w';
    std::cout << str1 << std::endl;

    return 0;
}

int main4()
{
    MyString str1 = "hello ";
    MyString str2 = "world";

    str1 = str1 + "borld";  
    // str1 = str1 + str1;
    // str1 += "world";
    //str1 += str2;

    std::cout << str1 << std::endl;

    return 0;
}

int main3()
{
    MyString str1;

    // MyString & operator(MyString &str, const char *s)
    str1 = "hello";   // 過載賦值操作符 = 
    //str1 = NULL;
    std::cout << str1 << std::endl;

    MyString str2;
    str2 = str1;
    std::cout << str2 << std::endl;

    str2 = str2;

    return 0;
}

int main2()
{
    MyString str1 = "hello world";   // 用一個字串去初始化類物件

    // << 
    // ostream & operator<<(ostream &out, MyString &str)
    std::cout << str1 << std::endl;

    MyString str2(4);

    // >> 
    // istream& operator>>(istream& in, MyString &str);
    std::cout << "請輸入字串:" ;
    std::cin >> str2;
    std::cout << str2 << std::endl;

    return 0;
}


int main8()
{
    MyString str;                    // 空字串
    MyString str1 = "hello world";   // 用一個字串去初始化類物件
    cout << str1 << endl;
    MyString str2 = NULL;            // 空字串
    MyString str3 = str1;            // 用一個字串物件初始化當前物件
    MyString str4(10, 'a');           // 長度為10,每一個元素都是'a'
    cout << str4 << endl;
    MyString str5(20); 

    return 0;
}

相關文章