-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathparenthesis.cpp
More file actions
62 lines (60 loc) · 866 Bytes
/
parenthesis.cpp
File metadata and controls
62 lines (60 loc) · 866 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
#include<iostream>
using namespace std;
#include<string.h>
#define n 20
char push(char);
char pop();
int top=-1;
int stack[20];
main()
{
char s[20],temp;
cout<<"enter expression"<<endl;
cin>>s;
int x=1,i;
for(i=0;i<strlen(s);i++)
{
if(s[i]=='('||s[i]=='{'||s[i]=='[')
push(s[i]);
if(s[i]==')'||s[i]=='}'||s[i]==']')
{
if(top==-1)
x=0;
else
{
temp=pop();
if(s[i]==')'&&(temp=='{'||temp=='['))
x=0;
if(s[i]=='}'&&(temp=='('||temp=='['))
x=0;
if(s[i]==']'&&(temp=='{'||temp=='('))
x=0;
}
}
}
if(top>=0)
x=0;
if(x==0)
cout<<"invalid\n";
else
cout<<"valid\n";
}
char push(char a)
{
if(top==n-1)
cout<<"stack overflow"<<endl;
else
{
top++;
stack[top]=a;
}
}
char pop()
{
if(top==-1)
cout<<"stack underflow"<<endl;
else
{
return(stack[top--]);
}
}