标准答案:(附带完整代码框架
#include <cstdlib>
#include <iostream>
using namespace std;
int strlen(const char *s)
{
int i = 0;
for (; s[i]; ++i)
;
return i;
}
void strcpy(char *d, const char *s)
{
int i = 0;
for (i = 0; s[i]; ++i)
d[i] = s[i];
d[i] = 0;
}
int strcmp(const char *s1, const char *s2)
{
for (int i = 0; s1[i] && s2[i]; ++i)
{
if (s1[i] < s2[i])
return -1;
else if (s1[i] > s2[i])
return 1;
}
return 0;
}
void strcat(char *d, const char *s)
{
int len = strlen(d);
strcpy(d + len, s);
}
class MyString
{
//implement start from here
private:
char *str;
public:
MyString(const char *s = NULL)
{
if (s)
{
str = new char[strlen(s) + 1];
strcpy(str, s);
}
else
{
str = new char[1];
str[0] = '\0';
}
}
~MyString()
{
if (str)
delete[] str;
}
MyString(const MyString &s)
{
if (s.str)
{
str = new char[strlen(s.str) + 1];
strcpy(str, s.str);
}
else
{
str = new char[1];
str[0] = '\0';
}
}
MyString &operator=(const char *s)
{
if (str == s)
{
return *this;
}
delete[] str;
if (s)
{
str = new char[strlen(s) + 1];
strcpy(str, s);
}
else
{
str = new char[1];
str[0] = '\0';
}
return *this;
}
MyString &operator=(const MyString &s)
{
if (str == s.str)
{
return *this;
}
delete[] str;
if (s.str)
{
str = new char[strlen(s.str) + 1];
strcpy(str, s.str);
}
else
{
str = new char[1];
str[0] = '\0';
}
return *this;
}
friend MyString operator+(const MyString &a, const MyString &b)
{
char *tmp = new char[strlen(a.str) + strlen(b.str) + 1];
strcpy(tmp, a.str);
strcat(tmp, b.str);
return MyString(tmp);
}
friend ostream &operator<<(ostream &o, const MyString &s)
{
o << s.str;
return o;
}
char &operator[](int i)
{
return str[i];
}
MyString &operator+=(const char *s)
{
char *tmp = new char[strlen(str) + 1];
strcpy(tmp, str);
delete[] str;
str = new char[strlen(tmp) + strlen(s) + 1];
strcpy(str, tmp);
strcat(str, s);
return *this;
}
friend MyString operator+(const char *p, const MyString &s)
{
char *tmp = new char[strlen(p) + strlen(s.str) + 1];
strcpy(tmp, p);
strcat(tmp, s.str);
return MyString(tmp);
}
friend MyString operator+(const MyString &s, const char *p)
{
char *tmp = new char[strlen(s.str) + strlen(p) + 1];
strcpy(tmp, s.str);
strcat(tmp, p);
return MyString(tmp);
}
friend bool operator<(const MyString &s1, const MyString &s2)
{
if (strcmp(s1.str, s2.str) < 0)
{
return true;
}
else
{
return false;
}
}
friend bool operator>(const MyString &s1, const MyString &s2)
{
if (strcmp(s1.str, s2.str) > 0)
return true;
else
return false;
}
friend bool operator==(const MyString &s1, const MyString &s2)
{
if (strcmp(s1.str, s2.str) == 0)
return true;
else
return false;
}
char *operator()(int start, int length)
{
char *tmp = new char[length + 1];
for (int i = start; i < start + length; ++i)
{
tmp[i - start] = str[i];
}
tmp[length] = '\0';
return tmp;
}
//implement end to here
};
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;
}
我的填空实现:
private:
char *p;
public:
MyString(const char *arr = NULL)
{
if (arr)
{
p = new char[strlen(p) + 1];
strcpy(p, arr);
}
else
{
p = new char[1];
p[0] = '\0';
}
}
MyString(const MyString &s)
{ //!
if (s.p)
{
p = new char[strlen(s.p) + 1];
strcpy(p, s.p);
}
else
{
p = new char[1];
p[0] = '\0';
}
}
~MyString()
{
if (p)
delete[] p;
}
friend ostream &operator<<(ostream &os, const MyString &a)
{
os << a.p;
return os;
}
char &operator[](int i)
{
return p[i];
}
bool operator>(const MyString &a)
{
if (strcmp(p, a.p) > 0)
return true;
else
return false;
}
bool operator<(const MyString &a)
{
if (strcmp(p, a.p) < 0)
return true;
else
return false;
}
bool operator==(const MyString &a)
{
if (strcmp(p, a.p) == 0)
return true;
else
return false;
}
MyString &operator=(const MyString &a)
{
delete[] p;
if (a.p)
{
p = new char[strlen(a.p) + 1];
strcpy(p, a.p);
}
else
{
p = new char[1];
p[0] = '\0';
}
return *this;
}
MyString &operator=(const char *a)
{
delete[] p;
if (a)
{
p = new char[strlen(a) + 1];
strcpy(p, a);
}
else
{
p = new char[1];
p[0] = '\0';
}
return *this;
}
friend MyString operator+(const char *b, const MyString &a)
{
char *ret = new char[strlen(b) + strlen(a.p) + 1];
strcpy(ret, b);
strcat(ret, a.p);
return MyString(ret);
}
MyString operator+(const MyString &a)
{
char *tem = new char[strlen(p) + strlen(a.p) + 1];
strcpy(tem, p);
strcat(tem, a.p);
return MyString(tem);
}
MyString operator+(const char *a)
{
char *tem = new char[strlen(p) + strlen(a) + 1];
strcpy(tem, p);
strcat(tem, a);
return MyString(tem);
}
MyString &operator+=(const char *a)
{
char *tem = new char[strlen(p) + 1];
strcpy(tem, p);
delete[] p;
p = new char[strlen(tem) + strlen(a) + 1];
strcpy(p, tem);
strcat(p, a);
return *this;
}
char *operator()(int start, int length)
{
char *tmp = new char[length + 1];
for (int i = start; i < start + length; ++i)
{
tmp[i - start] = p[i];
}
tmp[length] = '\0';
return tmp;
}