#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class MyString {
private:
char *s;
public:
MyString(const char *str) {
s = new char[strlen(str) + 1];
strcpy(s, str);
}
MyString() { s = NULL; }
MyString(const MyString &str) {
if (str.s) {
s = new char[strlen(str.s) + 1];
strcpy(s, str.s);
}
else {
s = NULL;
}
}
~MyString() {
if (s) delete[]s;
}
MyString & operator=(const MyString &str);
MyString operator+(const MyString &str);
char & operator[](int i);
int operator<(MyString &);
int operator>(MyString &);
int operator==(MyString &);
MyString & operator=(const char *);
MyString operator+(const char *);
MyString& operator+=(const char *);
MyString operator()(int, int);
friend ostream & operator<<(ostream & o, MyString str);
friend MyString operator+(const char *p, const MyString &str);
};
MyString MyString::operator()(int first, int last) {
MyString tmp;
tmp.s = new char[last+1];
int i, j = 0;
for (i = first; last > 0; i++) {
tmp.s[j++] = s[i];
last--;
}
tmp.s[j] = '\0';
return tmp;
}
MyString operator+(const char *p, const MyString &str) {
MyString tmp(p);
tmp = tmp + str;
return tmp;
}
MyString & MyString::operator=(const MyString &str) {
if (s == str.s) return *this;
if (str.s == NULL) {
if (s) delete[]s;
s = NULL;
return *this;
}
if (s) delete[]s;
s = new char[strlen(str.s) + 1];
strcpy(s, str.s);
return *this;
}
MyString MyString::operator+(const MyString &str) {
MyString tmp;
int n = strlen(s) + strlen(str.s) + 1;
tmp.s = new char[n];
strcpy(tmp.s, s);
if (str.s)strcat(tmp.s, str.s);
return tmp;
}
char & MyString::operator[](int i) {
return s[i];
}
int MyString::operator<(MyString &str) {
int v = strcmp(s,str.s);
if (v == -1)
return 1;
else
return 0;
}
int MyString::operator>(MyString &str) {
int v = strcmp(s, str.s);
if (v == 1)
return 1;
else
return 0;
}
int MyString::operator==(MyString &str) {
int v = strcmp(s, str.s);
if (v == 0)
return 1;
else
return 0;
}
MyString & MyString::operator=(const char *str) {
if (s) delete[]s;
s = new char[strlen(str) + 1];
strcpy(s, str);
return *this;
}
MyString MyString::operator+(const char *str) {
MyString tmp;
tmp.s = new char[strlen(s) + strlen(str) + 1];
strcpy(tmp.s, s);
if(str)strcat(tmp.s, str);
return tmp;
}
MyString & MyString::operator+=(const char *str) {
*this = this->operator+(str);
return *this;
}
ostream & operator<<(ostream & o, MyString str) {
if (str.s == NULL) return o;
o << str.s;
return o;
}
int CompareString( const void * e1, const void * e2) {
MyString * s1 = (MyString * ) e1;
MyString * s2 = (MyString * ) e2;
if( *s1 < *s2 ) return -1;
else if( *s1 == *s2 ) return 0;
else if( *s1 > *s2 ) return 1;
}
int main() {
MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
MyString SArray[4] = {"big","me","about","take"};
cout << "1. " << s1 << s2 << s3<< s4<< endl;
s4 = s3; s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1; s1 = "ijkl-";
s1[2] = 'A' ;
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
qsort(SArray,4,sizeof(MyString), CompareString);
for( int i = 0;i < 4;++i )
cout << SArray[i] << endl;
//输出s1从下标0开始长度为4的子串
cout << s1(0,4) << endl;
//输出s1从下标为5开始长度为10的子串
cout << s1(5,10) << endl;
return 0;
}
MyString类
最新推荐文章于 2020-05-04 04:40:14 发布