#include <iostream.h>
#include <assert.h>
unsigned cstrLen(const char str[])
{//计算字符长度
unsigned i=0;
while(str[i]!='\0')i++;
return i;
}
//说明部分
class string{
public:
//构造函数
string();
string(char);
string(unsigned);
string(const char *);
string(const string &);
//析构函数
~string();
//允许子串类访问
friend class substring;
//赋值
void operator =(const string & right);
void operator +=(const string & right);
//取子串
string operator () (unsigned start,unsigned len);
//行输入
istream & getline(istream &);
//求字符串长度
unsigned length() const;
//访问单个字符
char & operator[](unsigned index) const;
//字符串比较
int compare(const string &)const;
//到普通C字符串的转换
operator const char *()const;
private:
//数据域:字符缓冲区,缓冲区长度
unsigned buflen;
char* buffer;
};
//实现部分
string::string(unsigned size)
{
assert(size>=0);
//设置数据域值
buflen=1+size;
buffer=new char[buflen];
assert(buffer!=0);
//对缓冲区做初始化
for(unsigned i=0;i<buflen;i++)
buffer[i]='\0';
}
string::string(const char * inittext)
{//初始化数据域
buflen=1+cstrLen(inittext);
buffer=new char[buflen];
assert(buffer!=0);
//字符串内容复制
unsigned i;
for(i=0;inittext[i]!='\0';i++)
buffer[i]=inittext[i];
buffer[i]='\0';
}
//复制构造函数
string::string(const string & initstr)
{
buflen=1+cstrLen(initstr.buffer);
buffer=new char[buflen];
assert(buffer!=0);
unsigned i;
for( i=0;initstr.buffer[i]!='\0';i++)
buffer[i]=initstr.buffer[i];
buffer[i]='\0';
}
string::string(char c)
{//建立一个别单字符的字符串
buflen=2;
buffer=new char[buflen];
assert(buffer!=0);
buffer[0]=c;
buffer[1]='\0';
}
string::string()
{//建立一个空字符串
buflen=1;
buffer=new char[buflen];
assert(buffer!=0);
buffer[0]='\0';
}
string::~string()
{//释放缓冲区
delete [] buffer;
//指针置空
buffer=0;
}
unsigned string::length() const
{//计算字符串中非空字符个数
return cstrLen(buffer);
}
void string::operator=(const string & right)
{//字符串赋值
const unsigned rightLength=right.length();
//如果缓冲区不够大,申请更大的
if(rightLength>=buflen)
{//先释放缓冲区
delete [] buffer;
//申请新的缓冲区
buflen=1+rightLength;
buffer=new char[buflen];
assert(buffer!=0);
}
//实际复制
unsigned i;
for(i=0;buffer[i]!='\0';i++)
{buffer[i]=right.buffer[i];}
buffer[i]='\0';
}
int isLowerCase(char c)
{//判断字符是否小写
return(c>='a')&&(c<='z');
}
void toUpper(string & word)
{//小写转大写
for(unsigned i=0;word[i]!='\0';i++)
if(isLowerCase(word[i]))
word[i]=(word[i]-'a')+'A';
}
//元音计数
void vowelcount(const string & str)
{
long count=0;
unsigned len=str.length();
char c;
for(unsigned i=0;str[i]!='\0';i++){
if((c=str[i]=='a')||(c=='i')||(c=='e')||(c=='o')||(c=='u'))
count++;}
cout<<"the string has"<<count<<"vowels"<<endl;
cout<<"Ratio of vowels in the string is"<<(count*100.0)/len<<endl;
}
int string::compare(const string & val)const
{//比较字符串缓冲区里的字符序列
char *p=buffer;
char *q=val.buffer;
for(;(*p!='\0')&&(*p==*q);p++,q++)
;//空循环体,字符相同就继续比较
return *p-*q;//左字符串:小、大、相同;返回:正、负、零
}
//串连接
void string::operator+=(const string & v)
{
unsigned i;
//求出并置(即串连接)后字符的总长
unsigned conLen=length()+v.length();
if(conLen>=buflen)
{//原缓冲区不够大
char*newbuf=new char [1+conLen];
assert(newbuf!=0);
//复制原有字符
for(i=0;buffer[i]!='\0';i++)
{newbuf[i]=buffer[i];}
//删除原缓冲区,重置缓冲区指针和长度
delete [] buffer;
buflen=1+conLen;
buffer=newbuf;
}
else
i=cstrLen(buffer);
//连接v的内容
for(unsigned j=0;v.buffer[i]!='\0';i++,j++)
{buffer[i]=v.buffer[j];}
buffer[i]='\0';
}
string operator+ (const string & left,const string & right)
{//求出由并置两个字符串而得到的字符串
string result(left);
result+=right;
return result;
}
string string::operator ()(unsigned start,unsigned len)
{//取子串
unsigned blen=length();
if(start>=blen)
return "";
int maxlen=blen-start;
if(len>maxlen)len=maxlen;
string newstr(len);
for(unsigned i=0;i<len;i++)
newstr[i]=buffer[start+i];
return newstr;
}
//主要是局部变量i 的作用域 和重载函数的返回类型,还有就是#include <assert.h>.没有运行,只改了语法错误.