C++随手代码收藏 自定义实现 String 类

#include <iostream.h>
#include <string.h>
#include <stdlib.h>
class String         //定义字符串类
{ 
     char *ss;         //字符串的首地址
     int length;         //字符串长度
public:
     String(char *s);     //带参数的构造函数
     String(){ss=0;length=0;}     //缺省的构造函数
     String(String &s);             //拷贝构造函数
     String& operator =(String&);//重载赋值运算符“=”(不能用友元函数重载)
     String& operator =(char *);     //重载赋值运算符“=”(不能用友元函数重载)
     friend String operator +(String &s1,String &s2);//可改用成员函数
     friend String operator +(String &s1,char* s2);//可改用成员函数
     friend String operator +(char* s1,String &s2);
     String& operator +=(String&);
     String& operator +=(char *);
     friend int operator ==(String &s1,String &s2);//可改用成员函数
     friend int operator ==(String &s1,char* s2);//可改用成员函数
     friend int operator ==(char* s1,String &s2);
     friend int operator >(String &s1,String &s2);//可改用成员函数int operator >(String &s2);
     int operator >(char* s2);
     friend int operator >(char* s1,String &s2);
     char& operator [](int);     //重载下标运算符“[]”
operator char*()         //类型转换函数
     { return ss; }
     //有了类型转换函数,某些字符串函数,如:strlen(String),strcpy(char*,String),
     //strcmp(String,String)、strcmp(String,char*)、strcmp(char*,String),
     //strcat(char*,String)等等不再需要重载,但strcat(String&,char*)除外
     friend void strcat(String& s1,char* s2);//相当于s1+="..."
     //有了上述重载函数,strcat(String& s1,String& s2)也可执行:由类型转换函数自动将s2转
     //换成char*型。strcat(char *s1,...)不需要重载,只要s1有足够的存储空间就行。
     friend ostream& operator <<(ostream&,String&);
     friend istream& operator >>(istream&,String&);
     String Substr(int index,int count);//从index开始提取count个字符的子串
     int Find(char c,int start);//从下标start开始找到c后,返回字符c在本串的下标
     int IsEmpty();//判本串是否为空串
     void clear();//清本串为空串
     void Print();             //字符串的输出函数
     ~String()                     //析构函数
     {if (ss) delete []ss;}
};

String::String(char *s)     //带参数的构造函数
{
     int len=strlen(s);
     if (len>0)
     {
         ss=new char[len+1];
         strcpy(ss,s);
     }
     else ss=0;
     length=len;
}
String::String(String& s)//定义拷贝构造函数
{
     if (ss) delete[]ss;
     if (s.length>0)
     {
         ss=new char[s.length+1];
         strcpy(ss,s.ss);
     }
     else ss=0;
     length=s.length;
}

String& String::operator =(String& s)
{//重载“=”运算符
     if (ss) delete[] ss;
     if (s.length>0)
     {
         ss=new char[s.length+1];
         strcpy(ss,s.ss);
     }
     else ss=0;
     length=s.length;
     return *this;
}

void String::Print()                 //字符串的输出函数
{
     //cout<<"length="<<length<<",string=";
     if (length>0)
         cout<<ss<<endl;
     else
         cout<<"<null>"<<endl;
}

String& String::operator =(char *s)
{//重载“=”运算符
     if (ss) delete[] ss;
     int len=strlen(s);
     if (len>0)
     {
         ss=new char[len+1];
         strcpy(ss,s);
     }
     else ss=0;
     length=len;
     return *this;
}
String operator +(String &s1,String &s2)
{
     char *t;
     int l1,l2;
     l1=s1.length;
     l2=s2.length;
     if (l1+l2>0)
     {
         t=new char[l1+l2+1];
         if (l1>0)
         {
             strcpy(t,s1.ss);
             if (l2>0)   strcat(t,s2.ss);
         }
         else   //l1=0时,l2>0
             strcpy(t,s2.ss);
     }
     else t="\0";
     return String(t);
}
String& String::operator +=(String& s)
{
     String temp;
     temp=*this+s;
     *this=temp;
     return *this;
}
String& String::operator +=(char *s)
{
     String temp;
     temp=*this+s;
     *this=temp;
     return *this;
}
String operator +(String &s1,char* s2)
{
     char* t;
     int l1,l2;
     l1=s1.length;
     l2=strlen(s2);
     if (l1+l2>0)
     {
         t=new char[l1+l2+1];
         if (l1>0)
         {
             strcpy(t,s1.ss);
             if (l2>0) strcat(t,s2);
         }
   else strcpy(t,s2);
     }
     else t="\0";
     return String(t);
}
String operator +(char* s1,String &s2)
{
    char* t;
    int l1,l2;
    l1=strlen(s1);
    l2=s2.length;
    if (l1+l2>0)
    {
       t=new char[l1+l2+1];
       if (l1>0)
       {
          strcpy(t,s1);
          if (l2>0) strcat(t,s2.ss);
       }
       else strcpy(t,s2.ss);
    }
    else t="\0";
    return String(t);
}
int operator ==(String &s1,String &s2)
{
    if (strcmp(s1.ss,s2.ss)==0)
       return true;
    else
       return false;
}
int operator ==(String &s1,char* s2)
{
    if (strcmp(s1.ss,s2)==0)
       return true;
    else
       return false;
}
int operator ==(char* s1,String &s2)
{
    if (strcmp(s1,s2.ss)==0)
       return true;
    else
       return false;
}

int operator >(String &s1,String &s2)
{
    if (strcmp(s1.ss,s2.ss)>0)
       return true;
    else
       return false;
}
int String::operator >(char* s2)
{
     if (strcmp(this->ss,s2)>0)
         return true;
     else
         return false;
}
int operator >(char* s1,String &s2)
{
     if (strcmp(s1,s2.ss)>0)
         return true;
     else
         return false;
}
char& String::operator [](int n)
{
     if (n<0 || n>=length)
     {
         cout<<"下标超界!\n";
         exit(0);
     }
     return this->ss[n];
}
ostream& operator <<(ostream &os,String& s)
{   //友元函数,不能用成员函数
    if (s.ss)
       os<<s.ss;
    else
       os<<"<null>";
 return os;
}
istream& operator >>(istream &is,String &s)
{   //友元函数,不能用成员函数
    if (s.ss) delete[] s.ss;
    char *str=new char[2048];
    is>>str
    int len=strlen(str);
    if (len>0)
    {
       s.ss=new char[len+1];
       strcpy(s.ss,str);
       s.length=len;
    }
    else { s.ss=0;s.length=0; }
    delete[] str;
    return is;
}
void strcat(String& s1,char *s2)
{//友元函数,重载strcat()函数,第一个参数为char*的不需重载
    int l1,l2;
    l1=s1.length;
    l2=strlen(s2);
    if (l1+l2>0)
    {
       if (l1>0)
       {
          char* t;
          t=new char[l1+1];
          strcpy(t,s1.ss);
          delete []s1.ss;
          s1.ss=new char[l1+l2+1];
          strcpy(s1.ss,t);
          if (l2>0) strcat(s1.ss,s2);
       }
       else     //l1=0,l2>0的情况
          strcpy(s1.ss,s2);
       s1.length=l1+l2;
    }
    //l1+l2=0时,s1仍为空串,故不需处理
}

void main( )
{
     String s1="Nanjing",s2="Shanghai",s3,a("aa"),b("bb");
     char str[100]="Computer department,";
     cout<<"String s1="<<s1<<endl;//输出字符串s1(重载<<运算符)
     cout<<"String s2="<<s2<<endl;//输出字符串s2(重载<<运算符)
     cout<<"String s3="<<s3<<endl;
     cout<<"String   a="<<a<<endl;
     cout<<"String   b="<<b<<endl;
     s3=s1;                     //调用operator =(String&),重载“=”运算符
     cout<<"执行s3=s1;后,s3="<<s3<<endl;
     s3="Beijing";             //调用operator =(char*)
     cout<<"执行s3=\"Beijing\";后,s3="<<s3<<endl;
 s3+=s2;                     //operator +=(String&),重载“+=”运算符
     cout<<"执行s3+=s2;后,s3="<<s3<<endl;
     s3=s1+s2;                 //String+String,重载“+”
     cout<<"执行s3=s1+s2;后,s3="<<s3<<endl;
     s3=s1+"Tianjin";         //String+char*,重载“+”
     cout<<"执行s3=s1+\"Tianjin\"后,s3="<<s3<<endl;
     s3="Guangzhou"+s2;         //char* +String,重载“+”
     cout<<"执行s3=\"Guangzhou\"+s2;后,s3="<<s3<<endl;
     s3=a+b;                     //空String+空String
     cout<<"执行s3=a+b;后,s3="<<s3<<endl;
     s3+="Public";
     cout<<"接着执行s3+=\"Public\";后,s3="<<s3<<endl;
     cout<<"测试String==String功能:";
     if (s1==s3)             //operator ==(String,String),重载“==”
         cout<<s1<<'='<<s3<<endl;
     else
         cout<<s1<<"!="<<s3<<endl;
     cout<<"测试String==char*功能:";
     if (s3=="Public")     //operator ==(String,char*),重载“==”
         cout<<s3<<'='<<"Public"<<endl;
     else
         cout<<s3<<"!="<<"Public"<<endl;
     cout<<"测试char*==String功能:";
     if ("Public1"==s3)     //operator ==(char*,String),重载“==”
         cout<<s3<<'='<<"Public1"<<endl;
     else
         cout<<s3<<"!="<<"Public1"<<endl;
     char *sa="WuHan";
     cout<<"测试String>char*功能:";
     if (s2>sa)             //operator >(String,String),重载“>”
         cout<<s2<<'>'<<sa<<endl;
     else
         cout<<s2<<"<="<<sa<<endl;
 cout<<"测试String>String功能:";
     if (s2>s3)             //operator >(String,String),重载“>”
         cout<<s2<<'>'<<s3<<endl;
     else
         cout<<s2<<"<="<<s3<<endl;
     cout<<"测试char*>String功能:";
     if (sa>s3)             //operator >(String,String),重载“>”
         cout<<sa<<'>'<<s3<<endl;
     else
         cout<<sa<<"<="<<s3<<endl;
     cout<<"strlen(s1)="<<strlen(s1)<<endl;       //有了类型转换函数,字符串处
     cout<<"strcmp(s1,s2)="<<strcmp(s1,s2)<<endl;//理函数不用重载就可直接使用
     strcat(s1," University");                     //调用重载函数strcat(String,char*)
     cout<<"执行strcat(s1,\" University\");后,s1="<<s1<<endl;
     strcat(s2,s3);     //调用重载函数strcat(String,char*):自动将s3转换成char*类型
     cout<<"执行strcat(s2,s3);后,s2="<<s2<<endl;
     s2.Print();
     cout<<"str="<<str<<endl;
     strcat(str,s1);     //调用库函数strcat(char*,char*):自动将s1转换成char*类型
     cout<<"执行strcat(str,s1);后,str="<<str<<endl;
     cout<<"测试提取运算符“>>”,请输入一串字符:";cin>>s3;     //重载“>>”运算符
     cout<<"输入的字符串是:"<<s3<<endl;
     cout<<"输入的字符串长度是:"<<strlen(s3)<<endl;
     cout<<"测试下标运算符:\n";
     cout<<"s3中下标为2的字符为:"<<s3[2]<<endl;
     s3[2]='A';
     cout<<"执行s3[2]='A'后,s3变为:"<<s3<<endl;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值