-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path18 Stack Balancing Small Brackets .cpp
More file actions
87 lines (72 loc) · 1.6 KB
/
18 Stack Balancing Small Brackets .cpp
File metadata and controls
87 lines (72 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*Write a program to create a stack for characters with push, pop functions having maximum capacity of 10 then you have to create a function to check whether given expression of '(' and ')' characters is balanced or not.
Input Format
Input line can be any expression having '(' and ')' characters.
Constraints
1≤n≤10
Output Format
If the parenthesis is balanced then it will display "Balanced" otherwise it will display "Not Balanced". If expression contains more than 10 symbols then display message of "Stack Full".
Sample Input 0
(())()
Sample Output 0
Balanced
Explanation 0
Every '(' have corresponding ')' at appropriate place
Sample Input 1
(()))
Sample Output 1
Not Balanced
Explanation 1
There are only two starting '(' and three ')' so it is not balanced
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
char stack[10];
int top=-1;
void push(char c)
{
if(top==9)
{
cout<<"Stack Full";
exit(0);
}
else
{
top++;
stack[top] = c;
}
}
void pop()
{
if(top!=-1)
{
top--;
}
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
string s;
cin>>s;
if(s.length()>10)
{
cout<<"Stack Full";
return 0;
}
for(int i=0;i<s.length();i++)
{
if(stack[top]==-1)
push(s[i]);
else if(stack[top]=='(' && s[i]==')')
pop();
else
push(s[i]);
}
if(top==-1)
cout<<"Balanced";
else
cout<<"Not Balanced";
return 0;
}