File tree Expand file tree Collapse file tree 1 file changed +13
-24
lines changed Expand file tree Collapse file tree 1 file changed +13
-24
lines changed Original file line number Diff line number Diff line change 1
1
#include < iostream>
2
2
#include < stdlib.h>
3
- #include < string.h >
3
+ #include < string>
4
4
#include < stdio.h>
5
5
using namespace std ;
6
6
7
- char stack[100 ];
7
+ const int MAX = 100 ;
8
+
9
+ // -------------- stack --------------
10
+
11
+ char stack[MAX];
8
12
int top=0 ;
9
13
10
14
void push (char ch)
@@ -17,41 +21,26 @@ char pop()
17
21
return stack[--top];
18
22
}
19
23
20
- bool check (char x, char y)
21
- {
22
- if ((x==' (' && y==' )' ) || (x==' {' && y==' }' ) || (x==' [' && y==' ]' ) || (x==' <' && y==' >' ))
23
- {
24
- return true ;
25
- }
26
- else
27
- {
28
- return false ;
29
- }
30
- }
31
-
32
-
24
+ // -------------- end stack -----------
33
25
34
26
int main ()
35
27
{
36
- char exp[ 100 ] ;
28
+ string exp;
37
29
cout<<" Enter The Expression : " ;
38
- gets ( exp) ;
39
- for (int i = 0 ; i < strlen ( exp); i++)
30
+ cin >> exp;
31
+ for (int i = 0 ; i < exp. length ( ); i++)
40
32
{
41
33
if (exp[i]==' (' || exp[i]==' {' || exp[i]==' [' || exp[i]==' <' )
42
34
{
43
35
push (exp[i]);
44
36
}
45
37
else if (exp[i]==' )' || exp[i]==' }' || exp[i]==' ]' || exp[i]==' >' )
46
38
{
47
- if (!check (pop (), exp[i]))
48
- {
49
- cout<<" \n Wrong Expression" ;
50
- exit (0 );
51
- }
39
+ pop ();
52
40
}
53
41
}
54
42
43
+ // makes sure the stack is empty after processsing (above)
55
44
if (top==0 )
56
45
{
57
46
cout<<" Correct Expression" ;
@@ -60,6 +49,6 @@ int main()
60
49
{
61
50
cout<<" \n Wrong Expression" ;
62
51
}
63
-
52
+
64
53
return 0 ;
65
54
}
You can’t perform that action at this time.
0 commit comments