File tree 1 file changed +144
-0
lines changed
1 file changed +144
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < stdio.h>
2
+ #include < stdlib.h>
3
+ #include < string>
4
+ struct node
5
+ {
6
+ int data;
7
+ struct node * next;
8
+ }*top = NULL ;
9
+ void display ()
10
+ {
11
+ struct node * p;
12
+ p = top;
13
+ while (p != NULL )
14
+ {
15
+ printf (" %d " , p->data );
16
+ p = p->next ;
17
+ }
18
+ printf (" \n " );
19
+ }
20
+ void push ( int x)
21
+ {
22
+ struct node * t;
23
+ t = (struct node *)malloc (sizeof (struct node ));
24
+ if (t == NULL )
25
+ printf (" stack is full\n " );
26
+ else
27
+ {
28
+ t->data = x;
29
+ t->next = top;
30
+ top = t;
31
+ }
32
+ }
33
+ int pop ()
34
+ {
35
+ int x = -1 ;
36
+ struct node * t;
37
+ if (top == NULL )
38
+ printf (" stack is empty\n " );
39
+ else
40
+ {
41
+ t = top;
42
+ top = top ->next ;
43
+ x = t->data ;
44
+ free (t);
45
+ }
46
+ return x;
47
+ }
48
+ int isbalanced (const char * exp)
49
+ {
50
+ for (int i = 0 ; exp [i] != ' \0 ' ; i++)
51
+ {
52
+ if (exp [i] == ' (' )
53
+ push (exp [i]);
54
+ else if (exp [i] == ' )' )
55
+ {
56
+ if (top == NULL )
57
+ return 0 ;
58
+ pop ();
59
+ }
60
+ }
61
+ if (top == NULL )
62
+ return 1 ;
63
+ else
64
+ return 0 ;
65
+ }
66
+ int pre (char x)
67
+ {
68
+ if (x == ' +' || x == ' -' )
69
+ return 1 ;
70
+ else if (x==' *' ||x== ' /' )
71
+ return 2 ;
72
+ return 0 ;
73
+ }
74
+ int isoperand (char x)
75
+ {
76
+ if (x == ' +' || x == ' -' || x == ' *' || x == ' /' )
77
+ return 0 ;
78
+ else
79
+ return 1 ;
80
+ }
81
+ char * intopost (const char * infix)
82
+ {
83
+ int i = 0 , j = 0 ;
84
+ char * postfix;
85
+ int len = strlen (infix);
86
+ postfix = (char *)malloc ((len + 1 ) * sizeof (char ));
87
+ while (infix[i] != ' \0 ' )
88
+ {
89
+ if (isoperand (infix[i]))
90
+ postfix[j++] = infix[i++];
91
+ else
92
+ {
93
+ if (pre (infix[i]) > pre (top->data ))
94
+ push (infix[i++]);
95
+ else
96
+ postfix[j++] = pop ();
97
+ }
98
+ }
99
+ while (top != NULL )
100
+ postfix[j++] = pop ();
101
+ postfix[j] = ' \0 ' ;
102
+ return postfix;
103
+ }
104
+ int eval (const char * postfix)
105
+ {
106
+ int x1, x2, r;
107
+ for (int i = 0 ; postfix[i] != ' \0 ' ; i++)
108
+ {
109
+ if (isoperand (postfix[i]))
110
+ push (postfix[i]-' 0' );
111
+ else
112
+ {
113
+ x2 = pop ();
114
+ x1 = pop ();
115
+ switch (postfix[i])
116
+ {
117
+ case ' +' :
118
+ r = x1 + x2;
119
+ break ;
120
+ case ' -' :
121
+ r = x1 -x2;
122
+ break ;
123
+ case ' *' :
124
+ r = x1 *x2;
125
+ break ;
126
+ case ' /' :
127
+ r = x1 /x2;
128
+ break ;
129
+ }
130
+ push (r);
131
+ }
132
+ }
133
+ return top->data ;
134
+ }
135
+ int main ()
136
+ {
137
+ /* const char* infix = "a+b*c";
138
+ push('#');
139
+ char* postfix = intopost(infix);
140
+ printf("%s", postfix);*/
141
+ const char * postfix = " 234*+82/-" ;
142
+ printf (" %d" ,eval (postfix));
143
+ return 0 ;
144
+ }
You can’t perform that action at this time.
0 commit comments