1+ /* This program converts infix expression to postfix expression.
2+ * This program assume that there are Five operators: (*, /, +, -,^)
3+ in infix expression and operands can be of single-digit only.
4+ * This program will not work for fractional numbers.
5+ * Further this program does not check whether infix expression is
6+ valid or not in terms of number of operators and operands.*/
7+
8+ #include <stdio.h>
9+ #include <stdlib.h> /* for exit() */
10+ #include <ctype.h> /* for isdigit(char ) */
11+ #include <string.h>
12+
13+ #define SIZE 100
14+
15+
16+ /* declared here as global variable because stack[]
17+ * is used by more than one fucntions */
18+ char stack [SIZE ];
19+ int top = -1 ;
20+
21+ /* define push operation */
22+
23+ void push (char item )
24+ {
25+ if (top >= SIZE - 1 )
26+ {
27+ printf ("\nStack Overflow." );
28+ }
29+ else
30+ {
31+ top = top + 1 ;
32+ stack [top ] = item ;
33+ }
34+ }
35+
36+ /* define pop operation */
37+ char pop ()
38+ {
39+ char item ;
40+
41+ if (top < 0 )
42+ {
43+ printf ("Stack under flow: invalid infix expression" );
44+ getchar ();
45+ /* underflow may occur for invalid expression */
46+ /* where ( and ) are not matched */
47+ exit (1 );
48+ }
49+ else
50+ {
51+ item = stack [top ];
52+ top = top - 1 ;
53+ return (item );
54+ }
55+ }
56+
57+ /* define function that is used to determine whether any symbol is operator or not
58+ (that is symbol is operand)
59+ * this fucntion returns 1 if symbol is opreator else return 0 */
60+
61+ int is_operator (char symbol )
62+ {
63+ if (symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol == '-' )
64+ {
65+ return 1 ;
66+ }
67+ else
68+ {
69+ return 0 ;
70+ }
71+ }
72+
73+ /* define fucntion that is used to assign precendence to operator.
74+ * Here ^ denotes exponent operator.
75+ * In this fucntion we assume that higher integer value
76+ * means higher precendence */
77+
78+ int precedence (char symbol )
79+ {
80+ if (symbol == '^' )/* exponent operator, highest precedence*/
81+ {
82+ return (3 );
83+ }
84+ else if (symbol == '*' || symbol == '/' )
85+ {
86+ return (2 );
87+ }
88+ else if (symbol == '+' || symbol == '-' ) /* lowest precedence */
89+ {
90+ return (1 );
91+ }
92+ else
93+ {
94+ return (0 );
95+ }
96+ }
97+
98+ void InfixToPostfix (char infix_exp [], char postfix_exp [])
99+ {
100+ int i , j ;
101+ char item ;
102+ char x ;
103+
104+ push ('(' ); /* push '(' onto stack */
105+ strcat (infix_exp ,")" ); /* add ')' to infix expression */
106+
107+ i = 0 ;
108+ j = 0 ;
109+ item = infix_exp [i ]; /* initialize before loop*/
110+
111+ while (item != '\0' ) /* run loop till end of infix expression */
112+ {
113+ if (item == '(' )
114+ {
115+ push (item );
116+ }
117+ else if ( isdigit (item ) || isalpha (item ))
118+ {
119+ postfix_exp [j ] = item ; /* add operand symbol to postfix expr */
120+ j ++ ;
121+ }
122+ else if (is_operator (item ) == 1 ) /* means symbol is operator */
123+ {
124+ x = pop ();
125+ while (is_operator (x ) == 1 && precedence (x )>= precedence (item ))
126+ {
127+ postfix_exp [j ] = x ; /* so pop all higher precendence operator and */
128+ j ++ ;
129+ x = pop (); /* add them to postfix expresion */
130+ }
131+ push (x );
132+ /* because just above while loop will terminate we have
133+ oppped one extra item
134+ for which condition fails and loop terminates, so that one*/
135+
136+ push (item ); /* push current oprerator symbol onto stack */
137+ }
138+ else if (item == ')' ) /* if current symbol is ')' then */
139+ {
140+ x = pop (); /* pop and keep popping until */
141+ while (x != '(' ) /* '(' encounterd */
142+ {
143+ postfix_exp [j ] = x ;
144+ j ++ ;
145+ x = pop ();
146+ }
147+ }
148+ else
149+ { /* if current symbol is neither operand not '(' nor ')' and nor
150+ operator */
151+ printf ("\nInvalid infix Expression.\n" ); /* the it is illegeal symbol */
152+ getchar ();
153+ exit (1 );
154+ }
155+ i ++ ;
156+
157+
158+ item = infix_exp [i ]; /* go to next symbol of infix expression */
159+ } /* while loop ends here */
160+ if (top > 0 )
161+ {
162+ printf ("\nInvalid infix Expression.\n" ); /* the it is illegeal symbol */
163+ getchar ();
164+ exit (1 );
165+ }
166+ if (top > 0 )
167+ {
168+ printf ("\nInvalid infix Expression.\n" ); /* the it is illegeal symbol */
169+ getchar ();
170+ exit (1 );
171+ }
172+
173+
174+ postfix_exp [j ] = '\0' ; /* add sentinel else puts() fucntion */
175+ /* will print entire postfix[] array upto SIZE */
176+
177+ }
178+
179+ /* main function begins */
180+ int main ()
181+ {
182+ char infix [SIZE ], postfix [SIZE ]; /* declare infix string and postfix string */
183+
184+ /* why we asked the user to enter infix expression
185+ * in parentheses ( )
186+ * What changes are required in porgram to
187+ * get rid of this restriction since it is not
188+ * in algorithm
189+ * */
190+ printf ("ASSUMPTION: The infix expression contains single letter variables and single digit constants only.\n" );
191+ printf ("\nEnter Infix expression : " );
192+ gets (infix );
193+
194+ InfixToPostfix (infix ,postfix ); /* call to convert */
195+ printf ("Postfix Expression: " );
196+ puts (postfix ); /* print postfix expression */
197+
198+ return 0 ;
199+ }
0 commit comments