📄 string.h
字号:
#ifndef HEADER_STRING
#define HEADER_STRING
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
const int MaxSize = 500;
template<typename T>
class String
{
public:
String ();
~String();
String (T *p);
String (T array[],int n);
String (String &other);
int GetLength();
int StrCmp(String &other);
void StrCat(const String &other);
void Insert(int i,String &other);
void Delete(int i,int len); //删除从第i位置开始的len个元素
void Delete(int LineNum);
void StrPrint();
void Input(T *p);
void InsertBeforeLine(int line,String &other);
void InsertAfterLine(int line,String &other);
void InsertInLine(int line,int begin,String &other);
void DeleteInLine(int line,int begin,int len);
bool FindAndReplace(String &Old,String &New);
int BF(String &other,int startPos);
// int KMP(String &other,int startPos);
bool SaveToFile(char *FileName);
bool ReadFromFile(char *FileName);
int BeginOfLine(int LineNum);
friend ostream &operator << (ostream &o,String<T> &STR);
// friend istream &operator >> (istream &i,String<T> &STR);
//private:
public:
int m_Length;
T m_array[MaxSize];
};
template<typename T>
String<T>::String()
{
m_Length = 0;
}
template<typename T>
String<T>::String(T *p)
{
m_Length = strlen(p);
for(int i = 0;i < m_Length;i ++)
m_array[i] = p[i];
}
template<typename T>
String<T>::String(T array[],int n)
{
for(int i = 0;i < n;i ++)
m_array[i] = array[i];
m_Length = n;
}
template<typename T>
String<T>::String(String<T> &other)
{
for(int i = 0;i < other.m_Length;i ++)
{
m_array[i] = other.m_array[i];
}
}
template<typename T>
int String<T>::GetLength()
{
return m_Length;
}
template<typename T>
void String<T>::Insert(int i,String<T> &other)
{
if(m_Length + other.m_Length > MaxSize)
throw "上溢";
else
{
for(int j = m_Length - 1; j >= i;j --)
{
m_array[j + other.m_Length] = m_array[j];
}
for(j = 0; j < other.m_Length;j ++)
m_array[i ++] = other.m_array[j];
}
m_Length += other.m_Length;
}
template<typename T>
void String<T>::StrPrint()
{
for(int i = 0;i < m_Length; i ++)
cout<<m_array[i];
cout<<endl;
}
template<typename T>
void String<T>::Input(T *p)
{
m_Length = strlen(p);
for(int i = 0;i < m_Length; i ++)
{
m_array[i] = p[i];
}
}
template<typename T>
void String<T>::StrCat(const String &other)
{
if(m_Length + other.m_Length >MaxSize)
throw "上溢";
else
for(int i = 0;i < other.m_Length;i ++)
m_array[m_Length ++] = other.m_array[i];
}
template<typename T>
void String<T>::Delete(int i,int len)
{
int back = i + len;
if(i + len > m_Length)
throw "上溢";
else
{
for(int j = i + len; j <m_Length; j ++)
{
m_array[i ++] = m_array[j];
}
}
m_Length -= len;
}
template<typename T>
void String<T>::Delete(int LineNum)
{
int iBegin;
if(LineNum == 1)
iBegin = 0;
else
iBegin = BeginOfLine(LineNum - 1);
int iEnd = BeginOfLine(LineNum);
if(LineNum == 1)
iEnd += 1;
this->Delete(iBegin,iEnd - iBegin);
}
template<typename T>
int String<T>::StrCmp(String &other)
{
for(int i = 0;i < m_Length;i ++)
{
if(m_array[i] > other.m_array[i])
return 1;
else if(m_array[i] < other.m_array[i])
return -1;
}
if(m_Length < other.m_Length)
return -1;
return 0;
}
template<typename T>
void String<T>::InsertBeforeLine(int line,String &other)
{
this->Insert(BeginOfLine(line - 1) + 1,other);
}
template<typename T>
void String<T>::InsertAfterLine(int line,String &other)
{
this->Insert(BeginOfLine(line) + 1,other);
}
template<typename T>
int String<T>::BeginOfLine(int LineNum)
{
int EnterNum = 0;
for(int i = 0;;i ++)
{
if(m_array[i] == 10)
{
EnterNum ++;
}
if(EnterNum == LineNum)
return i;
}
}
template<typename T>
void String<T>::InsertInLine(int line,int begin,String &other)
{
int i = BeginOfLine(line - 1) + 1;
this->Insert(i + begin,other);
}
template<typename T>
void String<T>::DeleteInLine(int line,int begin,int len)
{
int i = BeginOfLine(line - 1) + 1;
this->Delete(i + begin,len);
}
template<typename T>
bool String<T>::FindAndReplace(String &Old,String &New)
{
/* int i = 0;
int j = 0;
bool flag;
while(i < Old.m_Length && j < this->m_Length)
{
if(Old.m_array[i] == this->m_array[j])
{
i ++;
j ++;
}
else
{
j ++;
i = 0;
if(this->m_Length - j < Old.m_Length)
{
flag = false;
break;
}
}
if(i == Old.m_Length)
{
int iBegin = j - Old.GetLength();
this->Delete(iBegin, Old.GetLength());
this->Insert(iBegin, New);
return true;
}
}
return flag;
*/
int DeletePos = 0;
while(true)
{
DeletePos = this->BF(Old,DeletePos);
if(DeletePos == -1)
break;
this->Delete(DeletePos,Old.m_Length);
this->Insert(DeletePos,New);
DeletePos += New.m_Length;
}
return true;
}
template<typename T>
bool String<T>::SaveToFile(char *FileName)
{
ofstream out(FileName);
if(out.is_open())
{
out<<m_Length<<"\n";
for(int i = 0;i < m_Length;i ++)
out<<m_array[i];
return true;
}
return false;
}
template<typename T>
int String<T>::BF(String &other,int startPos)
{
int i = startPos;
int j = 0;
while(i < other.m_Length && j < this->m_Length)
{
if(other.m_array[i] == this->m_array[j])
{
i ++;
j ++;
}
else
{
j = j - i + 1;
i = 0;
}
}
if(i == other.m_Length)
return j - i;
else
return -1;
}
template<typename T>
bool String<T>::ReadFromFile(char *FileName)
{
m_Length = 0;
ifstream in(FileName);
if (in.is_open())
{
int j = 0;
in >> m_Length;
for(string s; getline(in,s);)
{
if(s == "") //读完m_Length的时候会有一段空白,将其滤掉
continue;
int i = 0;
while(s[i])
{
m_array[j] = s[i++];
j ++;
}
m_array[j] = 10;
j ++;
}
}
return false;
}
template<typename T>
ostream &operator << (ostream &o,String<T> &STR)
{
for(int i = 0;i < STR.m_Length;i ++)
{
o<<STR.m_array[i];
}
return o;
}
template<typename T>
istream &operator >> (istream &i,String<T> &STR)
{
}
template<typename T>
String<T>::~String()
{
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -