Skip to content

Commit

Permalink
feat: add postfix evaluation algorithm (#890)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
kumaryash18 and github-actions authored Oct 25, 2021
1 parent ce3f01f commit 88a8299
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
128 changes: 128 additions & 0 deletions misc/postfix_evaluation.c
Original file line number Diff line number Diff line change
@@ -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 <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;
}

0 comments on commit 88a8299

Please sign in to comment.