Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
infix_to_postfix.c File Reference

Infix to Postfix Expression Conversion More...

#include <stdio.h>
Include dependency graph for infix_to_postfix.c:

Data Structures

struct  Stack
 for printf() and scanf() More...
 

Functions

void push (struct Stack *p, char x)
 push function More...
 
char pop (struct Stack *p)
 pop function More...
 
int isOprnd (char ch)
 isOprnd function More...
 
int isEmpty (struct Stack s)
 isEmpty function More...
 
int getPrecedence (char op1, char op2)
 getPrecedence function returns the precedence after comparing two operators passed as parameter. More...
 
void convert (char infix[], char postfix[])
 convert function More...
 
int main ()
 main function More...
 

Detailed Description

Infix to Postfix Expression Conversion

Convert Infixed expressions to Postfix expression.

Author
Harsh Karande

Function Documentation

◆ convert()

void convert ( char  infix[],
char  postfix[] 
)

convert function

Parameters
infix[]: infix array provided by user
postfix[]: empty array to be given to convert()
Returns
postfixed expresion or \0 on exit
131 {
132  struct Stack s; // initialze object reference of stack
133  s.tos = -1; // initalize the tos
134 
135  int i, j = 0, pr;
136  char ch, temp;
137 
138  for (i = 0; infix[i] != '\0'; i++)
139  {
140  ch = infix[i];
141 
142  if (isOprnd(ch) == 1) // check if char is operand or operator
143  {
144  postfix[j] = ch; // assign ch to postfix array with index j
145  j++; // incement j
146  }
147  else
148  {
149  if (ch == '(')
150  {
151  push(&s, ch);
152  }
153  else
154  {
155  if (ch == ')')
156  {
157  while ((temp = pop(&s)) != '(')
158  {
159  postfix[j] = temp;
160  j++;
161  }
162  }
163  else
164  {
165  while (isEmpty(s) == 0) // check if stack is empty
166  {
167  pr = getPrecedence (ch,
168  s.arr[s.tos]); // check operator precedence
169 
170  if (pr == 1)
171  {
172  break; // if ch has a greater precedence than
173  // s.arr[s.top]
174  }
175 
176  postfix[j] = pop(&s);
177  j++;
178  }
179 
180  push(&s, ch); // push ch to stack
181  }
182  }
183  }
184  }
185 
186  while (isEmpty(s) == 0) // check if stack is empty
187  {
188  postfix[j] = pop(&s);
189  j++;
190  }
191 
192  postfix[j] = '\0';
193 }
void push(struct Stack *p, char ch)
push function
Definition: infix_to_postfix.c:55
char pop(struct Stack *p)
pop function
Definition: infix_to_postfix.c:72
int getPrecedence(char op1, char op2)
getPrecedence function returns the precedence after comparing two operators passed as parameter.
Definition: infix_to_postfix.c:201
int isEmpty(struct Stack s)
isEmpty function
Definition: infix_to_postfix.c:112
int isOprnd(char ch)
isOprnd function
Definition: infix_to_postfix.c:93
for printf() and scanf()
Definition: infix_to_postfix.c:18
int tos
static array of integers
Definition: infix_to_postfix.c:20
Here is the call graph for this function:

◆ getPrecedence()

int getPrecedence ( char  op1,
char  op2 
)

getPrecedence function returns the precedence after comparing two operators passed as parameter.

Parameters
op1: first operator
op2: second operator
Returns
1 or 0 on exit
202 {
203  if (op2 == '$')
204  {
205  return 0;
206  }
207  else if (op1 == '$')
208  {
209  return 1;
210  }
211  else if (op2 == '*' || op2 == '/' || op2 == '%')
212  {
213  return 0;
214  }
215  else if (op1 == '*' || op1 == '/' || op1 == '%')
216  {
217  return 1;
218  }
219  else if (op2 == '+' || op2 == '-')
220  {
221  return 0;
222  }
223  else
224  {
225  return 1;
226  }
227 }

◆ isEmpty()

int isEmpty ( struct Stack  s)

isEmpty function

Parameters
s: it is the object reference of stack
Returns
1 or 0 on exit
113 {
114  if (s.tos == -1) // check if stack is empty
115  {
116  return 1; // return for true result
117  }
118  else
119  {
120  return 0; // return for false result
121  }
122 }

◆ isOprnd()

int isOprnd ( char  ch)

isOprnd function

Parameters
ch: this is the element from the infix array
Returns
1 or 0 on exit
94 {
95  if ((ch >= 65 && ch <= 90) ||
96  (ch >= 97 && ch <= 122) || // check if ch is an operator or
97  (ch >= 48 && ch <= 57)) // operand using ASCII values
98  {
99  return 1; // return for true result
100  }
101  else
102  {
103  return 0; // return for false result
104  }
105 }

◆ main()

int main ( void  )

main function

Returns
0 on exit
37 {
38  char infix[20], postfix[20]; // initialize empty infix and postfix array
39 
40  printf("Enter infix expression: "); // example : A+B-C*D/E$F
41  scanf("%s", infix); // get values for infix array
42 
43  convert(infix, postfix);
44  printf("Postfix expression is %s", postfix); // output : AB+CD*EF$/-
45 
46  return 0;
47 }
void convert(char infix[], char postfix[])
convert function
Definition: infix_to_postfix.c:130
Here is the call graph for this function:

◆ pop()

int pop ( struct Stack p)

pop function

Parameters
*p: used as a pointer variable of stack
Returns
x or \0 on exit
73 {
74  char x;
75 
76  if (p->tos == -1)
77  {
78  printf("Stack Underflow!");
79  return '\0';
80  }
81 
82  x = p->arr[p->tos]; // assign the value of stack at index tos to x
83  p->tos -= 1; // decrement tos
84 
85  return x;
86 }

◆ push()

void push ( struct Stack p,
char  x 
)

push function

Parameters
*p: used as a pointer variable of stack
x: char to be pushed in stack
Returns
void
56 {
57  if (p->tos == 9) // check if stack has reached its max limit
58  {
59  printf("Stack Overflow!");
60  return;
61  }
62 
63  p->tos += 1; // increment tos
64  p->arr[p->tos] = x; // assign char x to index of stack pointed by tos
65 }