-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (54 loc) · 836 Bytes
/
main.cpp
File metadata and controls
66 lines (54 loc) · 836 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
#include<iostream>
#include<stack>
#include<string>
using namespace std;
void paran(string ele);
int main()
{
string elems;
char input = 0;
while(input != 'q')
{
cout<<" Enter the Parans to check if it is balanced or not, Press q to quit :"<<endl;
cin>>elems;
paran(elems);
}
return 0;
}
void paran(string ele)
{
stack<char>pars;
int top = -1;
if(ele.at(0) == ')')
{
top--;
}
for(int i = 0; i< ele.size(); i++)
{
if(ele[i] == '(')
{
top++;
pars.push(ele.at(i));
//cout<<"it worked"<<endl;
}
else if(ele[i] == ')' && pars.top() == '(')
{
top--;
pars.pop();
//cout<<"it worked2"<<endl;
}
else if(ele[i] == '(')
{
top--;
//cout<<"it worked3"<<endl;
}
}
if(top == -1)
{
cout<<ele<<" is balanced "<<endl;
}
else
{
cout<<ele << " is not balanced "<<endl;
}
}