cyalue

stack problem

Jul 9th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int arra[12], cmd;
  7. void push(int x) {
  8.     cmd++;
  9.     arra[cmd] = x;
  10. }
  11.  
  12. int pop() {
  13.     int x = arra[cmd];
  14.     cmd--;
  15.     return x;
  16. }
  17.  
  18. bool isEmpty() {
  19.     if (cmd == -1) {
  20.         return true;
  21.     }
  22.     return false;
  23. }
  24.  
  25. int top() {
  26.     return arra[cmd];
  27. }
  28.  
  29. int main() {
  30.     int n = 10;
  31.     char str[6] = { '(', '[', '{', ')', ']', '}' };
  32.     const char* vars = "((([][]{}))]";
  33.     int abstr[12];
  34.     for (int i = 0; i < 12; i++) {
  35.         abstr[i] = 0;
  36.         for (int j = 0; j < 6; j++) {
  37.             if (vars[i] == str[j]) {
  38.                 abstr[i] = j;
  39.                 break;
  40.             }
  41.         }
  42.     }
  43.     cmd = -1;
  44.     for (int i = 0; i < 12; i++) {
  45.         if (abstr[i] < 3) {
  46.             push(abstr[i]);
  47.         }
  48.         else {
  49.             if (abstr[i] == arra[cmd] + 3) {
  50.                 pop();
  51.             }
  52.             else {
  53.                 cout << "this is not correct";
  54.                 return 0;
  55.             }
  56.         }
  57.     }
  58.     if (isEmpty()) {
  59.         cout << "this is correct";
  60.     }
  61.     else {
  62.         cout << "this is not correct";
  63.     }
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment