fork download
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. template<class T>
  5. class Stack {
  6.  
  7. public:
  8. Stack() = default;
  9. Stack(const Stack& other) :size(other.size) {
  10. this->data = new T[other.size];
  11. for (size_t i = 0; i < other.size; i++) {
  12. this->data[i] = other.data[i];
  13. }
  14. }
  15.  
  16. Stack& operator=(const Stack& x) {
  17. if (&x != this) {
  18. delete[] data;
  19.  
  20. this->data = new T[x.size];
  21. for (size_t i = 0; i < x.size; i++) {
  22. this->data[i] = x.data[i];
  23. }
  24. size = x.size;
  25. }
  26. return *this;
  27. }
  28.  
  29. Stack(Stack&& other) noexcept : size(other.size), data(other.data) {
  30. other.data = nullptr;
  31. other.size = 0;
  32. }
  33.  
  34. Stack& operator=(Stack&& x) noexcept {
  35. if (&x != this) {
  36. delete[] data;
  37. size = x.size;
  38. data = x.data;
  39.  
  40. x.data = nullptr;
  41. x.size = 0;
  42.  
  43. }
  44. return *this;
  45. }
  46. void setSize(size_t size) {
  47. this->size = size;
  48. }
  49. void push(int element) {
  50. T* copy_arr = new T[size + 1];
  51. for (size_t i = 0; i < size; i++) {
  52. copy_arr[i + 1] = data[i];
  53. }
  54. copy_arr[0] = element;
  55. size++;
  56. delete[] data;
  57. data = copy_arr;
  58. }
  59. void pop() {
  60. T* copy_arr = new T[size - 1];
  61. for (size_t i = 0; i < size - 1; i++) {
  62. copy_arr[i] = data[i + 1];
  63. }
  64. size--;
  65. delete[] data;
  66. data = copy_arr;
  67. }
  68. void swap(Stack& other) {
  69. if (this == &other)
  70. return;
  71. size_t tmpSz = other.size;
  72. other.size = size;
  73. size = tmpSz;
  74. T* tmpData = other.data;
  75. other.data = data;
  76. data = tmpData;
  77. }
  78. T top() {
  79. return data[size-1];
  80. }
  81. bool isEmpty() {
  82. return size == 0;
  83. }
  84. int get(size_t index) {
  85. return data[index];
  86. }
  87. int getSize() {
  88. return size;
  89. }
  90.  
  91.  
  92. ~Stack() {
  93.  
  94. }
  95.  
  96. private:
  97. size_t size;
  98. T* data = new T[size];
  99. };
  100.  
  101. void print(Stack<int>& stack) {
  102. for (size_t i = 0; i < stack.getSize(); i++) {
  103. std::cout.width(7);
  104. std::cout << stack.get(i);
  105. }
  106. std::cout << std::endl;
  107. }
  108.  
  109. void task1() {
  110.  
  111. Stack<int> stack;
  112. stack.push(1);
  113. stack.push(2);
  114. stack.push(3);
  115.  
  116. print(stack);
  117. stack.pop();
  118. stack.pop();
  119. print(stack);
  120. std::cout.width(7);
  121. std::cout << stack.top();
  122. }
  123. bool brackets(const std::string& line) {
  124. Stack<char> box;
  125. const std::string left{ "([{" };
  126. const std::string rigth{ ")]}" };
  127. bool balance = true;
  128. for (auto letter : line) {
  129. if (rigth.find(letter) != std::string::npos) {
  130. if (box.isEmpty() || box.top() != letter) {
  131. balance = false;
  132. break;
  133. }
  134. else box.pop();
  135. }
  136. auto pos = left.find(letter);
  137. if (pos != std::string::npos) box.push(rigth.at(pos));
  138. }
  139. return balance;
  140. }
  141. bool brackets(const char* line) {
  142. return brackets(std::string(line));
  143. }
  144.  
  145.  
  146.  
  147. void task2() {
  148. std::cout << ">>> ";
  149. std::string line;
  150. getline(std::cin, line);
  151. std::cout << (brackets(line) ? "YES" : "NO") << '\n';
  152. system("pause");
  153. }
  154.  
  155. int main() {
  156. //task1();
  157. task2();
  158. }
Success #stdin #stdout #stderr 0s 4172KB
stdin
Standard input is empty
stdout
>>> YES
stderr
sh: 1: pause: not found