Advertisement
JosepRivaille

X36902: Evaluacio d'una expressio amb parentesis

Oct 16th, 2015
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. bool calculate() {
  6.   char c;
  7.   int count_par = 0;
  8.   int count_cla = 0;
  9.   bool par_open = false;
  10.   bool cla_open = false;
  11.   while (cin >> c && c != '.') {
  12.     if (c == ')') {
  13.       --count_par;
  14.       if (cla_open) return false;
  15.       par_open = false;
  16.     }
  17.     else if (c == ']') {
  18.       --count_cla;
  19.       if (par_open) return false;
  20.       cla_open = false;
  21.     }
  22.     else if (c == '(') {
  23.       ++count_par;
  24.       par_open = true;
  25.       cla_open = false;
  26.     }
  27.     else if (c == '[') {
  28.       ++count_cla;
  29.       par_open = false;
  30.       cla_open = true;
  31.     }
  32.     if (count_par < 0 || count_cla < 0) {
  33.     return false;
  34.     }
  35.   }
  36.   return true;
  37. }
  38.  
  39.  
  40. int main() {
  41.   if (!calculate()) cout << "Incorrecte" << endl;
  42.   else cout << "Correcte" << endl;
  43. }
  44.  
  45. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement