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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值