-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoststack.c
More file actions
71 lines (69 loc) · 1.07 KB
/
Copy pathpoststack.c
File metadata and controls
71 lines (69 loc) · 1.07 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
67
68
69
70
71
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<string.h>
struct node
{
int data;
struct node *next;
};
struct node *top = NULL;
void push(int n)
{
struct node *new;
new = (struct node *)malloc(sizeof(struct node));
new->data = n;
new->next = top;
top = new;
}
int pop()
{
int data;
struct node *temp;
temp = top;
data = temp->data;
top = top->next;
free(temp);
return data;
}
int main()
{
char postfix[100],e;
int i=0,a,b,r;
printf("Enter the postfix expression: ");
fgets(postfix,100,stdin);
for(i=0;i<strlen(postfix) - 1; i++)
{
e = postfix[i];
if(isdigit(e))
{
push(e - '0');
}
else
{
a = pop();
b = pop();
switch(e)
{
case '+':
r = a+b;
break;
case '-':
r = b-a;
break;
case '*':
r = a*b;
break;
case '/':
r = b/a;
break;
case '^':
r = pow(b,a);
break;
}
push(r);
}
}
printf("\nResult=%d\n\n",r);
}