| 
 | 
 
 
发表于 2021-4-16 12:04:00
|
显示全部楼层
 
 
 
 本帖最后由 yuxijian2020 于 2021-4-16 13:03 编辑  
- #include <iostream>
 
  
- using namespace std;
 
  
- constexpr int MAX_LEN = 1024;
 
  
- class _STR
 
 - {
 
 - public:
 
 -     _STR() : str(nullptr), len(0) {}
 
 -     ~_STR();
 
  
-     _STR(const char* str);
 
 -     _STR(_STR& other);
 
  
-     _STR& operator=(_STR& other);
 
 -     _STR& operator=(const char* cstr);
 
 -     _STR operator+(const _STR& other);
 
 -     _STR operator+=(const _STR& other);
 
 -     bool  operator==(const _STR& other);
 
  
-     friend ostream& operator<<(std::ostream& os, const _STR& __str);
 
 -     friend istream& operator>>(std::istream& is, _STR& __str);
 
  
- private:
 
 -     char* str;
 
 -     unsigned int len;
 
 - };
 
  
- _STR::~_STR()
 
 - {
 
 -     len = 0;
 
  
-     if (str != nullptr)
 
 -     {
 
 -         delete[] str;
 
 -     }
 
 - }
 
  
- _STR::_STR(const char* str)
 
 - {
 
 -     len = strlen(str);
 
 -     this->str = new char[len + 1];
 
 -     if (str == nullptr)
 
 -     {
 
 -         len = 0;
 
 -         return;
 
 -     }
 
  
-     strcpy_s(this->str, len + 1, str);
 
 - }
 
  
- _STR::_STR(_STR& other)
 
 - {
 
 -     len = other.len;
 
 -     str = new char[len + 1];
 
 -     if (str == nullptr)
 
 -     {
 
 -         len = 0;
 
 -         return;
 
 -     }
 
  
-     strcpy_s(str, len + 1, other.str);
 
 - }
 
  
- _STR& _STR::operator=(_STR& other)
 
 - {
 
 -     if (this->str != nullptr)
 
 -         delete[] this->str;
 
  
-     len = other.len;
 
 -     str = new char[len + 1];
 
 -     if (str == nullptr)
 
 -     {
 
 -         len = 0;
 
 -         return *this;
 
 -     }
 
  
-     strcpy_s(str, len + 1, other.str);
 
  
-     return *this;
 
 - }
 
  
- _STR& _STR::operator=(const char* cstr)
 
 - {
 
 -     if (this->str != nullptr)
 
 -         delete[] this->str;
 
  
-     len = strlen(cstr);
 
 -     str = new char[len + 1];
 
 -     if (str == nullptr)
 
 -     {
 
 -         len = 0;
 
 -         return *this;
 
 -     }
 
  
-     strcpy_s(str, len + 1, cstr);
 
  
-     return *this;
 
 - }
 
  
- _STR _STR::operator+(const _STR& other)
 
 - {
 
 -     int l = other.len + this->len;
 
 -     char* temp = new char[l + 1];
 
 -     if (temp == nullptr)
 
 -         return *this;
 
  
-     strcpy_s(temp, len + 1, this->str);
 
 -     strcat_s(temp, other.len + 1, other.str);
 
  
-     return _STR(temp);
 
 - }
 
  
- _STR _STR::operator+=(const _STR& other)
 
 - {
 
 -     char* temp = new char[len + other.len + 1];
 
 -     strcpy_s(temp, len + 1, str);
 
 -     strcat_s(temp, other.len, other.str);
 
 -     delete[] str;
 
 -     str = temp;
 
 -     len = len + other.len;
 
  
-     return *this;
 
 - }
 
  
- bool _STR::operator==(const _STR& other)
 
 - {
 
 -     if (strcmp(this->str, other.str) == 0)
 
 -         return true;
 
  
-     return false;
 
 - }
 
  
- ostream& operator<<(std::ostream& os, const _STR& __str)
 
 - {
 
 -     os << __str.str << endl;
 
 -     return os;
 
 - }
 
  
- istream& operator>>(std::istream& is, _STR& __str)
 
 - {
 
 -     char temp[MAX_LEN];
 
 -     char c;
 
 -     int i = 0;
 
  
-     while (cin.get(c))
 
 -     {
 
 -         if (c == '\n')
 
 -             break;
 
  
-         temp[i] = c;
 
 -         i++;
 
 -     }
 
  
-     temp[++i] = '\0';
 
 -     __str = (char*)temp;
 
  
-     return is;
 
 - }
 
  复制代码 |   
 
 
 
 |