/*
输入
无
输出
1. abcd - efgh - abcd -
2. abcd -
3.
4. abcd - efgh -
5. efgh -
6. c
7. abcd -
8. ijAl -
9. ijAl - mnop
10. qrst - abcd -
11. abcd - qrst - abcd - uvw xyz
about
big
me
take
abcd
qrst - abcd -
*/
//vs 2015 编译过不了呢~ 05/23 2019
#include <cstdlib>
#include <iostream>
using namespace std;
//#pragma warning(disable:2556)
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
{
//your code starts here
private:
char * str;
int size;
public:
MyString() {
str = new char[2]; //确保分配的是数组
str[0] = 0;//既然是个字符串,里面起码也是个空串,不能让 str== NULL
size = 0;
}
MyString(const char * s) {
//如果 s == NULL,就让它出错吧
size = strlen(s);
str = new char[size + 1];
strcpy(str, s);
}
MyString & operator=(const char * s) {//如果 s == NULL,就让它出错吧
int len = strlen(s);
if (size < len) {
delete[] str;
str = new char[len + 1];
}
strcpy(str, s);
size = len;
return *this;
}
void duplicate(const MyString & s) {
if (size < s.size) { //否则就不用重新分配空间了
delete[] str;
str = new char[s.size + 1];
}
strcpy(str, s.str);
size = s.size;
}
MyString(const MyString & s) :size(0), str(new char[1]) {
duplicate(s);
}
MyString & operator=(const MyString & s) {
if (str == s.str)
return *this;
duplicate(s);
return *this;
}
bool operator==(const MyString & s) const {
return strcmp(str, s.str) == 0;
}
bool operator<(const MyString & s) const {
return strcmp(str, s.str) < 0;
}
bool operator>(const MyString & s) const {
return strcmp(str, s.str) > 0;
}
MyString operator + (const MyString & s) {
char * tmp = new char[size + s.size + 2];//确保能分配一个数组
strcpy(tmp, str);
strcat(tmp, s.str); MyString os(tmp);
delete[] tmp;
return os;
}
MyString & operator += (const MyString & s) {
char * tmp = new char[size + s.size + 2];
strcpy(tmp, str);
strcat(tmp, s.str);
size += s.size;
delete[] str;
str = tmp;
return *this;
}
char & operator[](int i) const {
return str[i];
}
MyString operator()(int start, int len) const {
char * tmp = new char[len + 1];
for (int i = 0; i < len; ++i)
tmp[i] = str[start + i];
tmp[len] = 0;
MyString s(tmp);
delete[] tmp;
return s;
}
~MyString() { delete[] str; }
friend ostream & operator << (ostream & o, const MyString & s)
{
o << s.str;
return o;
}
friend MyString operator +(const char * s1, const MyString & s2)
{
MyString tmp(s1);
tmp += s2;
return tmp;
}
//your code ends 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;
}
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class MyString :public string
{
public:
MyString() :string() {};
MyString(const char* s) :string(s) {};
MyString(const string &s) :string(s) {};
MyString operator()(int s, int l)
{
return substr(s, l);
}
};
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;
sort(SArray, SArray + 4);
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;
}