ClassStr
ClassStr.h
#pragma once
#include
using namespace std;
class ClassStr
{
public:
ClassStr();
ClassStr(const char* Str);
ClassStr(int nNumber);
ClassStr(ClassStr& cStr);
~ClassStr();
void operator= (ClassStr& cStr);
void operator= (int nNumber);
int operator+ (int nNumber);
int operator- (int nNumber);
char& operator[] (int nIndex);
const char* GetStr();
ClassStr& Insert(int nPosition,const char* str);
void FrontInsert(const char* str);
void TailInsert(const char* str);
ClassStr& Delete(int nIndex,int nCount);
int HeadFind(const char* str);
int TailFind(const char* str);
int HeadFindCount(const char* str);
int Modify(const char* nStr,const char* cStr,bool bmp = false);
private:
int NumberCount(int nNumber);
int GetDivNum(int NumberCount);
bool IsStr();
bool IsPnCount();
void FreeSpace();
void InitNumber(int nNumber);
int StrToNumber();
void FrontInsert(char* NewStr, int nSumLen, const char* str, int nLen);
void TailInsert(char* NewStr, int nSumLen, const char* str, int nLen);
void MiddleInsert(char* NewStr, int nSumLen, const char* str, int nLen,int nPosition);
int IsCmpByteHead(char chByte,int nIndex);
int IsCmpByteTail(char chByte, int nIndex);
bool IsCmpByteContinues(const char* str, int nLen, int nIndex, int nIn);
int IsCmpContinuesHead(const char* str, int nLen, int nIndex);
int IsCmpContinuesTail(const char* str, int nLen, int nIndex);
void Replace(const char* cStr,int nIndex);
private:
int m_nCount;
char* m_Str;
int* m_p_nCount;
};
ClassStr.cpp
#include "ClassStr.h"
ClassStr::ClassStr()
{
m_nCount = 0;
m_Str = nullptr;
m_p_nCount = new int(0);
}
ClassStr::ClassStr(const char* Str)
{
m_p_nCount = new int(0);
int nLen = strlen(Str);
m_nCount = nLen + 1;
m_Str = new char[m_nCount];
if (!IsStr())
{
cout << "str内存初始化失败" << endl;
}
else
{
memset(m_Str, 0, m_nCount);
memcpy_s(m_Str, nLen, Str, nLen);
}
}
ClassStr::ClassStr(int nNumber)
{
FreeSpace();
InitNumber(nNumber);
}
ClassStr::ClassStr(ClassStr& cStr)
{
m_nCount = cStr.m_nCount;
m_Str = cStr.m_Str;
m_p_nCount = cStr.m_p_nCount;
if (m_p_nCount != nullptr)
{
(*m_p_nCount)++;
}
}
void ClassStr::operator=(ClassStr& cStr)
{
FreeSpace();
m_Str = cStr.m_Str;
m_nCount = cStr.m_nCount;
m_p_nCount = cStr.m_p_nCount;
(*m_p_nCount)++;
}
void ClassStr::operator=(int nNumber)
{
FreeSpace();
InitNumber(nNumber);
}
int ClassStr::operator+(int nNumber)
{
int nTmp = StrToNumber();
nTmp = nTmp + nNumber;
return nTmp;
}
int ClassStr::operator-(int nNumber)
{
int nTmp = StrToNumber();
nTmp = nTmp - nNumber;
return nTmp;
}
char& ClassStr::operator[](int nIndex)
{
char* ch = nullptr;
if (nIndex < 0 || nIndex> m_nCount)
{
return *ch;
}
return m_Str[nIndex];
}
ClassStr::~ClassStr()
{
if (*m_p_nCount != 0)
{
(*m_p_nCount)--;
}
else
{
delete[] m_Str;
delete m_p_nCount;
}
}
const char* ClassStr::GetStr()
{
return m_Str;
}
ClassStr& ClassStr::Insert(int nPosition, const char* str)
{
if (0 > nPosition || nPosition > m_nCount || str == NULL)
{
cout << "插入错误:请选择正确的位置或str指针为NULL" << endl;
return *this;
}
int nLen = strlen(str);
int nSumLen = nLen + m_nCount;
char* NewStr = new char[nSumLen];
if (NewStr == nullptr)
{
cout << "内存申请失败" << endl;
return *this;
}
memset(NewStr,0,nSumLen);
if (nPosition == 0)
{
FrontInsert(NewStr, nSumLen, str, nLen);
}
else if (nPosition == m_nCount)
{
TailInsert(NewStr, nSumLen, str, nLen);
}
else
{
MiddleInsert(NewStr, nSumLen, str, nLen, nPosition);
}
delete[] m_Str;
m_Str = NewStr;
m_nCount = nSumLen;
return *this;
}
void ClassStr::FrontInsert(const char* str)
{
Insert(0, str);
}
void ClassStr::TailInsert(const char* str)
{
Insert(m_nCount, str);
}
ClassStr& ClassStr::Delete(int nPosition, int nCount)
{
int nLen = m_nCount - nPosition - 1;
if (nLen = nCount)
{
m_Str[nPosition] = '\0';
}
return*this;
}
int ClassStr::HeadFind(const char* str)
{
if (str == NULL)return -1;
int nLen = strlen(str);
int nIndex = 0;
int nCount = 0;
if (nLen == 1)
{
return IsCmpByteHead(str[0], nIndex);
}
if (nLen == (m_nCount -1))
{
if (IsCmpByteContinues(str, nLen, 0,0))
{
return 0;
}
}
return IsCmpContinuesHead(str, nLen, nIndex);
}
int ClassStr::TailFind(const char* str)
{
if (str == NULL)return false;
int nLen = strlen(str);
int nIndex = m_nCount - nLen -1;
int nCount = 0;
if (nLen == 1)
{
return IsCmpByteTail(str[0], nIndex);
}
if (nLen == (m_nCount - 1))
{
if (IsCmpByteContinues(str, nLen, 0,0))
{
return 0;
}
}
return IsCmpContinuesTail(str, nLen, nIndex);
}
int ClassStr::HeadFindCount(const char* str)
{
if (str == nullptr)return -1;
int nCount = 0;
int nLen = strlen(str);
int nIndex = 0;
do{
nIndex = IsCmpContinuesHead(str, nLen, nIndex);
if (nIndex == -1)
{
return nCount;
}
nCount++;
nIndex = nIndex + nLen;
} while (true);
return nCount;
}
int ClassStr::Modify(const char* nStr, const char* cStr, bool bmp)
{
int nCount = 0;
int nLen = strlen(cStr);
int nIndex = 0;
do
{
nIndex = IsCmpContinuesHead(nStr, nLen, nIndex);
if (nIndex == -1)
{
return nCount;
}
nCount++;
Replace(cStr,nIndex);
nIndex = nIndex + nLen;
} while (bmp);
return nCount;
}
int ClassStr::NumberCount(int nNumber)
{
int nLen = 0;
while (nNumber != 0)
{
nNumber = nNumber / 10;
nLen++;
}
return nLen;
}
int ClassStr::GetDivNum(int nLen)
{
int DivNum = 1;
while ( nLen > 1 )
{
DivNum = DivNum * 10;
nLen--;
}
return DivNum;
}
bool ClassStr::IsStr()
{
if (m_Str == nullptr)
{
return false;
}
return true;
}
bool ClassStr::IsPnCount()
{
if (m_p_nCount == nullptr)
{
return false;
}
return true;
}
void ClassStr::InitNumber(int nNumber)
{
m_p_nCount = new int(0);
int nLen = NumberCount(nNumber);
m_nCount = nLen + 1;
m_Str = new char[m_nCount];
if (!IsStr())
{
cout << "内存初始化失败" << endl;
}
else
{
memset(m_Str, 0, m_nCount);
int nDivNum = GetDivNum(nLen);
int nIndex = 0;
for (int i = 0; i < m_nCount - 1; i++)
{
m_Str[nIndex] = (char)(nNumber / (1 * nDivNum)) + '0';
nIndex++;
nNumber = nNumber % (1 * nDivNum);
nDivNum = nDivNum / 10;
}
}
}
int ClassStr::StrToNumber()
{
int nTmp = 0;
int nImu = GetDivNum(m_nCount - 1);
for (int i = 0; i < m_nCount - 1; i++)
{
char chCtr = m_Str[i];
int nNum = (int)chCtr - '0';
nTmp += nNum * nImu;
nImu = nImu / 10;
}
return nTmp;
}
void ClassStr::FrontInsert(char* NewStr, int nSumLen, const char* str,int nLen)
{
MiddleInsert(NewStr, nSumLen, str, nLen,0);
}
void ClassStr::TailInsert(char* NewStr, int nSumLen, const char* str, int nLen)
{
MiddleInsert(NewStr, nSumLen, str, nLen, m_nCount -1);
}
void ClassStr::MiddleInsert(char* NewStr, int nSumLen, const char* str, int nLen, int nPosition)
{
memcpy_s(NewStr, nSumLen, m_Str, nPosition);
memcpy_s(NewStr + nPosition, nSumLen, str, nLen);
memcpy_s(NewStr + nLen + nPosition, nSumLen, m_Str + nPosition, m_nCount - nPosition -1);
}
int ClassStr::IsCmpByteHead(const char chByte,int nIndex)
{
for (int i = nIndex; i < m_nCount -1; i++)
{
if (m_Str[i] == chByte)
{
return i;
}
}
return -1;
}
int ClassStr::IsCmpByteTail(const char chByte, int nIndex)
{
for (int i = nIndex; i >= 0; i--)
{
if (m_Str[i] == chByte)
{
return i;
}
}
return -1;
}
bool ClassStr::IsCmpByteContinues(const char* str, int nLen, int nIndex,int nIn)
{
for (int i = nIn; i < nLen; i++)
{
if (m_Str[nIndex] != str[i])
{
return false;
}
nIndex++;
}
return true;
}
int ClassStr::IsCmpContinuesHead(const char* str,int nLen,int nIndex)
{
int nLenCmp = 0;
int nIn = 1;
do {
nIndex = IsCmpByteHead(str[0], nIndex);
nLenCmp = m_nCount - 1 - nIndex;
if (nIndex == -1 || nLenCmp < nLen)
{
return -1;
}
if (IsCmpByteContinues(str, nLen, nIndex + nIn, nIn))
{
return nIndex;
}
nIndex++ ;
} while (true);
return -1;
}
int ClassStr::IsCmpContinuesTail(const char* str, int nLen, int nIndex)
{
int nLenCmp = 0;
int nIn = 1;
do {
nIndex = IsCmpByteTail(str[0], nIndex);
nLenCmp = m_nCount - 1 - nIndex;
if (nIndex == -1 || nLenCmp < nLen)
{
return -1;
}
if (IsCmpByteContinues(str, nLen, nIndex + nIn, nIn))
{
return nIndex;
}
nIndex--;
} while (true);
return -1;
}
void ClassStr::Replace(const char* cStr, int nIndex)
{
int nLen = strlen(cStr);
for (int i = 0; i < nLen; i++)
{
m_Str[nIndex] = cStr[i];
nIndex++;
}
}
void ClassStr::FreeSpace()
{
if (!IsStr())
{
return;
}
delete[] m_Str;
delete m_p_nCount;
}