#include <iostream>
#include <cstring>
#include <stdexcept>
using namespace std;
class myString {
private:
int size;
char *str;
public:
// 无参构造
myString() : size(10) {
str = new char[size + 1];
str[0] = '\0';
}
// 有参构造
myString(const char *s) {
size = strlen(s);
str = new char[size + 1];
strcpy(str, s);
}
// 有参构造
myString(int n, char ch) : size(n) {
str = new char[size + 1];
memset(str, ch, size);
str[size] = '\0';
}
// 析构函数
~myString() {
delete[] str;
}
// 拷贝构造函数
myString(const myString &other) : size(other.size) {
str = new char[size + 1];
strcpy(str, other.str);
}
// 拷贝赋值函数
myString &operator=(const myString &other) {
if (this != &other) {
delete[] str;
size = other.size;
str = new char[size + 1];
strcpy(str, other.str);
}
return *this;
}
// 判空函数
bool empty() const {
return size == 0;
}
// size函数
int length() const {
return size;
}
// c_str函数
const char *c_str() const {
return str;
}
// at函数
char &at(int index) {
if (index < 0 || index >= size) {
throw std::out_of_range("Index out of range");
}
return str[index];
}
// 二倍扩容
void expend() {
size *= 2;
char *newStr = new char[size + 1];
strcpy(newStr, str);
delete[] str;
str = newStr;
}
// += 运算符重载
myString &operator+=(const myString &other) {
int newSize = size + other.size;
char *newStr = new char[newSize + 1];
strcpy(newStr, str);
strcat(newStr, other.str);
delete[] str;
str = newStr;
size = newSize;
return *this;
}
// [] 运算符重载
char &operator[](int index) {
return at(index);
}
// + 运算符重载
myString operator+(const myString &other) const {
myString result;
result.size = size + other.size;
result.str = new char[result.size + 1];
strcpy(result.str, str);
strcat(result.str, other.str);
return result;
}
// == 运算符重载
bool operator==(const myString &other) const {
return strcmp(str, other.str) == 0;
}
// != 运算符重载
bool operator!=(const myString &other) const {
return !(*this == other);
}
// < 运算符重载
bool operator<(const myString &other) const {
return strcmp(str, other.str) < 0;
}
// > 运算符重载
bool operator>(const myString &other) const {
return strcmp(str, other.str) > 0;
}
// <= 运算符重载
bool operator<=(const myString &other) const {
return !(*this > other);
}
// >= 运算符重载
bool operator>=(const myString &other) const {
return !(*this < other);
}
// << 运算符重载 (流输出)
friend ostream &operator<<(ostream &os, const myString &s) {
os << s.str;
return os;
}
// >> 运算符重载 (流输入)
friend istream &operator>>(istream &is, myString &s) {
char buffer[1024]; // 假设最大输入长度为1024
is >> buffer;
s = myString(buffer); // 使用已有的构造函数
return is;
}
};
int main() {
myString s1("Hello");
myString s2(" World");
myString s3 = s1 + s2; // 使用 + 运算符重载
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
cout << "s3: " << s3 << endl;
cout << "s1 length: " << s1.length() << endl;
cout << "s1[1]: " << s1[1] << endl;
// 测试比较运算符
cout << "s1 == s2: " << (s1 == s2) << endl;
cout << "s1 != s2: " << (s1 != s2) << endl;
cout << "s1 < s2: " << (s1 < s2) << endl;
// 测试输入
myString s4;
cout << "Enter a string: ";
cin >> s4;
cout << "You entered: " << s4 << endl;
return 0;
}
