Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
postfix_evaluation.c File Reference

Postfix evaluation algorithm implementation More...

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
Include dependency graph for postfix_evaluation.c:

Data Structures

struct  Stack
 for printf() and scanf() More...
 

Functions

void push (int8_t opd)
 Function to push on the stack.
 
int8_t pop ()
 Function to pop from the stack.
 
int8_t evaluate (char post[])
 Function to evaluate postfix expression.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Variables

struct Stack st
 global declaration of stack st
 

Detailed Description

Postfix evaluation algorithm implementation

The input postfix expression is of type string upto 49 characters (including space delimiters). Supported operations- '+', '-', '/', '*', ''

Author
Kumar Yash

Function Documentation

◆ evaluate()

int8_t evaluate ( char  post[])

Function to evaluate postfix expression.

Parameters
postthe input postfix expression
Returns
evaluated answer
60 {
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}
void push(struct Stack *p, char ch)
push function
Definition infix_to_postfix.c:55
int8_t pop()
Function to pop from the stack.
Definition postfix_evaluation.c:44
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit

initialize

run self-test implementations

124 {
125 st.top = -1; /// initialize
126 test(); /// run self-test implementations
127 return 0;
128}
struct Stack st
global declaration of stack st
Definition postfix_evaluation.c:24
static void test()
Self-test implementations.
Definition postfix_evaluation.c:105
int top
stores index of the top element
Definition infix_to_postfix2.c:23
Here is the call graph for this function:

◆ pop()

int8_t pop ( void  )

Function to pop from the stack.

Returns
popped number

< to store the popped value to be returned

44 {
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}
char stack[10]
array stack
Definition infix_to_postfix2.c:22

◆ push()

void push ( int8_t  opd)

Function to push on the stack.

Parameters
opdnumber to be pushed in the stack
Returns
void
31 {
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}
Here is the call graph for this function:

◆ 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

105 {
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}
int8_t evaluate(char post[])
Function to evaluate postfix expression.
Definition postfix_evaluation.c:60
Here is the call graph for this function: