自定义 mystring 类
#include<iostream>
using namespace std;
class mystring {
char* str;
int maxsize;
int last;
public:
mystring();
mystring(const char *s);
mystring(mystring & st);
~mystring();
void show();
char& operator[](int i);
mystring & operator=(const mystring &);
mystring & operator=(const char *);
mystring operator+(const mystring &);
mystring operator+=(const mystring &);
bool operator<(mystring &);
bool operator==(mystring &);
};
mystring::mystring()
{
str = NULL;
maxsize = 0;
last = -1;
}
mystring::mystring(const char *s)
{
str = new char[strlen(s) + 1];
maxsize = strlen(s)+1;
last = maxsize - 1;
strcpy_s(str, strlen(s) + 1, s);
}
mystring::mystring(mystring & st)
{
str = new char[strlen(st.str) + 1];
maxsize = strlen(st.str) + 1;
last = maxsize - 1;
strcpy_s(str, strlen(st.str) + 1, st.str);
}
mystring::~mystring()
{
if (str != NULL)
{
delete[] str;
str = NULL;
}
}
void mystring::show()
{
cout << str << endl;
}
char& mystring::operator[](int i)
{
return str[i];
}
mystring & mystring::operator=(const mystring & Be_Copied)
{
last = Be_Copied.last;
maxsize = Be_Copied.maxsize;
if (str != NULL)
{
delete[] str;
str = NULL;
}
str = new char[maxsize];
strcpy_s(str, maxsize, Be_Copied.str);
return *this;
}
mystring & mystring::operator=(const char *C_string )
{
if (str != NULL)
{
delete[] str;
str = NULL;
}
maxsize = strlen(C_string) + 1;
str = new char[maxsize];
last = maxsize - 1;
strcpy_s(str, maxsize, C_string);
return *this;
}
mystring mystring::operator+(const mystring & Be_Added)
{
mystring temp;
temp.maxsize = maxsize + Be_Added.maxsize-1;
temp.last = temp.maxsize - 1;
temp.str = new char[temp.maxsize];
strcpy_s(temp.str, maxsize, str);
strcat_s(temp.str, temp.maxsize,Be_Added.str);
return temp;
}
mystring mystring::operator+=(const mystring & Be_added)
{
mystring temp=*this;
if (str != NULL)
{
delete[] str;
str = NULL;
}
maxsize += (Be_added.maxsize - 1);
last = maxsize - 1;
str = new char[maxsize];
strcpy_s(str, maxsize, temp.str);
strcat_s(str, maxsize, Be_added.str);
temp = *this;
return temp;
}
bool mystring::operator<(mystring & Be_Judged)
{
int i = 0;
for (; i <= last; i++)
{
if(str[i]<Be_Judged.str[i])
{
return true;
}
else if (str[i] > Be_Judged.str[i])
{
return false;
}
else if (str[i] == Be_Judged.str[i])
{
continue;
}
}
if (i >= last)
{
return false;
}
}
bool mystring::operator==(mystring & Be_Judged)
{
if (Be_Judged.last != last)return false;
int i = 0;
for (; i <= last; i++)
{
if (str[i] != Be_Judged.str[i])
{
return false;
}
else if (str[i] == Be_Judged.str[i])
{
continue;
}
}
if (i >= last)
{
return true;
}
}
int main()
{
mystring A_string("Good Job!");
A_string.show();
cout << A_string[0] << endl;
A_string[3] = 'T';
cout << "A_string is "; A_string.show();
mystring Sec_string("Well Done!");
Sec_string.show();
mystring Friend("Hello My Friend!");
Sec_string = Friend;
cout << "Sec_string is "; Sec_string.show();
mystring Thi_string("Well Done!");
Thi_string = "Love You!";
cout << "Thi_string is "; Thi_string.show();
mystring For_string;
For_string = (Friend + Thi_string);
cout << "For_string is "; For_string.show();
mystring Fif_string("Rival ");
Fif_string += Thi_string;
Fif_string.show();
mystring Six_string="Oh My God!";
mystring Sev_string = "oh My God!";
if (Sev_string < Six_string)cout << "Yes!" << endl;
else cout << "No!" << endl;
mystring Eig_string = "Imagine Dragons!";
mystring Nin_string = "Imagine Dragons!";
if (Eig_string == Nin_string)cout << "Yes!" << endl;
else cout << "No!" << endl;
system("pause");
return 0;
}