#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include"stringclass.h"
String::String(const char *c)//缺省构造函数的实现
{
size=strlen(c);
str=new char[size+1];
if(str==NULL)
Error("String:overflow!");
strcpy(str,c);
}
String::String(const String& cs)//拷贝构造函数的实现
{
size=cs.size;
str=new char[size+1];
if(str==NULL)
Error("String:overflow!");
strcpy(str,cs.str);
}
//类串的输入输出
istream& operator>>(istream& istr,String& cs)
{
char tmp[256];
istr>>tmp;
cs=tmp;
return(istr);
}
ostream& operator<<(ostream& ostr,const String& cs)
{
ostr<<cs.str;
return(ostr);
}
int String::ReadString(istream& istr,char delimiter)
{
char tmp[256];
if(istr.getline(tmp,256,delimiter))
{
*this=tmp;
return(size);
}
else
return(-1);
}
//赋值运算符
String& String::operator=(const String& cs)//类串=类串
{
if(size!=cs.size)
{
delete[]str;
size=cs.size;
str=new char[size+1];
if(str==NULL)
Error("operator=:overflow!");
}
strcpy(str,cs.str);
return(*this);
}
String& String::operator=(const char *c) //类串=C串
{
int n=strlen(c);
if(size!=n)
{
delete[]str;
size=n;
str=new char[size+1];
if(str==NULL)
Error("operator=:overflow!");
}
strcpy(str,c);
return(*this);
}
//成员转换函数
String::operator char*(void)const
{
return(str);
}
//串连接运算符
String String::operator+(const String& cs)const
{
String w;
delete[]w.str;
w.size=size+cs.size;
w.str=new char[w.size+1];
if(w.str==NULL)
Error("operator+:overflow!");
strcpy(w.str,str);
strcat(w.str,cs.str);
return(w);
}
String String::operator+(const char *c)const//String+C++String
{
String w;
delete[]w.str;
w.size=size+strlen(c);
w.str=new char[w.size+1];
if(w.str==NULL)
Error("operator+:overflow!");
strcpy(w.str,str);
strcat(w.str,c);
return(w);
}
String operator+(const char *c,const String& cs)//C++String+String
{
String w;//不能简化为return(cs+c),因为连接后的串顺序不对
delete[]w.str;
w.size=strlen(c)+cs.size;
w.str=new char[w.size+1];
if(w.str==NULL) //友元不能调用私有函数Error
{
cerr<<"operator+:overflow!";
exit(1);
}
strcpy(w.str,c);
strcat(w.str,cs.str);
return(w);
}
String& String::operator+=(const String& cs)//String+String
{
char *tmp=new char[size+1];
if(tmp==NULL)
Error("operator+=:overflow!");
strcpy(tmp,str);
delete[]str;
size=size+cs.size;
str=new char[size+1];
if(str==NULL)
Error("operator+=:overflow!");
strcpy(str,tmp);
strcat(str,cs.str);
delete tmp;
return(*this);
}
String& String::operator+=(const char *c)
{
char *tmp=new char[size+1];
if(tmp==NULL)
Error("operator+=:overflow!");
strcpy(tmp,str);
delete[]str;
size=size+strlen(c);
str=new char[size+1];
if(str==NULL)
Error("operator+=:overflow!");
strcpy(str,tmp);
strcat(str,c);
delete tmp;
return(*this);
}
String String::SubString(int id,int count)const
{
String cs;
if(id<0)
Error("SubString:id illegal!");
if(id>size-1||count<=0)
count=0;
int left=size-id;
if(left>0&&count>left)
count=left;
delete[]cs.str;
cs.size=count;
cs.str=new char[cs.size+1];
if(cs.str==NULL)
Error("SubString:overflow!");
char* p=cs.str;
char* q=str+id;
for(int i=1;i<=count;i++)
*p++=*q++;
*p='\0';
return(cs);
}
String& String::Insert(const char *c,int id)
{
if(id<0||id>size)
Error("Insert:id illegal!");
char* temp=new char[size+1];
if(temp==NULL)
Error("Insert:overflow!");
strcpy(temp,str);
delete[]str;
int n=size+strlen(c); //新串长
str=new char[n+1];
if(str==NULL)
Error("Insert:overflow!");
strcpy(str,temp);
delete[]temp;
char* p=str+size;
char* q=str+n;
int left=size-id+1; //包括串结束符
for(int i=1;i<=left;i++)
*q--=*p--;
p=str+id;
i=0;
while(*(c+i)!='\0')//c是const指针
*p++=*(c+i++);
size=n;
return(*this);
}
String& String::Insert(const String& cs,int id)
{
if(id<0||id>size)
Error("Insert:id illegal!");
char* temp=new char[size+1];
if(temp==NULL)
Error("Insert:overflow!");
strcpy(temp,str);
delete[]str;
int n=size+cs.size;
str=new char[n+1];
if(str==NULL)
Error("Insert:overflow!");
strcpy(str,temp);
delete[]temp;
char* p=str+size;
char* q=str+n;
int left=size-id+1; //需要移动的字符数,包括串结束符
for(int i=1;i<=left;i++)
*q--=*p--;
p=str+id;
q=cs.str;
while(*q!='\0')
*p++=*q++;
size=n;
return (*this);
}
void String::Erase(int id,int count)
{
if(id>size-1||count<=0)
return;
if(id<0)
Error("Delete:id illegal!");
int left=size-id;
if(count>left)
count=left;
char* p=str+id;
char* q=str+id+count;
while(*q!='\0')
*p++=*q++;
*p='\0';
size=strlen(str);
char* tmp=new char[size+1];
if(tmp==NULL)
Error("Delete :overflow!");
strcpy(tmp,str);
delete[]str;
str=new char[size+1];
if(str==NULL)
Error("Delete :overflow!");
strcpy(str,tmp);
delete[]tmp;
}
char& String::operator[](int id)
{
if(id<0||id>size-1)
Error("operator[]:Id illegal!");
return(*(str+id)); //return(str[id]);
}
const char& String::operator[](int id)const
{
if(id<0||id>size-1)
Error("operator[]:Id illegal!");
return(*(str+id)); //return(str[id]);
}
int String::Find(char ch,int start)const
{
int i; char *p;
if(start>size-1)
return(-1);
if(start<0)
start=0;
for(p=str+start,i=start;*p!='\0';++p,++i)
if(*p==ch)
return(i);
return(-1);
}
int String::FindLast(char ch)const
{
int i;
char *p;
for(p=str+size-1,i=size-1;p!=str;--p,--i)
if(*p==ch)
return(i);
return(-1);
}