-- 作者:一分之千
-- 发布时间:4/26/2007 7:38:00 PM
--
我自己编写cstring类,基本使用字符指针实现,使用了cout ,可以参考一下~~ //*****************CShlString.h #if _HCSHLSTRING_ #define _HCSHLSTRING_ #endif class CShlString { public: CShlString(); CShlString(char); CShlString(char *); CShlString(char,int); CShlString(CShlString&); ~CShlString(); void display(); //对应于CString类中的函数 int StringLength(); //CString::GetLength(); bool StringIsEmpty(); //CString::IsEmpty(); void StringEmpty(); //CString::Empty(); char FindChar(int); //CString::GetAt(); void StringOverWrite(int,char); //CString::SetAt(); int StringCompare(char *); //CString::Compare() int StringCompare(CShlString); CShlString StringJoin(char); //strcat() CShlString StringJoin(char*); CShlString StringJoin(CShlString&); CShlString MidFindChar(int,int); //CString::Mid(); CShlString RightFindChar(int); //CString::Right(); int ReverseFindChar(char); //CString::ReverseFind(); CShlString CharIncluding(CShlString);//CString::SpanIncluding(); CShlString StringCaptical(); //CString::MakeUpper(); CShlString StringLowerCase(); //CString::MakeLower(); void StringReverse(); //CString::MakeReverse(); int StringReplace(char,char); //CString::Replace(); int StringReplace(char*,char*); int StringRemove(char); //CString::Remove(); int StringInsert(int,CShlString); //CString::Insert(); int StringDelete(int,int); //CString::Delete(); friend CShlString operator +(CShlString,char);//operator + friend CShlString operator+(CShlString,char *); CShlString operator+(CShlString&); CShlString operator=(CShlString&); CShlString operator+=(CShlString&);// operator +=; char operator[](int); // operator []; private: char *p; }; //****************CShlString.cpp #include "CShlString.h" #include "iostream.h" CShlString::CShlString() { p=new char[1]; *p='\0'; } CShlString::CShlString(char ch) { p=new char[2]; *p=ch; *(p+1)='\0'; } CShlString::CShlString(char *pCh) { int i,m=0; char *pStr; pStr=pCh; for(m=0;*pStr;pStr++,m++); p=new char[m+1]; for(i=0;i<m;i++) *(p+i)=*(pCh+i); *(p+i)='\0'; } CShlString::CShlString(char ch,int nRepeat) { p=new char[nRepeat+1]; for(int i=0;i<nRepeat;i++) *(p+i)=ch; *(p+i)='\0'; } CShlString::CShlString(CShlString& Str) { char *pStr; int m=0; pStr=Str.p; for(int i=0;*pStr;i++,pStr++); pStr=Str.p; p=new char[i+1]; while(*pStr) { *(p+m)=*pStr; pStr++; m++; } *(p+m)='\0'; } CShlString::~CShlString() { // char *pp; // pp=p; // for(int i=0;*pp;i++,pp++); // if(i!=0) // delete []p; } void CShlString::display() { cout<<p<<endl; } int CShlString::StringLength() { int m=0; char *pStr; pStr=p; for(m=0;*pStr;pStr++,m++); return m; } bool CShlString::StringIsEmpty() { if(*p) { cout<<"This string is not empty!"<<endl; return true; } cout<<"This string is empty!"<<endl; return false; } void CShlString::StringEmpty() { if(*p) *p='\0'; } char CShlString::FindChar(int n) { char *pCh; pCh=p; for(int i=0;*pCh;i++,pCh++); if(n>i) { cout<<"Wrong number n!"<<endl; return '\0'; } else for(int j=0;j<n;j++,pCh++); return *pCh; } void CShlString::StringOverWrite(int m,char ch) { char *pp; pp=p; for(int i=0;*pp;i++,pp++); if(m>i) { cout<<"Wrong number n!"<<endl; return; } else *(p+m-1)=ch; } int CShlString::StringCompare(char* pStr) { char *pCh; int m; pCh=p; while (*pCh&&*pStr) { if(*pCh>*pStr) {m=1;break; } else if(*pCh<*pStr) {m=-1;break; } else {m=0;pCh++;pStr++; } } if(*pCh)m=1; if(*pStr)m=-1; return m; } int CShlString::StringCompare(CShlString Str) { char *pCh,*pStr; CShlString Com_Str; Com_Str=Str; pStr=Com_Str.p; int m; pCh=p; while (*pCh&&*pStr) { if(*pCh>*pStr) { m=1;break; } else if(*pCh<*pStr) { m=-1;break; } else { m=0;pCh++;pStr++; } } if(*pCh)m=1; if(*pStr)m=-1; return m; } CShlString CShlString::StringJoin(char ch) { CShlString str; char *pStr,*pp; pp=p; for(int i=0;*pp;i++,pp++); pStr=new char[i+2]; for(int j=0;j<i;j++) *(pStr+j)=*(p+j); *(pStr+j)=ch; *(pStr+j+1)='\0'; str.p=pStr; return str; } CShlString CShlString::StringJoin(char* pCh) { char *pStr,*pp; CShlString str; pp=p; for(int i=0;*pp;i++,pp++); for(int n=0;*(pCh+n);n++); pStr=new char[i+n+2]; for(int j=0;j<i;j++) *(pStr+j)=*(p+j); while (*pCh) { *(pStr+j)=*pCh; pCh++; j++; } *(pStr+j)='\0'; str.p=pStr; return pStr; } CShlString CShlString::StringJoin(CShlString& ch) { char *pCh,*pStr,*pp; CShlString str; pCh=ch.p; pp=p; for(int i=0;*pp;i++,pp++); for(int n=0;*(pCh+n);n++); pStr=new char[i+n+2]; for(int j=0;j<i;j++) *(pStr+j)=*(p+j); while(*pCh) { *(pStr+j)=*pCh; j++; pCh++; } *(pStr+j)='\0'; str.p=pStr; return str; } CShlString CShlString::MidFindChar(int mStart,int mLength) { char *pCh,*pp; CShlString Str; pp=p; for(int j=0;*pp;j++,pp++); pCh=new char[j+1]; for(int i=mStart;i<(mStart+mLength);i++) *(pCh+i-mStart)=*(p+i-1); *(pCh+i-mStart)='\0'; Str.p=pCh; return Str; } CShlString CShlString::RightFindChar(int mLength) { char *pCh,*pStr; CShlString Str; pStr=p; int i,j=0; for(i=0;*pStr;i++,pStr++); pCh=new char[i]; while (mLength) { *(pCh+j)=*(p+i-mLength); j++; mLength--; } *(pCh+j)='\0'; Str.p=pCh; return Str; } int CShlString::ReverseFindChar(char ch) { char *pp; int i,m=-1; bool flag=false; pp=p; for(i=0;*pp;i++,pp++); pp=p; while(i) { if(*(pp+i)==ch) { flag=true; break; } m++; // pp++; i--; } if(flag) return m; else return -1; } CShlString CShlString::CharIncluding(CShlString Str) { char *pCh,*pp,*pStr; int i=0; pStr=Str.p; pp=p; for(int j=0;*(p+j);j++); pCh=new char[j+1]; while(*pp) { pStr=Str.p; while(*pStr) { if(*pStr==*pp||*pStr==(*pp+32)||*pStr==(*pp-32)) {*(pCh+i)=*pp; i++;} pStr++; } pp++; } *(pCh+i)='\0'; Str.p=pCh; return Str; } CShlString CShlString::StringCaptical() { char *pp,*pCh; CShlString str; int i=0; for(int j=0;*(p+j);j++); pCh=new char[j+1]; pp=p; while(*pp) { if(*pp>=97&&*pp<=122) *(pCh+i)=*pp-32; else *(pCh+i)=*pp; pp++; i++; } *(pCh+i)='\0'; str.p=pCh; return str; } CShlString CShlString::StringLowerCase() { char *pCh,*pp; CShlString str; int i=0; for(int j=0;*(p+j);j++); pCh=new char[j+1]; pp=p; while(*pp) { if(*pp>=65&&*pp<=90) *(pCh+i)=*pp+32; else *(pCh+i)=*pp; i++; pp++; } *(pCh+i)='\0'; str.p=pCh; return str; } void CShlString::StringReverse() { char *pStr,*pCh; int i=0,j=0; pStr=p; for(i=0;*pStr;i++,pStr++); pStr=p; pCh=new char[i+1]; while(i) { *(pCh+j)=*(pStr+i-1); j++; i--; } *(pCh+j)='\0'; delete p; p=pCh; } int CShlString::StringReplace(char ch,char str) { int i=0,j,n=0; char *pp; pp=p; for(i=0;*pp;i++,pp++); for(j=0;j<=i;j++) { if(*(p+j)==ch) { *(p+j)=str; n++; } } return n; } int CShlString::StringReplace(char* pCh,char *pStr) { char *pp,*pSave; int i=0,j=0,NumReturn; pp=p; for(int m=0;*(pCh+m);m++); for(int n=0;*pp;n++,pp++); for(int k=0;*(pStr+k);k++); pSave=new char[n+k-m+1]; // CShlString Save; // pSave=Save.p; pp=p; while( i<=n&&j<=m) { if(*(pp+i)==*(pCh+j)) { i++;j++; } else {i=i-j+1;j=0; } if(j>=m) break; } if(j>=m) { for(int l=0;l<i-m;l++,pp++) *(pSave+l)=*pp; for(l=i-m;l<(i-m+k);l++,pStr++) *(pSave+l)=*pStr; for(l=i-m+k;*(pp+m);l++,*pp++) *(pSave+l)=*(pp+m); *(pSave+l)='\0'; delete []p; p=pSave; pp=p; for(NumReturn=0;*pp;*pp++,NumReturn++); return NumReturn; } else return n; } int CShlString::StringRemove(char ch) { int i=0,j,k,n=0; char *pp; pp=p; for(i=0;*pp;i++,pp++); pp=p; for(j=0;j<=i;j++) { if(*(p+j)==ch) { k=j; n++; while(k<=i) { *(p+k)=*(p+k+1);//将p中的ch字符清空。 k++; } k=0; } } return n; } int CShlString::StringInsert(int n,CShlString str) { char *pp,*pStr,*pCh; int i,j,k,m=0; pStr=str.p; pp=p; k=str.StringLength(); for(i=0;*pp;i++,pp++); pp=p; pCh=new char[i+k+2]; if(i<n) { cout<<"error number n!"; return i; } for(j=0;j<n-1;j++,pp++) { *(pCh+j)=*pp; m++;} for(j=n-1;j<n+k-1;j++) { *(pCh+j)=*(pStr+j-n+1); m++;} for(j=n+k-1;j<k+i;j++,pp++) { *(pCh+j)=*pp; m++;} *(pCh+j)='\0'; delete []p; p=pCh; return m; } int CShlString::StringDelete(int nStart,int nLength) { char *pCh,*pp; CShlString str; int m; str.p=p; m=str.StringLength(); pp=p; for(int n=0;*pp;n++,pp++); pCh=new char[n+1]; if(nStart>n) { cout<<"Wrong Statr number !"<<endl; return m; } pp=p; for(int i=0;i<nStart;i++,pp++) *(pCh+i)=*pp; for(i=nStart;i<(nStart+nLength)&&*pp;i++,pp++) *(pCh+i)=*(pp+nLength); while(*pp) { *(pCh+i)=*(pp+nLength); pp++; i++; } *(pCh+i)='\0'; str.p=pCh; delete p; p=pCh; m=str.StringLength(); return m; } CShlString operator +(CShlString ch,char str) { char *pCh,*pStr,*pp; CShlString Result; int i=0; pCh=ch.p; for(int m=0;*pCh;m++,pCh++); pp=new char[m+2]; pStr=&str; pCh=ch.p; while(*pCh) { *(pp+i)=*pCh; pCh++; i++;} while (*pStr) { *(pp+i)=*pStr; pStr++; i++; } *(pp+i)='\0'; Result.p=pp; return Result; } CShlString CShlString::operator +(CShlString& str) { char *pStr,*pCh,*pp; int i=0; CShlString Result; pStr=str.p; pp=p; for(int m=0;*(pStr+m);m++); for(int n=0;*(pp+n);n++); pCh=new char[m+n+1]; while(*pp) { *(pCh+i)=*(pp); i++; pp++; } while(*pStr) { *(pCh+i)=*(pStr); i++; pStr++; } *(pCh+i)='\0'; Result.p=pCh; return Result; } CShlString operator +(CShlString str,char *pCh) { char *pStr,*pp; int i=0; CShlString Result; pStr=str.p; for(int m=0;*(pStr+m);m++); for(int n=0;*(pCh+n);n++); pp=new char[m+n+1]; while(*pStr) { *(pp+i)=*pStr; i++; pStr++; } while(*pCh) { *(pp+i)=*pCh; i++; pCh++; } *(pp+i)='\0'; Result.p=pp; return Result; } CShlString CShlString::operator =(CShlString& str) { char *pStr,*pCh; int i=0; pStr=str.p; for(int m=0;*(pStr+m);m++); pCh=new char[m+1]; while(*pStr) { *(pCh+i)=*pStr; i++; pStr++; } *(pCh+i)='\0'; delete []p; p=pCh; //delete []pCh; return *this; } CShlString CShlString::operator+=(CShlString& str) { char *pStr,*pCh,*pp; int i=0; // CShlString Result; pStr=str.p; pp=p; for(int m=0;*(pp+m);m++); for(int n=0;*(pStr+n);n++); pCh=new char[m+n+1]; while(*pp) { *(pCh+i)=*pp; i++; pp++; } while(*pStr) { *(pCh+i)=*pStr; pStr++; i++; } *(pCh+i)='\0'; delete []p; p=pCh; return *this; } char CShlString::operator [](int n) { char *pp,Ch; pp=p; for(int i=0;*pp;i++,pp++); if(n>i) { cout<<"Error number n!"; return '\0'; } Ch=*(p+n-1); return Ch; }
|