TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Evaluation of Postfix Expression More...
#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <string>
Go to the source code of this file.
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
Definition in file postfix_evaluation.cpp.
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 |
Definition at line 77 of file postfix_evaluation.cpp.
bool others::postfix_expression::is_number | ( | const std::string & | s | ) |
Checks if scanned string is a number.
s | scanned string |
Definition at line 65 of file postfix_evaluation.cpp.
int main | ( | void | ) |
Main function.
Definition at line 171 of file postfix_evaluation.cpp.
float others::postfix_expression::pop | ( | Stack * | stack | ) |
Popping operand, also called the number from the stack.
stack | stack containing numbers |
Definition at line 54 of file postfix_evaluation.cpp.
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 |
Definition at line 115 of file postfix_evaluation.cpp.
void others::postfix_expression::push | ( | float | operand, |
Stack * | stack ) |
Pushing operand, also called the number in the array to the stack.
operand | float value from the input array or evaluation |
stack | stack containing numbers |
Definition at line 44 of file postfix_evaluation.cpp.
|
static |
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
Definition at line 146 of file postfix_evaluation.cpp.
|
static |
Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.
Definition at line 159 of file postfix_evaluation.cpp.