Infix to Postfix converter implementation
More...
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
|
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.
|
|
|
struct Stack | st |
| global declaration of stack st
|
|
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
◆ convert()
char * convert |
( |
char |
inf[] | ) |
|
Function to convert infix expression to postfix expression.
- Parameters
-
inf | the 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];
102 int i;
103 int j = 0;
104 for(i = 0; i < strlen(inf); i++) {
105 if(isalnum(inf[i])) {
106 post[j] = inf[i];
107 j++;
108 }
109 else if(inf[i] == '(') {
111 }
112 else if(inf[i] == ')') {
113 while(
Top() !=
'(') {
115 j++;
116 }
118 }
119 else {
122 j++;
123 }
125 }
126 }
129 j++;
130 }
131 post[j] = '\0';
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
◆ 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 {
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()
Main function.
- Returns
- 0 on exit
initialize
run self-test implementations
< to store input infix expression
156 {
159 char inf[25];
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
◆ pop()
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;
48 printf("Stack underflow...");
49 exit(1);
50 }
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
-
opr | operator 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()
Function to push on the stack.
- Parameters
-
opd | character to be pushed in the stack |
- Returns
- void
32 {
34 printf("Stack overflow...");
35 exit(1);
36 }
39}
◆ 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
141
142
143
144 assert(strcmp(
convert(
"(A/(B-C)*D+E)"),
"ABC-/D*E+") == 0);
145
146
147
148 assert(strcmp(
convert(
"7-(2*3+5)*(8-4/2)"),
"723*5+842/-*-") == 0);
149 printf("All tests have successfully passed!\n");
150}
◆ Top()
Function to get top of the stack.
- Returns
- top of stack