Advertisement
Maruf_Hasan

Parenthesis balance

Jun 26th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Stack
  4. {
  5. public:
  6. int maxn=10;
  7. char ara[10];
  8. int i=-1;
  9. char push(char c)
  10. {
  11. if(i<=maxn)
  12. {
  13. i++;
  14. ara[i]=c;
  15. }
  16. }
  17. void pop()
  18. {
  19. if(i>=0)
  20. {
  21. i--;
  22. }
  23. }
  24. int top()
  25. {
  26. return ara[i];
  27. }
  28. int IsEmpty()
  29. {
  30. if(i==-1)
  31. {
  32. return true;
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. }
  39. };
  40. int main()
  41. {
  42. Stack s;
  43. char str[30];
  44. gets(str);
  45. for(int j=0;j<strlen(str);j++)
  46. {
  47. if(str[j]=='('||str[j]=='{'||str[j]=='[')
  48. {
  49. s.push(str[j]);
  50. }
  51. else if(str[j]==')')
  52. {
  53. if(s.IsEmpty())
  54. {
  55. cout<<"Invalid"<<endl;
  56. return 0;
  57. }
  58. else if(s.top()=='(')
  59. {
  60. s.pop();
  61. }
  62. }
  63. else if(str[j]=='}')
  64. {
  65. if(s.IsEmpty())
  66. {
  67. cout<<"Invalid"<<endl;
  68. return 0;
  69. }
  70. else if(s.top()=='{')
  71. {
  72. s.pop();
  73. }
  74. }
  75. else if(str[j]==']')
  76. {
  77. if(s.IsEmpty())
  78. {
  79. cout<<"Invalid"<<endl;
  80. return 0;
  81. }
  82. else if(s.top()=='[')
  83. {
  84. s.pop();
  85. }
  86. }
  87. }
  88. if(s.IsEmpty())
  89. {
  90. cout<<"Valid"<<endl;
  91. }
  92. else
  93. {
  94. cout<<"Invalid"<<endl;
  95. }
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement