-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevelution_stack_.c
More file actions
88 lines (87 loc) · 1.47 KB
/
evelution_stack_.c
File metadata and controls
88 lines (87 loc) · 1.47 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next;
};
struct node *top = NULL;
int isoperand(char x);
void push(int x);
int pop();
int eval(char * postfix);
int main ()
{
char *postfix="382/+2+25/6*-4+";
printf("result is %d",eval(postfix));
return 0;
}
int isoperand(char x)
{
if (x == '+' || x == '-' || x == '*' || x == '/' ||
x == '^' || x == '(' || x == ')')
{
return 0;
}
else
{
return 1;
}
}
void push(int x)
{
struct node *t;
t = (struct node *)malloc(sizeof(struct node));
if (t == NULL)
{
printf("stake is full\n");
}
else
{
t->data = x;
t->next = top;
top = t;
}
}
int pop()
{
struct node *t;
int x = -1;
if (top == NULL)
{
printf("stake is empty\n");
}
else
{
t = top;
top = top->next;
x = t->data;
free(t);
}
return x;
}
int eval(char * postfix)
{
int i ,x1,x2,r;
for (i=0; postfix[i]!='\0'; i++)
{
if(isoperand(postfix[i]))
{
push(postfix[i]-'0');
}
else
{
x2=pop(); x1=pop();
switch(postfix[i])
{
case '+':r=x1+x2; break;
case '-':r=x1-x2; break;
case '*':r=x1*x2; break;
case '/':r=x1/x2; break;
}
push(r);
}
}
return top->data;
}