-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostfix_eval.c
More file actions
66 lines (63 loc) · 1.24 KB
/
postfix_eval.c
File metadata and controls
66 lines (63 loc) · 1.24 KB
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
// Give an input Postfix Expression, write the program to evaluate the given postfix expression using Stack Operations.
// Input : Postfix Expression and List of values of the variables
// Output : Result of the expression
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define max 50
int stack[max];
int top = -1;
void push(int x)
{
top++;
stack[top] = x;
}
void operation(char ch)
{
}
int pop()
{
int x = stack[top];
top--;
return x;
}
int main()
{
char st[max];
scanf("%s", st);
int x, c;
for (int i = 0; i < strlen(st); i++)
{
if (isalpha(st[i]))
{
scanf("%d", &x);
push(x);
}
else
{
int a = pop();
int b = pop();
switch (st[i])
{
case '+':
c = a + b;
push(c);
break;
case '-':
c = b - a;
push(c);
break;
case '*':
c = a * b;
push(c);
break;
case '/':
c = b / a;
push(c);
break;
}
}
}
printf("%d", stack[top]);
return 0;
}