c++
来源:互联网 发布:oppor9怎么切换3g网络 编辑:程序博客网 时间:2024/05/23 23:13
<pre name="code" class="cpp">#include <iostream>using namespace std;class String{public: String(); String(const char*const ch); int getlen()const { return len;} friend ostream&operator<<(ostream &o, const String &s) { o<<s.str; return o; } friend istream&operator>>(istream &i, String &s) { i>>s.str; return i; } friend bool operator<(const String&str1, const String&str2) { if(strcmp(str1.str, str2.str) < 0){ return 1; }else return 0; } friend bool operator>(const String&str1, const String&str2) { if(strcmp(str1.str, str2.str) > 0){ return 1; }else return 0; } friend bool operator==(const String&str1, const String&str2) { if(strcmp(str1.str, str2.str) == 0){ return 1; }else return 0; } char &operator[](unsigned short int length); char operator[](unsigned short int length)const; String (const String&r); String &operator=(const String &s); private: unsigned short int len; char *str;};String::String(){ len = 0; str = new char[1]; str[0] = '\0';};String::String(const char*const ch){ len = strlen(ch); str = new char[len+1]; for(int i=0; i<len; i++){ str[i] = ch[i]; } str[len] = '\0';}; char & String::operator[](unsigned short int length){ if(length > len){ return str[len-1]; }else{ return str[length]; }}; char String::operator[](unsigned short int length)const{ cout<<"下标运算符const执行"<<endl; if(length > len){ return str[len-1]; }else{ return str[length]; }}; String::String (const String&rs){ len = rs.getlen(); str = new char[len+1]; for(int i=0; i<len; i++){ str[i] = rs[i]; } str[len]='\0'; cout<<"复制构造函数完成:"<<str<<endl; }; String& String::operator=(const String &s){ cout<<"operator=执行"<<endl; if(this == &s) { return *this; }else{ delete []str; len = s.getlen(); str = new char[len+1]; for(int i=0; i<len; i++){ str[i] = s[i]; } str[len] = '\0'; } return *this; } int main(){ String s1("aaa");cout<<"s1=aaa"<<endl; String s2("bbb");cout<<"s2=bbb"<<endl; int ch1 = s1>s2;if (ch1=1)cout<<"s1<s2"<<endl;else cout<<"s2>=s1"<<endl;String s3("aaa");cout<<"s3=aaa"<<endl;int ch3 = s1==s3;if (ch3=1)cout<<"s1s3相等"<<endl;else cout<<"并不相等"<<endl;int ch4 = s2==s3;if (ch4=1)cout<<"s2s3并不相等"<<endl;else cout<<"相等"<<endl; return 0;}
#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
- c
- c
- c
- c
- C
- c
- c
- c
- C+
- c
- C
- c
- c
- c
- C
- C
- c
- C
- Xcode高级调试技巧
- 黑马程序员——OC基础:block和Protocol及Category
- Missing number(5166)
- 二进制炸弹BOMB报告
- 《剑指Offer》面试题4习题
- c++
- C:\Vim\_vimrc
- [CF538E]Demiurges Play Again && 树形DP
- 安胜安全操作系统的隐蔽通道场景演示之二【最近访问时间信道】
- Android系统移植总结
- JDBC 如何获得自动生成的主键
- 字符编码笔记:ASCII,Unicode和UTF-8
- C:\Vim\VIMPROJ\vimrc\_vimrc
- ZigBee智能家居安防硬件设计