-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
postfix_evaluation.c
128 lines (121 loc) · 2.89 KB
/
postfix_evaluation.c
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
/**
* @file
* @brief [Postfix evaluation algorithm](https://www.includehelp.com/c/evaluation-of-postfix-expressions-using-stack-with-c-program.aspx) implementation
* @details
* The input postfix expression is of type string upto 49 characters (including space delimiters).
* Supported operations- '+', '-', '/', '*', '%'
* @author [Kumar Yash](https://github.com/kumaryash18)
*/
#include <stdio.h> /// for IO operations
#include <string.h> /// for strlen()
#include <ctype.h> /// for isdigit()
#include <stdlib.h> /// for exit()
#include <stdint.h> /// for int8_t
#include <assert.h> /// for assert
/**
* @brief array implementation of stack using structure
*/
struct Stack {
int8_t stack[20]; ///< array stack
int top; ///< stores index of the top element
};
struct Stack st; ///< global declaration of stack st
/**
* @brief Function to push on the stack
* @param opd number to be pushed in the stack
* @returns void
*/
void push(int8_t opd) {
if(st.top == 19) { // overflow condition
printf("Stack overflow...");
exit(1);
}
st.top++;
st.stack[st.top] = opd;
}
/**
* @brief Function to pop from the stack
* @returns popped number
*/
int8_t pop() {
int8_t item; ///< to store the popped value to be returned
if(st.top == -1) { // underflow condition
printf("Stack underflow...");
exit(1);
}
item = st.stack[st.top];
st.top--;
return item;
}
/**
* @brief Function to evaluate postfix expression
* @param post the input postfix expression
* @returns evaluated answer
*/
int8_t evaluate(char post[]) {
int8_t it1;
int8_t it2;
int8_t temp;
int8_t number;
int i;
for(i = 0; i < strlen(post); i++) {
if(post[i] == ' ') {
continue; // ignore delimiter
}
else if(isdigit(post[i])) {
number = 0;
do {
number = number * 10 + (post[i]-'0');
i++;
} while(i < strlen(post) && isdigit(post[i]));
push(number);
}
else {
it2 = pop();
it1 = pop();
switch(post[i]) {
case '+':
temp = it1 + it2; break;
case '-':
temp = it1 - it2; break;
case '*':
temp = it1 * it2; break;
case '/':
temp = it1 / it2; break;
case '%':
temp = it1 % it2; break;
default:
printf("Invalid operator"); exit(1);
}
push(temp);
}
}
return pop();
}
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
/* check sample test case
input: "2 10 + 9 6 - /"
expected output: 4
*/
char temp1[50] = "2 10 + 9 6 - /";
assert(evaluate(temp1) == 4); /// this ensures that the algorithm works as expected
/* input: "4 2 + 3 5 1 - * +"
expected output: 18
*/
char temp2[50] = "4 2 + 3 5 1 - * +";
assert(evaluate(temp2) == 18); /// this ensures that the algorithm works as expected
printf("All tests have successfully passed!\n");
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
st.top = -1; /// initialize
test(); /// run self-test implementations
return 0;
}