Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <queue>
- using std::string;
- struct stack
- {
- std::queue<char> q1, q2;
- int curr_size;
- stack()
- {
- curr_size = 0;
- }
- void push(char x)
- {
- q1.push(x);
- curr_size++;
- }
- void pop()
- {
- if (q1.empty())
- return;
- while (q1.size() != 1)
- {
- q2.push(q1.front());
- q1.pop();
- }
- q1.pop();
- curr_size--;
- std::queue<char> q = q1;
- q1 = q2;
- q2 = q;
- }
- int top()
- {
- if (q1.empty())
- return -1;
- while (q1.size() != 1) {
- q2.push(q1.front());
- q1.pop();
- }
- char temp = q1.front();
- q1.pop();
- q2.push(temp);
- std::queue<char> q;
- q = q1;
- q1 = q2;
- q2 = q;
- return temp;
- }
- int size()
- {
- return curr_size;
- }
- bool empty()
- {
- return curr_size == 0;
- }
- };
- int anterior(char s)
- {
- if (s == '(' || s == ')')
- return 1;
- else if (s == '[' || s == ']')
- return 2;
- else if (s == '{' || s == '}')
- return 3;
- }
- bool verificare(std::string s,stack Stack)
- {
- for (int i = 0; i < s.size(); i++)
- {
- if (s[i] == '{' || s[i] == '[' || s[i] == '(')
- {
- Stack.push(s[i]);
- if (anterior(Stack.top()) <= anterior(s[i])) return false;
- }
- else if (s[i] == '}' || s[i] == ']' || s[i] == ')')
- {
- if (anterior(s[i]) != anterior(Stack.top())) return false;
- Stack.pop();
- }
- }
- if (Stack.size() != 0)
- return false;
- return true;
- }
- int main()
- {
- string s;
- std::cin >> s;
- stack Stack;
- if (verificare(s,Stack))
- {
- std::cout << "Expresia este valida.\n";
- }
- else
- {
- std::cout << "Expresia nu este valida.\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement