-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfix_Postfix_Perfect.c
More file actions
66 lines (59 loc) · 1.33 KB
/
Infix_Postfix_Perfect.c
File metadata and controls
66 lines (59 loc) · 1.33 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
#include <stdio.h>
#include <ctype.h>
char stack[50];
int top = -1;
void push(char ch)
{
stack[++top]=ch;
}
char pop()
{
return(stack[top--]);
}
int prioritity(char ch)
{
switch (ch) {
case '#': return 0;
case '(':return 1;
case '+':
case '-':return 2;
case '*':
case '/':return 3;
case '^':return 4;
}
}
void main()
{
char infix[50],postfix[50],ch;
int i=0,j=0;
printf("\nEnter the infix expression Below:\n" );
scanf("%s",infix);
push('#');
while ((ch=infix[i++])!='\0') {
if(ch =='(')
{
push(ch);
}
else if (isalnum(ch)) {
postfix[j++]=ch;
}
else if (ch == ')') {
while(stack[top]!='('){
postfix[j++] = pop();
}
pop();//Popping the '(' i.e is the last remaining bracket from the stack! :)
}
else
{
while(prioritity(stack[top])>prioritity(ch))
{
postfix[j++]=pop();
}push(ch);//Pushing the remaining operators of lesser prioritity into the stack! :)
}
}
while (stack[top]!='#') {
postfix[j++]=pop();//Adding the he remaining operators of lesser prioritity into the postfix expression! :)
}
postfix[j]='\0';
printf("\nRequired postfix is : %s",postfix );
}