Advertisement
NikKrasavchik

Untitled

Mar 30th, 2020
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class String {
  7. public:
  8.     String();
  9.     String(const String&tmp);
  10.     String(char *str);
  11.     ~String();
  12.  
  13.     int length();
  14.     int find(String t);
  15.  
  16.     String &operator =(const String &);
  17.     char &operator [](int index);
  18.     bool operator ==(const String &tmp);
  19.     bool operator !=(const String &tmp);
  20.     bool operator > (const String &tmp);
  21.     bool operator >=(const String &tmp);
  22.     bool operator < (const String &tmp);
  23.     bool operator <=(const String &tmp);
  24.  
  25.     friend istream &operator >>(istream &in, String &tmp);
  26.     friend ostream &operator <<(ostream &out, const String &tmp);
  27.  
  28. private:
  29.     int len;
  30.     char *str;
  31. };
  32.  
  33. String::String()
  34. {
  35.     this->len = 0;
  36. }
  37.  
  38. String::String(const String &tmp)
  39. {
  40.     for (int i = 0; i < tmp.len; ++i)
  41.         this->str[i] = tmp.str[i];
  42.     this->len = tmp.len;
  43. }
  44.  
  45. String::String(char *str)
  46. {
  47.     strncpy(this->str, str, strlen(str));
  48. }
  49.  
  50. String::~String()
  51. {
  52.     delete [] this->str;
  53. }
  54.  
  55. int String::length()
  56. {
  57.     return this->len;
  58. }
  59.  
  60. String &String::operator=(const String &tmp)
  61. {
  62.     strncpy(this->str, tmp.str, tmp.len);
  63.     this->len = tmp.len;
  64.     return *(this);
  65. }
  66.  
  67. char &String::operator [](int index)
  68. {
  69.     return this->str[index];
  70. }
  71.  
  72. bool String::operator ==(const String &tmp)
  73. {
  74.     if (strcmp(this->str, tmp.str))
  75.         return false;
  76.     return true;
  77. }
  78.  
  79. bool String::operator !=(const String &tmp)
  80. {
  81.     if (strcmp(this->str, tmp.str))
  82.         return true;
  83.     return false;
  84. }
  85.  
  86. bool String::operator >(const String &tmp)
  87. {
  88.     if (strcmp(this->str, tmp.str) > 0)
  89.         return true;
  90.     return false;
  91. }
  92.  
  93. bool String::operator >=(const String &tmp)
  94. {
  95.     if ((strcmp(this->str, tmp.str) > 0) || !strcmp(this->str, tmp.str))
  96.         return true;
  97.     return false;
  98. }
  99.  
  100. bool String::operator <(const String &tmp)
  101. {
  102.     if (strcmp(this->str, tmp.str) < 0)
  103.         return true;
  104.     return false;
  105. }
  106.  
  107. bool String::operator <=(const String &tmp)
  108. {
  109.     if ((strcmp(this->str, tmp.str) < 0) || !strcmp(this->str, tmp.str))
  110.         return true;
  111.     return false;
  112. }
  113.  
  114. ostream &operator <<(ostream &out, const String &tmp)
  115. {
  116.     out << tmp.str;
  117.     return out;
  118. }
  119.  
  120. istream &operator >>(istream &in, String &tmp)
  121. {
  122.     char str[1000000];
  123.      in.getline(str, 1000000);
  124.  
  125.      size_t inputLength = strlen(str);
  126.      if (static_cast<char>(str[inputLength - 1]) == '\r') {
  127.      str[inputLength - 1] = '\0';
  128.      }
  129.  
  130.      if (tmp.len != 0)
  131.          delete [] tmp.str;
  132.  
  133.      tmp.len = int(strlen(str));
  134.      tmp.str = new char[tmp.len + 1];
  135.  
  136.      for (int i = 0; i < tmp.len; ++i) {
  137.          tmp.str[i] = str[i];
  138.      }
  139.      tmp.str[tmp.len] = '\0';
  140.  
  141.      return in;
  142. }
  143.  
  144. int main()
  145. {
  146.     String str1, str2;
  147.     char sign[3] = {};
  148.     cin >> str1 >> sign >> str2;
  149.  
  150.     if (strlen(sign) == 1) {
  151.         if (sign[0] == '>')
  152.             cout << (str1 > str2 ? "YES" : "NO");
  153.         else if (sign[0] == '<')
  154.             cout << (str1 < str2 ? "YES" : "NO");
  155.     } else {
  156.         if (sign[0] == '>')
  157.             cout << (str1 >= str2 ? "YES" : "NO");
  158.         else if (sign[0] == '<')
  159.             cout << (str1 <= str2 ? "YES" : "NO");
  160.         else if (sign[0] == '=')
  161.             cout << (str1 == str2 ? "YES" : "NO");
  162.         else if (sign[0] == '!')
  163.             cout << (str1 != str2 ? "YES" : "NO");
  164.     }
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement