
程序代码:
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
class String {
public:
String(const char* = 0);
String(const String&);
~String();
public:
int getLength();
String& assign(const char*);
String& assign(const String&);
String& append(const char*);
String& append(const String&);
int compare(const char*);
int compare(const String&);
String& operator=(const char*);
String& operator=(const String&);
String& operator+=(const char*);
String& operator+=(const String&);
String operator+(const char*);
String operator+(const String&);
bool operator==(const char*);
bool operator==(const String&);
bool operator!=(const char*);
bool operator!=(const String&);
bool operator>(const char*);
bool operator>(const String&);
bool operator<(const char*);
bool operator<(const String&);
friend istream& operator>>(istream&, String&);
friend ostream& operator<<(ostream&, const String&);
private:
int capacity;
char* storage;
};
String::String(const char* cstr)
{
if (cstr) {
capacity = strlen(cstr) + 1;
storage = new char[capacity];
strcpy(storage, cstr);
} else {
capacity = 1;
storage = new char[capacity];
*storage = '\0';
}
}
String::String(const String& str)
{
capacity = str.capacity;
storage = new char[capacity];
strcpy(storage, str.storage);
}
String::~String()
{
if (storage)
delete[] storage;
}
int String::getLength()
{
return capacity - 1;
}
String& String::assign(const char* cstr)
{
if (storage)
delete[] storage;
capacity = strlen(cstr) + 1;
storage = new char[capacity];
strcpy(storage, cstr);
return *this;
}
String& String::assign(const String& str)
{
if (storage)
delete[] storage;
capacity = str.capacity;
storage = new char[capacity];
strcpy(storage, str.storage);
return *this;
}
String& String::append(const char* cstr)
{
capacity += strlen(cstr);
char* newStorage = new char[capacity];
strcpy(newStorage, storage);
strcat(newStorage, cstr);
delete[] storage;
storage = newStorage;
return *this;
}
String& String::append(const String& str)
{
capacity += str.capacity;
char* newStorage = new char[capacity];
strcpy(newStorage, storage);
strcat(newStorage, str.storage);
delete storage;
storage = newStorage;
return *this;
}
int String::compare(const char* cstr)
{
return strcmp(storage, cstr);
}
int String::compare(const String& str)
{
return strcmp(storage, str.storage);
}
String& String::operator=(const char* cstr)
{
return assign(cstr);
}
String& String::operator=(const String& str)
{
return assign(str);
}
String& String::operator+=(const char* cstr)
{
return append(cstr);
}
String& String::operator+=(const String& str)
{
return append(str);
}
String String::operator+(const char* cstr)
{
String tmp(*this);
tmp.append(cstr);
return tmp;
}
String String::operator+(const String& str)
{
String tmp(*this);
tmp.append(str);
return tmp;
}
bool String::operator==(const char* cstr)
{
return compare(cstr) == 0;
}
bool String::operator==(const String& str)
{
return compare(str) == 0;
}
bool String::operator!=(const char* cstr)
{
return compare(cstr) != 0;
}
bool String::operator!=(const String& str)
{
return compare(str) != 0;
}
bool String::operator>(const char* cstr)
{
return compare(cstr) > 0;
}
bool String::operator>(const String& str)
{
return compare(str) > 0;
}
bool String::operator<(const char* cstr)
{
return compare(cstr) < 0;
}
bool String::operator<(const String& str)
{
return compare(str) < 0;
}
void inflate(char** pBuffer, int* capacity, int incr)
{
char* newBuffer = new char[*capacity + incr];
strncpy(newBuffer, *pBuffer, *capacity);
delete[] *pBuffer;
*pBuffer = newBuffer;
*capacity += incr;
}
istream& operator>>(istream& in, String& str)
{
int capacity = 100;
int increment = 100;
char* buffer = new char[capacity], ch;
int i = 0;
while (in.get(ch) && !isspace(ch)) {
if (i == capacity)
inflate(&buffer, &capacity, increment);
buffer[i++] = ch;
}
if (i == capacity)
inflate(&buffer, &capacity, 1);
buffer[i] = '\0';
str.assign(buffer);
return in;
}
ostream& operator<<(ostream& out, const String& str)
{
return out << str.storage;
}
int main()
{
String strs[] = {
"As long as you love me",
"Iridescent",
"Love story",
"I'm yours",
"How to break a heart"
};
for (int i = 0; i < sizeof strs / sizeof *strs; ++i)
cout << strs[i] << endl;
String str;
cin >> str;
cout << str << endl;
cin >> str;
cout << str << endl;
cout << (strs[0] == strs[0]) << endl;
cout << (strs[0] == strs[1]) << endl;
cout << (strs[0] < strs[1]) << endl;
cout << (strs[1] < strs[0]) << endl;
cout << (strs[0] > strs[1]) << endl;
cout << (strs[1] > strs[0]) << endl;
strs[0] += strs[1];
cout << strs[0] << endl;
strs[1] += " Hello";
cout << strs[1] << endl;
str = strs[0] + " " + strs[1];
cout << str << endl;
}