-
Notifications
You must be signed in to change notification settings - Fork 0
/
stlparant.cpp
73 lines (70 loc) · 1.2 KB
/
stlparant.cpp
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>
#include<stack>
using namespace std;
class paranthesis
{
string s;
stack<char>st;
public:
void read();
bool isbalanced();
};
void paranthesis::read()
{
cout<<"enter the string "<<endl;
cin>>s;
for(int i=0;i<s.length();i++)
{
if(s[i]=='#'|| s[i]>='a' && s[i]<= 'z')
continue;
if(s[i]=='('|| s[i]=='<'||s[i]=='['||s[i]=='"')
{
st.push(s[i]);
}
else
if (st.empty()== true && s[i]==')'||st.empty()==true&& s[i]=='>')
{
st.push(s[i]);
}
else
if (st.top()=='('&& s[i]==')'||st.top()=='<'&& s[i]=='>'||st.top()=='['&&s[i]==']'||st.top()=='"'&&s[i]=='"')
{
st.pop();
}
else
{
cout<<"fail";
}
}
}
bool paranthesis::isbalanced()
{
if(st.empty())
{
return true;
}
else
return false;
}
int main()
{
int ch;
paranthesis a;
while(1)
{
cout<<"enter the choice"<<endl;
cout<<"1.enter the code"<<endl;
cout<<"2.check balanced or not"<<endl;
cin>>ch;
switch (ch) {
case /* vlue */1:a.read();
break;
case 2 : if(a.isbalanced())
{
cout<<("balanced");
}
else
cout<<("not balanced");
}
}
}