Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. using namespace std;
  5.  
  6. bool expcheckPair(string exp)
  7. {
  8. stack<char> s;
  9. char a;
  10. for (int i = 0; i < exp.length(); i++)
  11. {
  12. if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
  13. {
  14. s.push(exp[i]);
  15. }
  16. if (s.empty()) return false;
  17.  
  18. switch (exp[i])
  19. {
  20. case ')':
  21. a = s.top();
  22. s.pop();
  23. if (a == '{' || a == '[') return false; break;
  24.  
  25. case '}':
  26. a = s.top();
  27. s.pop();
  28. if (a == '(' || a == '[') return false; break;
  29.  
  30. case ']':
  31. a = s.top();
  32. s.pop();
  33. if (a == '(' || a == '{') return false; break;
  34. }
  35. }
  36. return (s.empty());
  37. }
  38.  
  39. int main() {
  40. string expression;
  41. cin >> expression;
  42. if (expcheckPair(expression)) { cout << "Pair " << endl; }
  43. else { cout << "Not pair" << endl; }
  44.  
  45. system("pause");
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement