From 88a82991f22ed2276a9fbb2e54c78b15cb5e0627 Mon Sep 17 00:00:00 2001 From: Kumar Yash Date: Mon, 25 Oct 2021 23:10:53 +0530 Subject: [PATCH] feat: add postfix evaluation algorithm (#890) * add infix to postfix converter algorithm * docs: documentation changes * docs: documentation changes * updating DIRECTORY.md * docs: documentation changes * fix: continuous integration * [test, docs]: add test case, documentation changes * docs: documentation changes * fix: continuous integration * docs: documentation changes * docs: documentation changes * test: add new test * feat: add postfix evaluation algorithm * updating DIRECTORY.md * fix: increase stack size * fix: change data type * add: parse feature * fix: CodeQL * docs: documentation changes * remove unnecessary code Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 1 + misc/postfix_evaluation.c | 128 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 misc/postfix_evaluation.c diff --git a/DIRECTORY.md b/DIRECTORY.md index 2b405e29..482d8b4e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -273,6 +273,7 @@ * [Palindrome](https://github.com/TheAlgorithms/C/blob/master/misc/palindrome.c) * [Pid](https://github.com/TheAlgorithms/C/blob/master/misc/pid.c) * [Poly Add](https://github.com/TheAlgorithms/C/blob/master/misc/poly_add.c) + * [Postfix Evaluation](https://github.com/TheAlgorithms/C/blob/master/misc/postfix_evaluation.c) * [Prime](https://github.com/TheAlgorithms/C/blob/master/misc/prime.c) * [Prime Factoriziation](https://github.com/TheAlgorithms/C/blob/master/misc/prime_factoriziation.c) * [Prime Seive](https://github.com/TheAlgorithms/C/blob/master/misc/prime_seive.c) diff --git a/misc/postfix_evaluation.c b/misc/postfix_evaluation.c new file mode 100644 index 00000000..ca8e97c3 --- /dev/null +++ b/misc/postfix_evaluation.c @@ -0,0 +1,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 /// for IO operations +#include /// for strlen() +#include /// for isdigit() +#include /// for exit() +#include /// for int8_t +#include /// 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; +}