hpnq

2laba

Oct 3rd, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. //speed coding handle
  3.  
  4. #define mp make_pair
  5. #define cve(tpy) for (auto i : tpy) {for(auto j : i){cout << j << " "; }cout << "\n";} ;
  6. #define f first
  7. #define s second
  8. #define loop(i, x, n) for (int i = x; i < n; i++)
  9. #define joop(x, n) for (ll j = x; j < n; j++)
  10. #define lp(n) for (ll i = 0; i < n; i++)
  11. #define err cout << "ERROR" << endl;
  12. #define all(x) x.begin(), x.end()
  13. #define pb push_back
  14. #define sz(x) x.size()
  15. #define rndm rng()
  16.  
  17. // types
  18. #define pii pair<int, int>
  19. #define pll pair<ll, ll>
  20. #define vvi vector<vector<int>>
  21. #define vvll vector<vector<ll>>
  22. typedef long long ll;
  23. typedef long double ld;
  24.  
  25. // types of data
  26. #define inf 1000000000
  27. #define infll 1000000000000000000
  28. #define INF ll(1e9)
  29.  
  30. #define md 998244353
  31. #define mod 1000000009
  32. //#define K 239017
  33.  
  34. #define DEBUG 1
  35. using namespace std;
  36. mt19937_64 rng(113113);
  37. uniform_int_distribution<ll> drist;
  38. //const int INF = numeric_limits<int>::max();
  39.  
  40. class TString {
  41. private:
  42. char* str;
  43. int length;
  44.  
  45. public:
  46. // ����������� ��� ����������
  47. TString() : str(nullptr), length(0) {}
  48.  
  49. // ����������� � �����������
  50. TString(const char* s) {
  51. length = strlen(s);
  52. str = new char[length + 1];
  53. strcpy(str, s);
  54. }
  55.  
  56. // ����������� �����������
  57. TString(const TString& other) {
  58. length = other.length;
  59. str = new char[length + 1];
  60. strcpy(str, other.str);
  61. }
  62.  
  63. // ����������
  64. virtual ~TString() {
  65. delete[] str;
  66. }
  67.  
  68. // ����� ������
  69. void print(){
  70. if (str) {
  71. cout << str << endl;
  72. }
  73. }
  74.  
  75. // ����� ������ � �� ����� �� �����
  76. void printWithLength(){
  77. if (str) {
  78.  
  79. cout << str << " (Length: " << length << ")" << endl;
  80. }
  81. }
  82.  
  83. // ���������� ����� ������ �� �������� ��������
  84. void increaseLength(int increment) {
  85. if (increment > 0) {
  86. char* newStr = new char[length + increment + 1];
  87. if (str) {
  88. strcpy(newStr, str);
  89. } else {
  90. newStr[0] = '\0';
  91. }
  92. delete[] str;
  93. str = newStr;
  94. length += increment;
  95. str[length] = '\0';
  96. }
  97. }
  98.  
  99. // ����������� � ������ ��������� �������
  100. void appendChar(char c) {
  101. increaseLength(1);
  102. str[length - 1] = c;
  103. str[length] = '\0';
  104. }
  105.  
  106. // ��������� ������ �� �������� ��������
  107. void setString(const char* s) {
  108. delete[] str;
  109. length = strlen(s);
  110. str = new char[length + 1];
  111. strcpy(str, s);
  112. }
  113.  
  114. // ��������� ��������� �� ������ ������
  115. TString substring(int start, int count) const {
  116. if (start < 0 || start >= length || count <= 0 || start + count > length) {
  117. return TString(""); // Return an empty string for invalid ranges
  118. }
  119.  
  120. char* subStr = new char[count + 1];
  121. strncpy(subStr, str + start, count);
  122. subStr[count] = '\0';
  123.  
  124. TString result(subStr);
  125. delete[] subStr;
  126. return result;
  127. }
  128.  
  129. // ����� ������ ���������� ������
  130. const char* getString() const {
  131. return str;
  132. }
  133.  
  134. // ����������� � ������ �������� ������ (������������)
  135. void appendString(const TString& other) {
  136. if (other.str) {
  137. char* newStr = new char[length + other.length + 1];
  138. if (str) {
  139. strcpy(newStr, str);
  140. }
  141. if (other.str) {
  142. strcpy(newStr + length, other.str);
  143. }
  144. delete[] str;
  145. str = newStr;
  146. length += other.length;
  147. str[length] = '\0';
  148. }
  149. }
  150.  
  151. // ��������� ����� ������ ������ � ������ ������ ������
  152. int compareLength(const TString& other) const {
  153. if (length < other.length) {
  154. return -1;
  155. } else if (length > other.length) {
  156. return 1;
  157. } else {
  158. return 0;
  159. }
  160. }
  161.  
  162.  
  163. TString& operator =(const TString& other);
  164.  
  165. friend std::ostream& operator << (std::ostream& os, const TString& other);
  166. friend std::istream& operator >> (std::istream& in, TString& other);
  167.  
  168. bool operator ==(const TString& other);
  169.  
  170. TString& operator ++();
  171. TString& operator --();
  172.  
  173. bool operator >(const TString& other);
  174. TString operator+(const TString& other);
  175.  
  176. TString& operator+=(const TString& other);
  177. TString& operator !();
  178.  
  179. };
  180.  
  181. TString& TString::operator =(const TString& other){
  182. // detele []str;
  183. str = other.str;
  184. length = other.length;
  185. return *this;
  186. }
  187.  
  188. std::ostream& operator << (std::ostream& os, const TString& other) {
  189. loop(i, 0, other.length){
  190. os << other.str[i];
  191. }
  192. return os;
  193. }
  194.  
  195. std::istream& operator >> (std::istream& in, TString& other) {
  196. string s;
  197.  
  198. getline(in, s);
  199.  
  200. other.setString(s.c_str());
  201.  
  202. return in;
  203. }
  204.  
  205. bool TString::operator ==(const TString& other){
  206. if(compareLength(other) != 0){
  207. return 0;
  208. }
  209. if(strcmp(str, other.str) != 0){
  210. return 0;
  211. }
  212. return 1;
  213. }
  214.  
  215. TString& TString::operator ++(){
  216. appendChar(' ');
  217. return *this;
  218. }
  219.  
  220. bool TString::operator >(const TString& other){
  221. return (str > other.str);
  222. }
  223.  
  224. TString& TString::operator --(){
  225. string s = this->str;
  226. // cout << s << "\n";
  227. s.pop_back();
  228. this->setString(s.c_str());
  229. return *this;
  230. }
  231.  
  232.  
  233. TString TString::operator+(const TString& other){
  234. // cout << other.str;
  235. TString ts;
  236. string temp1 = other.str;
  237. string temp2 = str; // idk why (*other).str doesn't work
  238. ts.setString((temp2 + temp1).c_str());
  239. return ts;
  240. }
  241.  
  242.  
  243. TString& TString::operator+=(const TString& other){
  244. this->appendString(other);
  245. return *this;
  246. }
  247.  
  248. TString& TString::operator!(){
  249. for(int i = 0; 2*i < length; i++){
  250. swap(str[i], str[length-i-1]);
  251. }
  252. return *this;
  253.  
  254. }
  255.  
  256. class TIntName : TString{
  257. private:
  258. int zn;
  259. public:
  260. TIntName():TString(){
  261. zn = 0;
  262. }
  263.  
  264. TIntName(const char* s, int zn): TString(s){
  265. this->zn = zn;
  266. }
  267.  
  268. TIntName(const TIntName& other): TString(other.getString()){
  269. this->zn = zn;
  270. }
  271.  
  272. void printint(){
  273. printWithLength();
  274. cout << zn << "\n";
  275. }
  276. void changeInt(int zn){
  277. this->zn = zn;
  278. }
  279.  
  280. bool comparezn(const TIntName* other){
  281. if(other->zn == this->zn){
  282. return true;
  283. }else{
  284. return false;
  285. }
  286. }
  287.  
  288. bool compare2zn(const TIntName* other){
  289. if(other->zn > this->zn){
  290. return true;
  291. }else{
  292. return false;
  293. }
  294. }
  295. ~TIntName(): ~TString(){}
  296.  
  297.  
  298. };
  299.  
  300. int main() {
  301. // ���������� ��������
  302. // cin.ignore(); // clear the input buffer
  303. string s = "abc";
  304. // getline(cin, s);
  305. TString str1(s.c_str());
  306. --str1;
  307.  
  308. TString str2;
  309. cout << "Enter second:\n";
  310.  
  311. cin >> str2;
  312. // str1 = str2;
  313.  
  314.  
  315. cout << str2 << "\n";
  316. // return 1;
  317. //
  318. cout << "check equals: str1 and str2 : " << (str2==str1 ? "Yes" : "No") << "\n";
  319. --str1;
  320. ++str2;
  321. cout << "increment and decrement :" << str1 << " " << str2 << "\n";
  322. //
  323. cout << "check str1 > str2 ? : " << (str1 > str2 ? "Yes" : "No" ) << "\n";
  324. //
  325. cout << "str1 plus str2 = " << str1+str2 << "\n";
  326. //
  327. str1+= str2;
  328. //
  329. cout << "and equals but += : " << str1 << '\n';
  330. //
  331. //
  332. str1 = str2;
  333. cout << "str1 = str2: " << str1 << "\n";
  334. //
  335. cout << "So and reverse str2(!str2): ";
  336. cout << !str2 << '\n';
  337.  
  338.  
  339. return 0;
  340. }
  341.  
Advertisement
Add Comment
Please, Sign In to add comment