-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp2.c
72 lines (71 loc) · 1.38 KB
/
Exp2.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
67
68
69
70
71
72
#include<stdio.h>
#define MAX 10
#include<string.h>
#include<ctype.h>
int top=-1, j=0, i;
char stack[MAX];
char infix[MAX], postfix[MAX];
void push(char c)
{
if(top==MAX-1)
printf("\nStack overflow");
else
stack[++top] = c;
}
char pop()
{
if(top==-1)
return '>';
else
return (stack[top--]);
}
int getPriority(char c1)
{
if(c1 == "/" || c1 =='%' || c1 =="*" )
return 2;
else if(c1=='+' || c1=='-')
return 1;
else if(c1=='(' )
return 0;
return 9;
}
void infixToPostfix()
{
char temp;
i=0;
while(infix[i]!='\0')
{
char ch = infix[i];
if(ch=='(')
push(ch);
else if(isalnum(ch))
postfix[j++] = ch;
else if(ch==')')
{
while(stack[top]!=')' && top!=-1)
{
postfix[j++] = pop();
}
temp = pop();
}
else{
while(getPriority(ch)<= getPriority(stack[top]))
{
postfix[j++] = pop();
}
push(ch);
}
i++;
}
}
void main()
{
int t=0;
printf("Enter a infix expression: ");
scanf("%s", infix);
stack[top++] = '(';
strcat(infix, ")");
infixToPostfix();
for(t=0; t<j; t++)
printf("%c", postfix[t]);
}