串
2016-06-13 00:00
573 查看
MyString.h
MyString.cpp
Test.cpp
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_p 8000 str[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; }
相关文章推荐
- HDU2888二维RMQ
- ARP(地址解析协议)
- 类的sizeof
- HDU2054 A == B ? 模拟题
- 算法导论 10.4-2 O(n)时间 递归遍历二叉树
- P05: 二维费用的背包问题
- 算法导论-9.3-3-快速排序-最坏时间O(nlgn)
- 算法导论-24.1-Bellman-Ford算法
- 带权的二分匹配
- 浙江大华2012届校园招聘研发类试卷B
- Linux2.6设备管理
- 深入理解javascript的作用域--函数声明为什么会前置
- Linux0.12-内存寻址
- 算法导论-15-1-双调欧几里得旅行商问题
- NULL是什么
- Linux0.12中断处理
- 排序
- 求逆序数
- HDU3524 Perfect Squares 数论
- 腾讯2011.10.15校园招聘会笔试题