#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
//我的字符串类
class MyString
{
friend ostream& operator<<(ostream& out, const MyString& str);
friend istream& operator>>(istream& in, MyString& str);
public:
MyString();
MyString(const char* str); //MyString str="abc";
MyString(const MyString& str);
~MyString();
MyString& operator=(const MyString& str);
MyString& operator=(const char *str);
char& operator[](int pos);
MyString operator+(const char* str);
MyString operator+(const char c);
MyString operator+(const MyString &str);
MyString operator+(int n);
bool operator==(const char* str);
bool operator==(const MyString& str);
private:
char* pString;//维护底层堆区开辟的字符数组
int m_Size;//长度
};
#include "mystring.h"
//重载 左移
ostream& operator<<(ostream& out, const MyString& str)
{
out << str.pString;
return out;
}
//右移
istream& operator>>(istream& in, MyString& str)
{
if (str.pString)
{
delete str.pString;
str.pString = NULL;
str.m_Size = 0;
}
char buf[1024] = {0};
in >> buf;
str.m_Size = strlen(buf);
str.pString = new char[str.m_Size + 1];
strcpy(str.pString, buf);
return in;
}
MyString::MyString()
{
m_Size = 0;
pString = NULL;
}
MyString::MyString(const char* str)
{
m_Size = strlen(str);
pString = new char[m_Size + 1];
strcpy(pString, str);
}
MyString::MyString(const MyString& str)
{
m_Size = strlen(str.pString);
pString = new char[m_Size + 1];
strcpy(pString, str.pString);
}
MyString::~MyString()
{
if (pString)
{
delete pString;
pString == NULL;
}
}
MyString& MyString::operator=(const MyString& str)
{
if (pString)
{
delete pString;
pString == NULL;
}
m_Size = strlen(str.pString);
pString = new char[m_Size + 1];
strcpy(pString, str.pString);
return *this;
}
MyString& MyString::operator=(const char* str)
{
if (pString)
{
delete pString;
pString == NULL;
}
m_Size = strlen(str);
pString = new char[m_Size + 1];
strcpy(pString, str);
return *this;
}
char& MyString::operator[](int pos)
{
return pString[pos];
}
MyString MyString::operator+(const char c)
{
int newSize = m_Size + 1 + 1;
char str[2] = {0};
str[0] = c;
return *this+str;
}
MyString MyString::operator+(const char* str)
{
int newSize = m_Size + strlen(str) + 1;
char *temp=new char[newSize];
memset(temp, 0, newSize);
strcat(temp, pString);
strcat(temp, str);
MyString newString(temp);
delete[] temp;
return newString;
}
MyString MyString::operator+(const MyString& str)
{
int newSize = m_Size + strlen(str.pString) + 1;
char* temp = new char[newSize];
memset(temp, 0, newSize);
strcat(temp, pString);
strcat(temp, str.pString);
MyString newString(temp);
delete[] temp;
return newString;
}
MyString MyString::operator+(int n)
{
char buff[1024] = {0};
int i=0;
while (n)
buff[i++] = n % 10 + '0', n /= 10;
for (int j = 0; j < i / 2; j++)
{
char tmp = buff[j];
buff[j] = buff[i - j - 1];
buff[i - j - 1] = tmp;
}
return *this+buff;
}
bool MyString::operator==(const char* str)
{
if (strcmp(pString, str) == 0)
return true;
return false;
}
bool MyString::operator==(const MyString& str)
{
if (strcmp(pString, str.pString) == 0)
return true;
return false;
}