postfix_evaluation.cpp File Reference

Evaluation of Postfix Expression More...

`#include <algorithm>`
`#include <array>`
`#include <cassert>`
`#include <iostream>`
`#include <string>`
Include dependency graph for postfix_evaluation.cpp:

## Classes

class | others::postfix_expression::Stack
| Creates an array to be used as stack for storing values. More...

## Namespaces

| others
| Other algorithms.
| postfix_expression
| Functions for Postfix Expression algorithm.

## Functions

void | others::postfix_expression::push (float operand, Stack *stack)
| Pushing operand, also called the number in the array to the stack. More...
float | others::postfix_expression::pop (Stack *stack)
| Popping operand, also called the number from the stack. More...
bool | others::postfix_expression::is_number (const std::string &s)
| Checks if scanned string is a number. More...
void | others::postfix_expression::evaluate (float a, float b, const std::string &operation, Stack *stack)
| Evaluate answer using given last two operands from and operation. More...
template<std::size_t N>
float | others::postfix_expression::postfix_evaluation (std::array< std::string, N > input)
| Postfix Evaluation algorithm to compute the value from given input array. More...
static void | test_function_1 ()
| Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}. More...
static void | test_function_2 ()
| Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}. More...
int | main ()
| Main function. More...

## Detailed Description

Evaluation of Postfix Expression

Author
  Darshana Sarma

Create a stack to store operands (or values). Scan the given expression and do following for every scanned element. If the element is a number, push it into the stack If the element is a operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack When the expression is ended, the number in the stack is the final answer

## Function Documentation

## evaluate()

void others::postfix_expression::evaluate | ( | float | a,
|  | float | b,
|  | const std::string & | operation,
|  | Stack * | stack
| ) |  |

Evaluate answer using given last two operands from and operation.

Parameters

  a | second last added operand which will be used for evaluation
  b | last added operand which will be used for evaluation
  operation | to be performed with respective floats
  stack | containing numbers

Returns
  none

{
float c = 0;
const char *op = operation.c_str();
switch (*op) {
case '+':
c = a + b; // Addition of numbers
others::postfix_expression::push(c, stack);
break;
case '-':
c = a - b; // Subtraction of numbers
others::postfix_expression::push(c, stack);
break;
case '*':
c = a * b; // Multiplication of numbers
others::postfix_expression::push(c, stack);
break;
case '/':
c = a / b; // Division of numbers
others::postfix_expression::push(c, stack);
break;
default:
std::cout << "Operator not defined\n";
break;
}
}
std::cout
std::string::c_str
T c_str(T... args)
stack
Definition: stack.h:26
push
void push(char ch)
push byte to stack variable
Definition: paranthesis_matching.cpp:26
Here is the call graph for this function:

## is_number()

bool others::postfix_expression::is_number | ( | const std::string & | s | ) |

Checks if scanned string is a number.

Parameters

  s | scanned string

Returns
  bool boolean value if string is number

{
return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
}
std::all_of
T all_of(T... args)
std::string::begin
T begin(T... args)
std::string::empty
T empty(T... args)
std::string::end
T end(T... args)
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test_function_1();
test_function_2();
std::cout << "\nTest implementations passed!\n";
return 0;
}
test_function_2
static void test_function_2()
Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.
Definition: postfix_evaluation.cpp:159
test_function_1
static void test_function_1()
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
Definition: postfix_evaluation.cpp:146
Here is the call graph for this function:

## pop()

float others::postfix_expression::pop | ( | Stack * | stack | ) |

Popping operand, also called the number from the stack.

Parameters

  stack | stack containing numbers

Returns
  operand float on top of stack

{
float operand = stack->stack[stack->stackTop];
stack->stackTop--;
return operand;
}
stack::stack
stack()
Definition: stack.h:41
stack::stackTop
node< Type > * stackTop
Definition: stack.h:146
Here is the call graph for this function:

## postfix_evaluation()

template<std::size_t N>

float others::postfix_expression::postfix_evaluation | ( | std::array< std::string, N > | input | ) |

Postfix Evaluation algorithm to compute the value from given input array.

Template Parameters

  N | number of array size

Parameters

  input | Array of characters consisting of numbers and operations

Returns
  stack[stackTop] returns the top value from the stack

{
Stack stack;
int j = 0;
while (j < N) {
std::string scan = input[j];
if (is_number(scan)) {
push(std::stof(scan), &stack);
} else {
float op2 = pop(&stack);
float op1 = pop(&stack);
evaluate(op1, op2, scan, &stack);
}
j++;
}
std::cout << stack.stack[stack.stackTop] << "\n";
return stack.stack[stack.stackTop];
}
std::string
pop
char pop()
pop a byte out of stack variable
Definition: paranthesis_matching.cpp:29
stack
char stack[MAX]
Definition: paranthesis_matching.cpp:20
others::postfix_expression::is_number
bool is_number(const std::string &s)
Checks if scanned string is a number.
Definition: postfix_evaluation.cpp:65
others::postfix_expression::evaluate
void evaluate(float a, float b, const std::string &operation, Stack *stack)
Evaluate answer using given last two operands from and operation.
Definition: postfix_evaluation.cpp:77
std::stof
T stof(T... args)
Here is the call graph for this function:

## push()

void others::postfix_expression::push | ( | float | operand,
|  | Stack * | stack
| ) |  |

Pushing operand, also called the number in the array to the stack.

Parameters

  operand | float value from the input array or evaluation
  stack | stack containing numbers

Returns
  none

{
stack->stackTop++;
stack->stack[stack->stackTop] = operand;
}
Here is the call graph for this function:

## test_function_1()

static void test_function_1 | ( |  | ) |

static

Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.

Returns
  none

{
std::array<std::string, 7> input = {"2", "3", "1", "*", "+", "9", "-"};
float answer = others::postfix_expression::postfix_evaluation(input);
assert(answer == -4);
}
std::array
others::postfix_expression::postfix_evaluation
float postfix_evaluation(std::array< std::string, N > input)
Postfix Evaluation algorithm to compute the value from given input array.
Definition: postfix_evaluation.cpp:115

## test_function_2()

static void test_function_2 | ( |  | ) |

static

Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.

Returns
  none

{
std::array<std::string, 9> input = {"100", "200", "+", "2", "/",
"5", "*", "7", "+"};
float answer = others::postfix_expression::postfix_evaluation(input);
assert(answer == 757);
}