-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix_to_postfix.c
111 lines (108 loc) · 1.9 KB
/
infix_to_postfix.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char data;
struct node *next;
}*top=NULL;
void push(char x)
{
struct node *t=(struct node*)malloc(sizeof(struct node));
if(t==NULL)
printf("Stack overflow\n");
else
{
t->data=x;
t->next=top;
top=t;
}
}
char pop()
{
struct node *p;
char x=-1;
if(top==NULL)
printf("Underflow\n");
else
{
p=top;
top=top->next;
x=p->data;
free(p);
}
return x;
}
char stacktop()
{
if(top)
return top->data;
else
return -1;
}
int isEmpty()
{
if(top==NULL)
return 1;
else
return 0;
}
int isoperand(char ch)
{
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='z'))
return 1;
else
return 0;
}
int pre(char c)
{
if(c=='+'||c=='-')
return 1;
else if(c=='*'||c=='/')
return 2;
else if(c=='^')
return 3;
else
return 0;
}
void in_to_post(char *c)
{
int i=0,k=0;
char postfix[strlen(c)+1];
for(i=0;c[i]!='\0';i++)
{
if(isoperand(c[i]))
postfix[k++]=c[i];
else if(c[i]=='(')
push(c[i]);
else if(c[i]==')')
{
while(!isEmpty()&&stacktop()!='(')
postfix[k++]=pop();
if(stacktop()!='(')//!isEmpty()
printf("Invalid expression\n");
pop();
}
else
{
while(pre(c[i])<=pre(stacktop()))
postfix[k++]=pop();
push(c[i]);
}
}
while(top)
{
postfix[k++]=pop();
}
postfix[k]='\0';
printf("%s",postfix);
}
void main()
{
char expr[20];
//printf("Enter the size of the expression\n");
//scanf("%d",&n);
printf("Enter the expression\n");
scanf("%s",expr);
in_to_post(expr);
}