exit
(1);
}
StringString:
:
operator+(constString&s)const//类串+类串
{
Stringw;
intlen=size+s.size;
delete[]w.str;
w.str=newchar[len+1];
if(w.str==NULL)
Error("operator+:
overflow!
");
strcpy(w.str,str);
strcat(w.str,s.str);
w.size=len;
return(w);
}
StringString:
:
operator+(constchar*c)const//类串+C串
{
Stringw;
intlen=size+strlen(c);
delete[]w.str;
w.str=newchar[len+1];
if(w.str==NULL)
Error("operator+:
overflow!
");
strcpy(w.str,str);
strcat(w.str,c);
w.size=len;
return(w);//相当于String_temp=w;调用复制构造函数
}
Stringoperator+(constchar*c,constString&s1)//C串+类串
{
Stringw;//不能简化为return(s1+c),因为连接后的串顺序不对
delete[]w.str;
w.size=strlen(c)+s1.size;
w.str=newchar[w.size+1];
if(w.str==NULL)//友元不能直接调用私有函数Error
s1.Error("operator+:
overflow!
");
strcpy(w.str,c);
strcat(w.str,s1.str);
return(w);
}
String&String:
:
operator=(constString&s)//复制赋值:
类串=类串
{
if(size!
=s.size)
{
delete[]str;
str=newchar[s.size+1];
if(str==NULL)
Error("operator=:
overflow!
");
size=s.size;
}
strcpy(str,s.str);
return(*this);
}
String&String:
:
operator=(constchar*c)//转换赋值:
类串=C串
{
intlen=strlen(c);
if(size!
=len)
{
delete[]str;
str=newchar[len+1];
if(str==NULL)
Error("operator=:
overflow!
");
size=len;
}
strcpy(str,c);
return(*this);
}
StringString:
:
SubStr(intid,intnum)const//取子串
{
Strings;
intlen=size;
intleft=len-id,i;
if(id<0||id>len-1)
Error("idillegal!
");
if(num<=0||num>left)
Error("numillegal!
");
delete[]s.str;
s.str=newchar[num+1];
if(s.str==NULL)
Error("overflow!
");
s.size=num;
char*p=str+id;
char*p1=s.str;
for(i=1;i<=num;i++)
*p1++=*p++;
*p1='\0';
return(s);
}
String&String:
:
Insert(intid,constString&s)//子串插入
{
char*p,*p1,*buf;
intlen=size;//记录本串长度
intlen1=s.size;//记录子串长度
intleft=len-id;//计算后段长度
inti;
if(id<0||id>len)//检验插入位置的合法性
Error("idillegal!
");
buf=str;//保留本串的字符串
str=newchar[len+len1+1];//重新分配本串的字符串空间
if(str==NULL)
Error("overflow!
");
strcpy(str,buf);//取回保留的字符串
delete[]buf;//释放保留字符串的空间
p=str+len;//指向本串结束符
p1=p+len1;//指向本串结束符移动的终点
for(i=1;i<=left+1;i++)//移动字符是后段字符和串结束符
*p1--=*p--;
p=str+id;//指向插入的起始位置
p1=s.str;//指向待插子串的起始位置
while(*p1!
='\0')//逐个字符插入
*p++=*p1++;
size=len+len1;//修改本串的长度
return(*this);//返回插入后的本串
}
String&String:
:
Erase(intid,intnum)//子串删除
{
char*p,*q,*buf;
intlen=size;
intleft=len-id;
if(id<0||id>len-1)
Error("idillegal!
");
if(num<=0||num>left)
Error("numillegal!
");
p=str+id;
q=str+id+num;
while(*q!
='\0')
*p++=*q++;
*p='\0';
buf=str;
len=strlen(buf);
str=newchar[len+1];
if(str==NULL)
Error("overflow!
");
strcpy(str,buf);
size=len;
delete[]buf;
return(*this);
}
boolString:
:
operator==(constString&s)const//类串==类串
{
return(strcmp(str,s.str)==0);
}
boolString:
:
operator==(constchar*c)const//类串==C串
{
return(strcmp(str,c)==0);
}
booloperator==(constchar*c,constString&s)//C串==类串
{
return(strcmp(c,s.str)==0);
}
String:
:
operatorchar*(void)const
{
char*c=newchar[size+1];
if(c==NULL)
Error("overflow");
strcpy(c,str);
return(c);
}
char&String:
:
operator[](intid)
{
if(id<0||id>size-1)
Error("operator[]:
Idillegal!
");
return(*(str+id));//return(str[id]);
}
constchar&String:
:
operator[](intid)const
{
if(id<0||id>size-1)
Error("operator[]:
Idillegal!
");
return(*(str+id));//return(str[id]);
}
intString:
:
Find_First_Of(charch,intid)const//从id开始查找ch首次出现的位置
{
inti=id;
char*p;
if(id<0||id>size-1)//检验查找起始位置合法性
Error("Startillegal!
");
p=str+id;
while(*p!
='\0')
if(*p==ch)
break;
else
{
i++;p++;
}
return(*p=='\0'?
-1:
i);
}
intString:
:
Find_First_Of(constString&s,intid)const
{
intlen=s.Size(),end=Size()-1;
charfirst=s[0],last=s[len-1];
intfirstid,lastid;
Stringmid,cs;
if(len>2)
mid=s.SubStr(1,len-2);
firstid=Find_First_Of(first,id);
lastid=firstid+len-1;
while(firstid!
=-1&&lastid<=end)
{
if(str[lastid]==last)
{
if(len<=2)
return(firstid);
cs=SubStr(firstid+1,len-2);
if(cs==mid)
return(firstid);
}
id=firstid+1;
firstid=Find_First_Of(first,id);
lastid=firstid+len-1;
}
return(-1);
}
ostream&operator<<(ostream&ostr,constString&s)
{
ostr<return(ostr);
}
istream&operator>>(istream&istr,String&s)
{
charbuf[256];//256是输入缓冲区长度
istr>>buf;
s=buf;//调用转换赋值函数
return(istr);
}
intString:
:
ReadString(istream&istr,chardelimiter)
{
charbuf[256];
inttoken=-1;
if(istr.getline(buf,256,delimiter))
{
*this=buf;//调用转换赋值函数
token=size;
}
return(token);
}