Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Evaluacio d'una expressio amb parentesis X36902 jutge.org
- #include <stack>
- #include <iostream>
- using namespace std;
- int main () {
- stack<char> p;
- // Parentesis compta el nombre de parentesis '(' no tancats
- int parentesis = 0;
- // Claudators compta el nombre de claudators '[' no tancats
- int claudators = 0;
- bool correcte = true;
- char c;
- while (correcte and cin >> c and c != '.') {
- if (c == '(') {
- p.push(c);
- ++parentesis;
- }
- if (c == '[') {
- p.push(c);
- ++claudators;
- }
- if (c == ')') {
- if (p.empty() or p.top() == '[') correcte = false;
- else p.pop();
- --parentesis;
- }
- if (c == ']') {
- if (p.empty() or p.top() == '(') correcte = false;
- else p.pop();
- --claudators;
- }
- }
- correcte = correcte and (parentesis == 0) and (claudators == 0);
- if (correcte) cout << "Correcte";
- else cout << "Incorrecte";
- cout << endl;
- }
- // J. Muntaner
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement