Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
infix_to_postfix2.c File Reference

Infix to Postfix converter implementation More...

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
Include dependency graph for infix_to_postfix2.c:

Data Structures

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

Functions

void push (char opd)
 Function to push on the stack.
 
char pop ()
 Function to pop from the stack.
 
uint16_t isEmpty ()
 Function to check whether the stack is empty or not.
 
char Top ()
 Function to get top of the stack.
 
int16_t priority (char opr)
 Function to check priority of operators.
 
char * convert (char inf[])
 Function to convert infix expression to postfix expression.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Variables

struct Stack st
 global declaration of stack st
 

Detailed Description

Infix to Postfix converter implementation

The input infix expression is of type string upto 24 characters. Supported operations- '+', '-', '/', '*', ''

Author
Kumar Yash
See also
infix_to_postfix.c

Function Documentation

◆ convert()

char * convert ( char  inf[])

Function to convert infix expression to postfix expression.

Parameters
infthe input infix expression
Returns
output postfix expression

< to store the postfix expression

< loop iterator

< keeps track of end of postfix string

100 {
101 static char post[25]; ///< to store the postfix expression
102 int i; ///< loop iterator
103 int j = 0; ///< keeps track of end of postfix string
104 for(i = 0; i < strlen(inf); i++) {
105 if(isalnum(inf[i])) { // if scanned element is an alphabet or number
106 post[j] = inf[i]; // append in postfix expression
107 j++;
108 }
109 else if(inf[i] == '(') { // if scanned element is opening parentheses
110 push(inf[i]); // push on stack.
111 }
112 else if(inf[i] == ')') { // if scanned element is closing parentheses,
113 while(Top() != '(') { // pop elements from stack and append in postfix expression
114 post[j] = pop(); // until opening parentheses becomes top.
115 j++;
116 }
117 pop(); // pop opening parentheses
118 }
119 else { // if scanned element is an operator
120 while( (!isEmpty()) && (priority(inf[i]) <= priority(Top())) ) { // pop and append until stack becomes
121 post[j] = pop(); // empty or priority of top operator
122 j++; // becomes smaller than scanned operator
123 } // '(' has priority -1
124 push(inf[i]); // push the scanned operator
125 }
126 }
127 while(!isEmpty()) { // pop and append residual operators from stack
128 post[j] = pop();
129 j++;
130 }
131 post[j] = '\0'; // end postfix string with null character
132 return post;
133}
int16_t priority(char opr)
Function to check priority of operators.
Definition infix_to_postfix2.c:83
char Top()
Function to get top of the stack.
Definition infix_to_postfix2.c:72
char pop()
Function to pop from the stack.
Definition infix_to_postfix2.c:45
uint16_t isEmpty()
Function to check whether the stack is empty or not.
Definition infix_to_postfix2.c:61
void push(struct Stack *p, char ch)
push function
Definition infix_to_postfix.c:55
Here is the call graph for this function:

◆ isEmpty()

int isEmpty ( )

Function to check whether the stack is empty or not.

Returns 1 if stack is empty, returns 0 if not empty.

Returns
1 if the stack IS empty
0 if the stack is NOT empty
61 {
62 if(st.top == -1) {
63 return 1;
64 }
65 return 0;
66}
struct Stack st
global declaration of stack st
Definition infix_to_postfix2.c:25
int top
stores index of the top element
Definition infix_to_postfix2.c:23

◆ main()

int main ( void  )

Main function.

Returns
0 on exit

initialize

run self-test implementations

< to store input infix expression

156 {
157 st.top = -1; /// initialize
158 test(); /// run self-test implementations
159 char inf[25]; ///< to store input infix expression
160 printf("Enter infix: ");
161 scanf("%s", inf);
162 printf("Postfix: %s", convert(inf));
163 return 0;
164}
static void test()
Self-test implementations.
Definition infix_to_postfix2.c:139
char * convert(char inf[])
Function to convert infix expression to postfix expression.
Definition infix_to_postfix2.c:100
Here is the call graph for this function:

◆ pop()

void pop ( void  )

Function to pop from the stack.

Pop data from the stack.

Returns
popped character

< to store the popped value to be returned

45 {
46 char item; ///< to store the popped value to be returned
47 if(st.top == -1) { // underflow condition
48 printf("Stack underflow...");
49 exit(1);
50 }
51 item = st.stack[st.top];
52 st.top--;
53 return item;
54}
char stack[10]
array stack
Definition infix_to_postfix2.c:22

◆ priority()

int16_t priority ( char  opr)

Function to check priority of operators.

Parameters
oproperator whose priority is to be checked
Returns
0 if operator is '+' or '-'
1 if operator is '/' or '*' or ''
-1 otherwise
83 {
84 if(opr == '+' || opr == '-') {
85 return 0;
86 }
87 else if(opr == '/' || opr == '*' || opr == '%') {
88 return 1;
89 }
90 else {
91 return -1;
92 }
93}

◆ push()

void push ( char  opd)

Function to push on the stack.

Parameters
opdcharacter to be pushed in the stack
Returns
void
32 {
33 if(st.top == 9) { // overflow condition
34 printf("Stack overflow...");
35 exit(1);
36 }
37 st.top++;
38 st.stack[st.top] = opd;
39}
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void

this ensures that the algorithm works as expected

this ensures that the algorithm works as expected

139 {
140 /* check sample test case
141 input- "(A/(B-C)*D+E)"
142 expected output- "ABC-/D*E+"
143 */
144 assert(strcmp(convert("(A/(B-C)*D+E)"), "ABC-/D*E+") == 0); /// this ensures that the algorithm works as expected
145 /* input- "7-(2*3+5)*(8-4/2)"
146 expected output- "723*5+842/-*-"
147 */
148 assert(strcmp(convert("7-(2*3+5)*(8-4/2)"), "723*5+842/-*-") == 0); /// this ensures that the algorithm works as expected
149 printf("All tests have successfully passed!\n");
150}
Here is the call graph for this function:

◆ Top()

char Top ( )

Function to get top of the stack.

Returns
top of stack
72 {
73 return st.stack[st.top];
74}