forked from Dipak3007/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfix_to_Postfix.c
More file actions
143 lines (135 loc) · 3.19 KB
/
Infix_to_Postfix.c
File metadata and controls
143 lines (135 loc) · 3.19 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
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
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 100
char stack[max];
int top = -1;
void push(char);
char pop(char stack[], char item);
int precedence(char symbol);
void inftopfx(char Q[], char P[]);
int main()
{
char Q[max], P[max]; // declare the infix string and postfix string
int choice = 1;
while (choice == 1)
{
printf("\n*Infix to postfix Expression*\n\n");
printf("\nEnter Infix expression : ");
scanf("%s", &Q);
inftopfx(Q, P); // call to convert infix to postfix
printf("Do you want to convert more press(1)\n");
scanf("%d", &choice);
}
return 0;
}
void push(char item)
{
if (top >= max - 1)
{
printf("overflow\n");
}
else
{
top = top + 1;
stack[top] = item;
}
}
char pop(char stack[], char item)
{
if (top < 0)
{
printf("underflow\n");
exit(0);
}
else
{
item = stack[top];
top = top - 1;
return (item);
}
}
int operator(char symbol)
{
if (symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol == '-')
{
return 1;
}
else
{
return 0;
}
}
int precedence(char symbol)
{
if (symbol == '^')
{
return (3);
}
else if (symbol == '*' || symbol == '/')
{
return (2);
}
else if (symbol == '+' || symbol == '-') // lowest precedence
{
return (1);
}
else
{
return (0);
}
}
void inftopfx(char Q[], char P[])
{
int i = 0, j = 0,last;
char data, n;
push('('); // push this '(' onto stack
last =strlen(Q);
Q[last] = ')'; // add ')' to end of infix expression
data = Q[i];
Q[last+1] = '\0';
printf("New Expression of Q is %s\n\n", Q);
while (data != '\0')
{
if ((data >= '0' & data <= '9') || (data >= 'a' & data <= 'z') || (data >= 'A' & data <= 'Z'))
{
P[j] = data; /* add the operand symbol to postfix expression */
j++;
}
else if (data == '(')
{
push(data); // if '(' then add it to stack
}
else if (operator(data) == 1) /* means symbol is operator */
{
n = pop(stack, data);
while (operator(n) == 1 && precedence(n) >= precedence(data))
{
P[j] = n;
j++;
n = pop(stack, data); /* add them to postfix expresion */
}
push(n);
push(data); /* pushes the current oprerator symbol onto stack */
}
else if (data == ')')
{
n = pop(stack, data);
while (n != '(') /* '(' encounterd then stop*/
{
P[j] = n;
j++;
n = pop(stack, data);
}
}
else
{
printf("\nInfix Expression is invalid\n");
exit(1);
}
i++;
data = Q[i];
}
printf("Postfix Expression is \n ");
printf("%s\n", P); /* print postfix expression */
}