-
Notifications
You must be signed in to change notification settings - Fork 1
/
postfix notation from infix notation.c
136 lines (125 loc) · 1.71 KB
/
postfix notation from infix notation.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//Implement C program for conversion of the postfix notation from infix notation of the numerical statement 6+5/4-3*4*3-12 using stack.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <string.h>
char infix_string[30], postfix_str[30];
int top;
int stack[30];
int pop();
int order(char exp);
int emptystr();
void infix_to_postfix();
int check_space(char exp);
void push(long int exp);
int main()
{
int count, length;
char temp;
top = -1;
printf("\nENTER THE INFIX EXPRESSION : ");
scanf("%s", infix_string);
infix_to_postfix();
printf("\nTHE POSTFIX EXPRESSION : %s\n", postfix_str);
return 0;
}
void infix_to_postfix()
{
unsigned int count, temp = 0;
char next;
char exp;
for(count = 0; count < strlen(infix_string); count++)
{
exp = infix_string[count];
if(!check_space(exp))
{
switch(exp)
{
case '(': push(exp);
break;
case ')':
while((next = pop()) != '(')
{
postfix_str[temp++] = next;
}
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
while(!emptystr() && order(stack[top]) >= order(exp))
postfix_str[temp++] = pop();
push(exp);
break;
default:
postfix_str[temp++] = exp;
}
}
}
while(!emptystr())
{
postfix_str[temp++] = pop();
}
postfix_str[temp] = '\0';
}
int order(char exp)
{
switch(exp)
{
case '(': return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
default:
return 0;
}
}
int check_space(char exp)
{
if(exp == '\t' || exp == ' ' )
{
return 1;
}
else
{
return 0;
}
}
void push(long int exp)
{
if(top > 30)
{
printf("\n Stack Overflow! \n");
exit(1);
}
top = top + 1;
stack[top] = exp;
}
int emptystr()
{
if(top == -1)
{
return 1;
}
else
{
return 0;
}
}
int pop()
{
if(emptystr())
{
printf("Empty Stack!\n");
exit(1);
}
return(stack[top--]);
}