-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPOSTFIX.C
More file actions
46 lines (43 loc) · 708 Bytes
/
Copy pathPOSTFIX.C
File metadata and controls
46 lines (43 loc) · 708 Bytes
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
#include<stdio.h>
#include<conio.h>
int evaluate(char*);
void main()
{
char exp[15];
clrscr();
printf("Enter an expression in postfix form : ");
gets(exp);
printf("\nResult = %d",evaluate(exp));
getch();
}
int evaluate(char *str)
{
int stack[15],top=0,op1,op2,i;
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='0' && str[i]<='9')
stack[top++]=str[i]-'0';
else
{
op2=stack[--top];
op1=stack[--top];
switch(str[i])
{
case '+':
stack[top++]=op1+op2;
break;
case '-':
stack[top++]=op1-op2;
break;
case '*':
stack[top++]=op1*op2;
break;
case '/':
stack[top++]=op1/op2;
break;
default:top+=2;
}
}
}
return stack[--top];
}