来源:互联网 发布:编程字体 monaco 编辑:程序博客网 时间:2024/04/30 10:40

MyString.h

const int MAXSIZE = 100;class CMyString{private:int m_ncurlen;char *m_pstr;int *m_pnext;public:CMyString(const CMyString& copy);CMyString(const char *init);CMyString();~CMyString(){delete[] m_pstr;}int Length() const{return m_ncurlen;}int Find(CMyString part) const;char* GetBuffer() const;public:CMyString& operator()(int pos, int len);bool operator==(const CMyString cmp_str)const;bool operator!=(const CMyString cmp_str)const;bool operator<(const CMyString cmp_str)const;bool operator>(const CMyString cmp_str)const;bool operator!()const{return m_ncurlen==0;}CMyString& operator=(const CMyString ©);CMyString& operator+=(const CMyString ©);char& operator[](int i);friend ostream& operator<<(ostream&,CMyString&);friend istream& operator>>(istream&,CMyString&);private:void Next();};

MyString.cpp

#include <iostream>#include <cstring>using namespace std;#include"MyString.h"CMyString::CMyString(){m_pstr = new char[MAXSIZE+1];if(!m_pstr){cerr<<"Allocation Error"<<endl;exit(1);}this->m_ncurlen = 0;m_pstr[0] = '\0';}CMyString::CMyString(const char *init){m_pstr = new char[MAXSIZE+1];if(!m_pstr){cerr<<"Allocation Error"<<endl;exit(1);}this->m_ncurlen = strlen(init);strcpy(m_pstr,init);}CMyString::CMyString(const CMyString ©){m_pstr = new char[MAXSIZE+1];if(!m_pstr){cerr<<"Allocation Error"<<endl;exit(1);}this->m_ncurlen = strlen(copy.m_pstr);strcpy(m_pstr,copy.m_pstr);}int CMyString::Find(CMyString part) const{int ls = this->m_ncurlen;int lp = part.m_ncurlen;part.Next();int i,j;for(i=j=0; i<ls&&j<lp; i++){if(this->m_pstr[i] == part.m_pstr[j])j++;else if(j){j = part.m_pnext[j-1]+1;i--;}}return j==lp?(i-lp):-1;}void CMyString::Next(){this->m_pnext=new int[m_ncurlen];memset(m_pnext, 0 , sizeof(m_pnext));int i = 0, j;m_pnext[0] = -1;i = 0;for(j = 1; j < m_ncurlen; j++){for(i = m_pnext[j-1]; i>=0&&(m_pstr[i+1]!=m_pstr[j]); i = m_pnext[i]);m_pnext[j] = (m_pstr[i+1]==m_pstr[j]) ? i+1 : -1;}}char *CMyString::GetBuffer() const{return m_pstr;}CMyString& CMyString::operator()(int pos, int len){CMyString *temp = new CMyString;if(pos < 0 || len < 0 || pos >m_ncurlen){temp->m_ncurlen = 0;temp->m_pstr[0] = '\0';}else{if(pos + len > m_ncurlen)len = m_ncurlen - pos;temp->m_ncurlen = len;int i,j;for(i=0,j=pos; i < len; i++,j++){temp->m_pstr[i] = this->m_pstr[j];}temp->m_pstr[len] = '\0';}return *temp;}bool CMyString::operator==(const CMyString cmp_str) const{if(this->m_ncurlen != cmp_str.m_ncurlen)return 0;int i;for(i = 0; i < this->m_ncurlen; i++){if(this->m_pstr[i] != cmp_str.m_pstr[i])return 0;}return 1;}bool CMyString::operator<(const CMyString cmp_str) const{if(this->m_ncurlen != cmp_str.m_ncurlen)return (this->m_ncurlen < cmp_str.m_ncurlen);int i = 0;for(i = 0; i < this->m_ncurlen; i++){if(this->m_pstr[i] != cmp_str.m_pstr[i])return (this->m_pstr[i] < cmp_str.m_pstr[i]);}return 0;}bool CMyString::operator>(const CMyString cmp_str) const{if(this->m_ncurlen != cmp_str.m_ncurlen)return (this->m_ncurlen > cmp_str.m_ncurlen);int i = 0;for(i = 0; i > this->m_ncurlen; i++){if(this->m_pstr[i] != cmp_str.m_pstr[i])return (this->m_pstr[i] > cmp_str.m_pstr[i]);}return 0;}CMyString& CMyString::operator=(const CMyString ©){delete[] m_pstr;m_pstr = new char[copy.m_ncurlen+1];strcpy(m_pstr, copy.m_pstr);return *this;}CMyString& CMyString::operator+=(const CMyString &add){int length = this->m_ncurlen + add.m_ncurlen ;CMyString temp(*this);delete[] this->m_pstr;this->m_pstr = new char[length+1];int i,j;for(i = 0; i < temp.m_ncurlen; i++){this->m_pstr[i] = temp.m_pstr[i];}for(i,j=0; j < add.m_ncurlen; j++,i++){this->m_pstr[i] = add.m_pstr[j];}this->m_pstr[i] = '\0';return *this;}char& CMyString::operator[](int i){if(i < 0 || i >= this->m_ncurlen){cout<<"out of boundary!"<<endl;exit(1);}return this->m_pstr[i];}ostream& operator<<(ostream& os,CMyString& str){os<<str.m_pstr;return os;}istream& operator>>(istream& is,CMyString& str){is>>str.m_pstr;return is;}

Test.cpp

#include <iostream>using namespace std;#include "MyString.h"int main(){CMyString test1("babc");CMyString test2("abababcdefb");cout<<test2.Find(test1)<<endl;cout<<test2(2,3)<<endl;if(test1<test2){cout<<test1<<"<"<<test2<<endl;}else{if(test1==test2){cout<<test1<<"=="<<test2<<endl;}else{if(test1>test2){cout<<test1<<">"<<test2<<endl;}}}int length=test2.Length();for(int i=0;i<length;i++){cout<<test2[i];}cout<<endl;test1+=test2;cout<<test1<<endl;test1=test2;cout<<test1<<endl;return 0;}