Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Evaluation of Postfix Expression More...
#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <string>
Classes | |
class | others::postfix_expression::Stack |
Creates an array to be used as stack for storing values. More... | |
Namespaces | |
namespace | others |
for vector | |
namespace | 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. | |
float | others::postfix_expression::pop (Stack *stack) |
Popping operand, also called the number from the stack. | |
bool | others::postfix_expression::is_number (const std::string &s) |
Checks if scanned string is a number. | |
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. | |
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. | |
static void | test_function_1 () |
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}. | |
static void | test_function_2 () |
Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}. | |
int | main () |
Main function. | |
Evaluation of Postfix Expression
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
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.
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 |
bool others::postfix_expression::is_number | ( | const std::string & | s | ) |
Checks if scanned string is a number.
s | scanned string |
int main | ( | void | ) |
Main function.
float others::postfix_expression::pop | ( | Stack * | stack | ) |
Popping operand, also called the number from the stack.
stack | stack containing numbers |
float others::postfix_expression::postfix_evaluation | ( | std::array< std::string, N > | input | ) |
Postfix Evaluation algorithm to compute the value from given input array.
N | number of array size |
input | Array of characters consisting of numbers and operations |
void others::postfix_expression::push | ( | float | operand, |
Stack * | stack ) |
|
static |
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
|
static |
Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.