-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix_to_postfix_conversion.c
81 lines (81 loc) · 1.75 KB
/
infix_to_postfix_conversion.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
73
74
75
76
77
78
79
80
81
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
bool issalnum(char a)
{
if((a>47 && a<58)||(a>64 && a<91)||(a>96 && a<123))
return true;
return false;
}
int pre(char a)
{
if(a=='^')
return 3;
else if(a=='*' || a=='/')
return 2;
else if(a=='+' || a=='-')
return 1;
else
return 0;
}
int main()
{
char arr[100],*stack,*postfix;
stack=(char*)malloc(100*sizeof(char));
postfix=(char*)malloc(100*sizeof(char));
printf("Enter the infix expression to be converted : ");
scanf("%[^\n]",arr);
int i=0,ts=-1,tp=-1;
while(arr[i]!='\0')
{
if(issalnum(arr[i]))
{
tp++;
*(postfix+tp)=arr[i];
}
else if(pre(arr[i])!=0)
{
if(pre(arr[i])<=pre(*(stack+ts)))
{
while(pre(arr[i])<=pre(*(stack+ts)))
{
tp++;
*(postfix+tp)=*(stack+ts);
ts--;
}
}
else
{
ts++;
*(stack+ts)=arr[i];
}
}
else if(arr[i]=='(')
{
ts++;
*(stack+ts)=arr[i];
}
else if(arr[i]==')')
{
while(*(stack+ts)!='(')
{
tp++;
*(postfix+tp)=*(stack+ts);
ts--;
}
ts--;
}
i++;
}
while(ts!=-1)
{
tp++;
*(postfix+tp)=*(stack+ts);
ts--;
}
*(postfix + tp +1)='\0';
printf("The postfix expression is : %s",postfix);
free(stack);
free(postfix);
return 0;
}