forked from casafurix/c-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfixToPostfix.c
More file actions
77 lines (77 loc) · 1.7 KB
/
infixToPostfix.c
File metadata and controls
77 lines (77 loc) · 1.7 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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 20
struct stack{
int top;
char arr[MAXSIZE];
};
void Push(struct stack *p,char ch)
{
if(p->top==MAXSIZE-1)
printf("Stack is full");
else
p->arr[++(p->top)]=ch;
}
char Pop(struct stack *p)
{
if (p->top==-1)
printf("Stack is empty");
else
{
return p->arr[(p->top)--];
}
}
int Precedence(char ch)
{
if(ch=='^' || ch=='$' )
return 3;
else if(ch=='*' || ch=='/' || ch=='%')
return 2;
else if(ch=='+' || ch=='-')
return 1;
else if(ch=='(')
return 0;
}
void Create(struct stack *p)
{
p->top=-1;
}
void main()
{
char Infix[MAXSIZE],Postfix[MAXSIZE];
int i=0,j=0,l;
struct stack s;
Create(&s);
printf("Enter an infix expression:");
scanf("%[^\n]s",Infix);
l=strlen(Infix);
Infix[l]=')';
Infix[l+1]='\0';
Push(&s,'(');
while(Infix[i]!='\0')
{
if((Infix[i]>='A' && Infix[i]<='Z') || (Infix[i]>='a' && Infix[i]<='z'))
{
Postfix[j++]=Infix[i];
}
else if(Infix[i]=='(')
Push(&s,'(');
else if(Infix[i]==')')
{
while(s.arr[s.top]!='(')
Postfix[j++]=Pop(&s);
Pop(&s);
}
else
{
while(Precedence(s.arr[s.top])>=Precedence(Infix[i]))
Postfix[j++]=Pop(&s);
if(Precedence(s.arr[s.top])<Precedence(Infix[i]))
Push(&s,Infix[i]);
}
i++;
}
Postfix[j]='\0';
printf("\nPostfix expression=%s",Postfix);
}