Advertisement
jmuntaner

Evaluacio d'una expressio amb parentesis: X36902

Mar 23rd, 2017
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. // Evaluacio d'una expressio amb parentesis X36902 jutge.org
  2.  
  3. #include <stack>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main () {
  9.     stack<char> p;
  10.     // Parentesis compta el nombre de parentesis '(' no tancats
  11.     int parentesis = 0;
  12.     // Claudators compta el nombre de claudators '[' no tancats
  13.     int claudators = 0;
  14.     bool correcte = true;
  15.     char c;
  16.     while (correcte and cin >> c and c != '.') {
  17.         if (c == '(') {
  18.             p.push(c);
  19.             ++parentesis;
  20.         }
  21.         if (c == '[') {
  22.             p.push(c);
  23.             ++claudators;
  24.         }
  25.         if (c == ')') {
  26.             if (p.empty() or p.top() == '[') correcte = false;
  27.             else p.pop();
  28.             --parentesis;
  29.         }
  30.         if (c == ']') {
  31.             if (p.empty()  or p.top() == '(') correcte = false;
  32.             else p.pop();
  33.             --claudators;
  34.         }
  35.     }
  36.     correcte = correcte and (parentesis == 0) and (claudators == 0);
  37.     if (correcte) cout << "Correcte";
  38.     else cout << "Incorrecte";
  39.     cout << endl;
  40. }
  41.  
  42. // J. Muntaner
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement