+-+

//+-=
#include<iostream.h>
#include<string.h>
class string
{
protected:
 char *sp;
public:
 string(){sp=0;}  
 string(string &);
 string(char *s)  
 {
  sp=new char[strlen(s)+1];
  strcpy(sp,s);
 }
 ~string (){if(sp)delete sp;} 
 void show(){cout<<sp<<endl;}
 string & operator = (string &);  
 friend string operator + (string &,string &); 
 string operator - (string &); 
 string operator - (char);   
 int operator > (string &);   
};
string::string(string &s)
{
 if(s.sp){
  sp=new char[strlen(s.sp)+1];
  strcpy(sp,s.sp);
 }
 else
  sp=0;
}
string operator + (string &s1,string &s2)
{
 string t;
 t.sp=new char[strlen(s1.sp)+strlen(s2.sp)+1];
 strcpy(t.sp,s1.sp);  
 strcat(t.sp,s2.sp);  
 return t;
}
string string::operator - (string &s)
{
 string t1=*this;
 char *p;
 while(1)
 {
  if(p=strstr(t1.sp,s.sp))    
  { 
   if(strlen(t1.sp)==strlen(s.sp))  
   {
    delete t1.sp;    
    t1.sp=0;     
    break;       
   }
   string t2;
   t2.sp=new char[strlen(t1.sp)-strlen(s.sp)+1];
   char *p1=t1.sp,*p2=t2.sp;
   int i=strlen(s.sp);
   while(p1<p) *p2++=*p1++;
   while(i){p1++;i--;}   
   while(*p2++=*p1++);  
   t1=t2;
  }
  else break;
 }
 return t1;
}
string string::operator - (char s)
{
 string t1;
 int i=0,flag=0;
 t1.sp=new char[strlen(sp)+1];
 char *p1=sp,*p2=t1.sp;
 while(*p1)
 {
  if(*p1!=s){
   *p2++=*p1++;
   i++;
   flag=1;
  }
  else p1++;
 }
 *p2='\0';
 return t1;
}
int string::operator > (string &s)
{
 if(strcmp(sp,s.sp)>0)  
 {
  return 1;
 }
 else return 0;
}
string & string::operator = (string &s) 
{
 if(sp) delete sp; 
 if(s.sp)
 {
  sp=new char[strlen(s.sp)+1]; 
  strcpy(sp,s.sp);
 }
 else sp=0;
 return *this;
}
void main()
{
 string s1("southeast"),s2("as"),s3,s4,s5;
 s1.show();
 s2.show();
 s3=s1+s2;
 s3.show();
 if(s1>s2)
  cout<<"s1>s2成立!"<<endl;
 else
  cout<<"s1>s2不成立!"<<endl;
 s4=s3-s2;
 s4.show();
 s5=s3-'t';
 s5.show();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值