-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostfix.c
More file actions
77 lines (72 loc) · 1.26 KB
/
Copy pathPostfix.c
File metadata and controls
77 lines (72 loc) · 1.26 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<stdlib.h>
#include<string.h>
#include<ctype.h>
char st[100];
int top=-1;
int is_empty()
{
return top==-1;
}
void push(char a)
{
st[++top]=a;
}
char pop()
{
return st[top--];
}
int precidence(char a)
{
switch (a)
{
case '^':
return 3;
case '/':
case '*':
return 2;
case '+':
case '-':
return 1;
}
}
char* inf_to_pos(char* arr)
{
int len=strlen(arr),siz=sizeof(char),a=((len+1)*siz),i=0,j=0;
char* post=(char* )malloc(a);
while (arr[i]!='\0')
{
if(isalnum(arr[i]))
{
post[j++]=arr[i++];
}
else
{
if(precidence(arr[i]>precidence(st[top])))
{
push(arr[i++]);
}
else
{
char x=pop();
post[j++]=x;
}
}
}
while (!is_empty())
{
post[j++]=pop();
}
post[j]='\0';
return post;
}
int main(void)
{
char exp[100];
char* e;
printf("Enter your Infix expression: ");
scanf("%s",exp);
e=exp;
char* result=inf_to_pos(e);
printf("Postfix Expression: %s",result);
}