+-+
来源:互联网 发布:软件系统 编辑:程序博客网 时间:2024/06/06 00:30
//+-=#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