#include <stdlib.h>
#include <string.h>
//我用了3天的时间来构想和编写这个类,大家使用一下,看看有什么
要改进的地方!并多多指点.
//最初编写这个类只是为了方便我编写字符串的程序,后来觉得发上
来给大家一起探讨可能会对我的
//学习有比较的的帮助 我也想结交编程方面的好友,我的
QQ:17160764
class TString
{
public:
TString():len(0),size(0),sp(0),error(0),pNeedFree(0){}
TString(const char *Newchar);
~TString();
public:
void BigWrite(); //全变成大写
void LittleWrite(); //全变成小写
const char *GetString()const{return sp;}//得到字符串的指针
const int GetLen()const{return len;} //得到字符串的长度
public:
//下面的用于字符串赋值
TString & operator +=(const TString ©);
TString & operator +=(const char * Newchar);
TString & operator =(const TString ©);
TString & operator =(const char * Newchar);
//下面的都是用于字符串比较
int operator ==(const char* cmp);
int operator ==(const TString &cmp);
int operator !=(const char* cmp);
int operator !=(const TString &cmp);
int operator <(const char* cmp);
int operator <(const TString &cmp);
int operator >(const char* cmp);
int operator >(const TString &cmp);
int operator <=(const char* cmp);
int operator <=(const TString &cmp);
int operator >=(const char* cmp);
int operator >=(const TString &cmp);
//下面的用于连接字符串
char* operator +(const char * cs);
char* operator +(const TString & s1);
char operator [](const int i);//这个用于提取某个字符
public:
int StrToInt(const char* interger);
TString & ChangeLen(int NewLen);
protected:
int compare(const char* Cmpchar);
int compare(const TString &Cmpchar)const;
char * sp; //存储字符串的
int len;
int size;
int error;
char * pNeedFree;//用于释放动态字符串
};
/////////////////////////////////////////////////////
TString::TString(const char *Newchar)
{
pNeedFree=0;
if(Newchar==NULL)
{
len=0;
size=0;
sp=NULL;
error=0;
}
else
{
len=strlen(Newchar);
size=len+1;
sp=new char[size];
if(sp!=NULL)
strcpy(sp,Newchar);
else
error=(1);//out_of_memory);
}
}
TString & TString::operator =(const TString ©)
{
if(this!=©) //不能自赋值给自己
{
delete sp;
if(copy.sp==NULL)
{
len=0;
size=0;
sp=0;
error=0;
}
else
{
len=copy.len;
size=len+1;
sp=new char[size];
if(sp!=NULL)
strcpy(sp,copy.sp);
else
error=1;
}
}
delete [] pNeedFree;
return *this;
}
TString & TString::operator =(const char * Newchar)
{
if(Newchar==NULL)
{
len=0;
size=0;
sp=NULL;
error=0;
}
else
{
len=strlen(Newchar);
size=len+1;
sp=new char[size];
if(sp!=NULL)
strcpy(sp,Newchar);
else
error=1;
}
delete [] pNeedFree;
return *this;
}
TString::~TString()
{
delete sp;
sp=0;
}
char* TString::operator +(const char *cs)
{
char *Newchar=new char[this->len+strlen(cs)+1];
if(Newchar!=NULL)strcpy(Newchar,this->sp);
if(cs !=NULL)strcat(Newchar,cs);
pNeedFree=Newchar;
return Newchar;
}
char* TString:: operator +(const TString & s1)
{
char * Newchar=new char[s1.len+this->len+1];
if(Newchar!=NULL)strcpy(Newchar,this->sp);
if(s1.sp !=NULL)strcat(Newchar,s1.sp);
pNeedFree=Newchar;
return Newchar;
}
int TString::StrToInt(const char* interger)
{
int i=0;
i=atoi(interger);
return i;
}
TString &TString:: operator +=(const char * Newchar)
{
*this=ChangeLen(len+strlen(Newchar));
strcat(sp,Newchar);
return *this;
}
TString &TString:: operator +=(const TString &s1)
{
*this=ChangeLen(len+s1.len);
strcat(sp,s1.sp);
return *this;
}
TString &TString:: ChangeLen(int NewLen)
{
char *temp=new char[size];
strcpy(temp,sp);
len=NewLen;
size=len+1;
if(sp!=NULL)delete sp;
sp=new char[size];
strcpy(sp,temp);
delete temp;
return *this;
}
char TString:: operator [](const int i)
{
if(i<0)
{
error=2;
return sp[0];
}
if(i>len)
{
error=2;
return sp[len];
}
else
return sp[i];
}
void TString::BigWrite()
{
strupr(sp);
}
void TString::LittleWrite()
{
strlwr(sp);
}
//////////////////////////////////////////////////
int TString::compare(const char* Cmpchar)
{
return strcmp(sp,Cmpchar);
}
int TString::compare(const TString &Cmpchar)const
{
return strcmp(sp,Cmpchar.sp);
}
int TString::operator ==(const char* cmp)
{
return compare(cmp)==0;
}
int TString::operator ==(const TString& cmp)
{
return compare(cmp)==0;
}
int TString::operator !=(const char* cmp)
{
return compare(cmp)!=0;
}
int TString::operator !=(const TString &cmp)
{
return compare(cmp)!=0;
}
int TString::operator <(const char* cmp)
{
return compare(cmp)<0;
}
int TString::operator <(const TString &cmp)
{
return compare(cmp)<0;
}
int TString::operator >(const char* cmp)
{
return compare(cmp)>0;
}
int TString::operator >(const TString &cmp)
{
return compare(cmp)>0;
}
int TString::operator <=(const char* cmp)
{
return compare(cmp)<=0;
}
int TString::operator <=(const TString &cmp)
{
return compare(cmp)<=0;
}
int TString::operator >=(const char* cmp)
{
return compare(cmp)>=0;
}
int TString::operator >=(const TString &cmp)
{
return compare(cmp)>=0;
}
//这个类还差一个错误处理,有时间我会添加上去
饿的的
[size=3][/size][font=宋体][/font]