Advertisement
aimon1337

Untitled

Mar 20th, 2022
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <queue>
  4. using std::string;
  5.  
  6. struct stack
  7. {
  8.     std::queue<char> q1, q2;
  9.     int curr_size;
  10.     stack()
  11.     {
  12.         curr_size = 0;
  13.     }
  14.     void push(char x)
  15.     {
  16.         q1.push(x);
  17.         curr_size++;
  18.     }
  19.     void pop()
  20.     {
  21.         if (q1.empty())
  22.             return;
  23.         while (q1.size() != 1)
  24.         {
  25.             q2.push(q1.front());
  26.             q1.pop();
  27.         }
  28.         q1.pop();
  29.         curr_size--;
  30.         std::queue<char> q = q1;
  31.         q1 = q2;
  32.         q2 = q;
  33.     }
  34.     int top()
  35.     {
  36.         if (q1.empty())
  37.             return -1;
  38.  
  39.         while (q1.size() != 1) {
  40.             q2.push(q1.front());
  41.             q1.pop();
  42.         }
  43.         char temp = q1.front();
  44.         q1.pop();
  45.         q2.push(temp);
  46.         std::queue<char> q;
  47.         q = q1;
  48.         q1 = q2;
  49.         q2 = q;
  50.         return temp;
  51.     }
  52.     int size()
  53.     {
  54.         return curr_size;
  55.     }
  56.     bool empty()
  57.     {
  58.         return curr_size == 0;
  59.     }
  60. };
  61.  
  62. int anterior(char s)
  63. {
  64.     if (s == '(' || s == ')')
  65.         return 1;
  66.     else if (s == '[' || s == ']')
  67.         return 2;
  68.     else if (s == '{' || s == '}')
  69.         return 3;
  70. }
  71. bool verificare(std::string s,stack Stack)
  72. {
  73.  
  74.     for (int i = 0; i < s.length(); i++)
  75.     {
  76.  
  77.         if(Stack.empty())
  78.         {
  79.             if(s[i] == '{' || s[i] == '[' || s[i] == '(')
  80.                 Stack.push(s[i]);
  81.         }
  82.         else
  83.         if (s[i] == '{' || s[i] == '[' || s[i] == '(')
  84.         {
  85.             //Stack.push(s[i]);
  86.             if (anterior(Stack.top()) <= anterior(s[i]))
  87.                 return false;
  88.             else
  89.             {
  90.                 Stack.push(s[i]);
  91.             }
  92.         }
  93.         else if (s[i] == '}' || s[i] == ']' || s[i] == ')')
  94.         {
  95.             if (anterior(s[i]) != anterior(Stack.top())) return false;
  96.             Stack.pop();
  97.  
  98.         }
  99.     }
  100.     if (Stack.size() != 0)
  101.         return false;
  102.  
  103.     return true;
  104. }
  105. int main()
  106. {
  107.     string s;
  108.     std::cin >> s;
  109.     stack Stack;
  110.     if (verificare(s,Stack))
  111.     {
  112.         std::cout << "Expresia este valida.\n";
  113.     }
  114.     else
  115.     {
  116.         std::cout << "Expresia nu este valida.\n";
  117.     }
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement