-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParenthesis Checker - cpp
More file actions
73 lines (73 loc) · 925 Bytes
/
Copy pathParenthesis Checker - cpp
File metadata and controls
73 lines (73 loc) · 925 Bytes
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
#include<iostream>
using namespace std;
#include<string.h>
#include<ctype.h>
class stack{
private:
int top;
char s[1000];
public:
stack()
{
top=-1;
}
char pop();
void push(char);
int isempty();
};
int stack::isempty()
{
if(top==-1)
{
return 1;
}
else return 0;
}
void stack::push(char x)
{
if(top==-1)
top=0;
else
top+=1;
s[top]=x;
}
char stack::pop()
{
char t=s[top];
top--;
return t;
}
main()
{
int test;
cin>>test;
cin>>ws;
while(test--)
{
int n,flag=0;
stack s;
string a;
getline(cin,a);
for(int i=0;a[i]!='\0';i++)
{
if(a[i]=='('||a[i]=='{'||a[i]=='[')
{
s.push(a[i]);
}
else if(a[i]==')'||a[i]==']'||a[i]=='}')
{
char m=s.pop();
if((m=='(' && a[i]==')') || (m=='{' && a[i]=='}') || (m=='[' && a[i]==']'))
continue;
else
{
flag++;
break;
}
}
}
if(s.isempty()&&flag==0)
cout<<"balanced\n";
else
cout<<"not balanced\n";
}}