-
Notifications
You must be signed in to change notification settings - Fork 0
/
infix to prefix 1.c
66 lines (59 loc) · 1.21 KB
/
infix to prefix 1.c
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
// INFIX TO PREFIX
//Prefix Evaluation
#include<stdio.h>
#include<string.h>
#include"mystack.h"
int power(int a,int n)
{ int p;
if(n==0)
{
return 1;
}
else
{ p=power(a,n/2);
if (n%2==0)
return p*p;
else
return p*p*a;
}
}
result(int a,int b,char symb)
{
switch(symb)
{
case'+':return a+b;
case'-':return a-b;
case'*':return a*b;
case'/':return a/b;
case'%':return a%b;
case'^':return power(a,b);
}
}
int postfixevaluation(char postop[])
{ int i,symb,a,b,value,ans,l=strlen(postop);;
struct stack S;
intialization(&S);
for(i=l-1;i>=0;i--)
{
symb=postop[i];
if(symb>='0'&&symb<='9')
push(&S,symb-'0');
else
{
a=pop(&S);
b =pop(&S);
value=result(a,b,symb);
push(&S,value);
}
}
ans=stacktop(&S);
return ans;
}
int main()
{ int ans,z;
char postop[20];
printf("Enter the expressions\n");
scanf("%s",postop);
z=postfixevaluation(postop);
printf("%d",z);
}