|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief [Postfix evaluation algorithm](https://www.includehelp.com/c/evaluation-of-postfix-expressions-using-stack-with-c-program.aspx) implementation |
| 4 | + * @details |
| 5 | + * The input postfix expression is of type string upto 49 characters (including space delimiters). |
| 6 | + * Supported operations- '+', '-', '/', '*', '%' |
| 7 | + * @author [Kumar Yash](https://github.com/kumaryash18) |
| 8 | + */ |
| 9 | + |
| 10 | +#include <stdio.h> /// for IO operations |
| 11 | +#include <string.h> /// for strlen() |
| 12 | +#include <ctype.h> /// for isdigit() |
| 13 | +#include <stdlib.h> /// for exit() |
| 14 | +#include <stdint.h> /// for int8_t |
| 15 | +#include <assert.h> /// for assert |
| 16 | + |
| 17 | +/** |
| 18 | + * @brief array implementation of stack using structure |
| 19 | + */ |
| 20 | +struct Stack { |
| 21 | + int8_t stack[20]; ///< array stack |
| 22 | + int top; ///< stores index of the top element |
| 23 | +}; |
| 24 | +struct Stack st; ///< global declaration of stack st |
| 25 | + |
| 26 | +/** |
| 27 | + * @brief Function to push on the stack |
| 28 | + * @param opd number to be pushed in the stack |
| 29 | + * @returns void |
| 30 | + */ |
| 31 | +void push(int8_t opd) { |
| 32 | + if(st.top == 19) { // overflow condition |
| 33 | + printf("Stack overflow..."); |
| 34 | + exit(1); |
| 35 | + } |
| 36 | + st.top++; |
| 37 | + st.stack[st.top] = opd; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief Function to pop from the stack |
| 42 | + * @returns popped number |
| 43 | + */ |
| 44 | +int8_t pop() { |
| 45 | + int8_t item; ///< to store the popped value to be returned |
| 46 | + if(st.top == -1) { // underflow condition |
| 47 | + printf("Stack underflow..."); |
| 48 | + exit(1); |
| 49 | + } |
| 50 | + item = st.stack[st.top]; |
| 51 | + st.top--; |
| 52 | + return item; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * @brief Function to evaluate postfix expression |
| 57 | + * @param post the input postfix expression |
| 58 | + * @returns evaluated answer |
| 59 | + */ |
| 60 | +int8_t evaluate(char post[]) { |
| 61 | + int8_t it1; |
| 62 | + int8_t it2; |
| 63 | + int8_t temp; |
| 64 | + int8_t number; |
| 65 | + int i; |
| 66 | + for(i = 0; i < strlen(post); i++) { |
| 67 | + if(post[i] == ' ') { |
| 68 | + continue; // ignore delimiter |
| 69 | + } |
| 70 | + else if(isdigit(post[i])) { |
| 71 | + number = 0; |
| 72 | + do { |
| 73 | + number = number * 10 + (post[i]-'0'); |
| 74 | + i++; |
| 75 | + } while(i < strlen(post) && isdigit(post[i])); |
| 76 | + push(number); |
| 77 | + } |
| 78 | + else { |
| 79 | + it2 = pop(); |
| 80 | + it1 = pop(); |
| 81 | + switch(post[i]) { |
| 82 | + case '+': |
| 83 | + temp = it1 + it2; break; |
| 84 | + case '-': |
| 85 | + temp = it1 - it2; break; |
| 86 | + case '*': |
| 87 | + temp = it1 * it2; break; |
| 88 | + case '/': |
| 89 | + temp = it1 / it2; break; |
| 90 | + case '%': |
| 91 | + temp = it1 % it2; break; |
| 92 | + default: |
| 93 | + printf("Invalid operator"); exit(1); |
| 94 | + } |
| 95 | + push(temp); |
| 96 | + } |
| 97 | + } |
| 98 | + return pop(); |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * @brief Self-test implementations |
| 103 | + * @returns void |
| 104 | + */ |
| 105 | +static void test() { |
| 106 | + /* check sample test case |
| 107 | + input: "2 10 + 9 6 - /" |
| 108 | + expected output: 4 |
| 109 | + */ |
| 110 | + char temp1[50] = "2 10 + 9 6 - /"; |
| 111 | + assert(evaluate(temp1) == 4); /// this ensures that the algorithm works as expected |
| 112 | + /* input: "4 2 + 3 5 1 - * +" |
| 113 | + expected output: 18 |
| 114 | + */ |
| 115 | + char temp2[50] = "4 2 + 3 5 1 - * +"; |
| 116 | + assert(evaluate(temp2) == 18); /// this ensures that the algorithm works as expected |
| 117 | + printf("All tests have successfully passed!\n"); |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * @brief Main function |
| 122 | + * @returns 0 on exit |
| 123 | + */ |
| 124 | +int main() { |
| 125 | + st.top = -1; /// initialize |
| 126 | + test(); /// run self-test implementations |
| 127 | + return 0; |
| 128 | +} |
0 commit comments