![]() |
TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Evaluation of Postfix Expression More...
#include <algorithm>
#include <cassert>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | others |
for vector | |
namespace | postfix_expression |
Functions for Postfix Expression algorithm. |
Functions | |
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, std::stack< float > &stack) |
Evaluate answer using given last two operands from and operation. | |
float | others::postfix_expression::postfix_evaluation (const std::vector< std::string > &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 {'100', '200', '+', '2', '/', '5', '*', '7', '+'}. | |
static void | test_function_3 () |
static void | test_single_input () |
static void | test_not_enough_operands () |
static void | test_not_enough_operands_empty_input () |
static void | test_too_many_operands () |
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, | ||
std::stack< float > & | 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 49 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 37 of file postfix_evaluation.cpp.
int main | ( | void | ) |
Main function.
Definition at line 202 of file postfix_evaluation.cpp.
float others::postfix_expression::postfix_evaluation | ( | const std::vector< std::string > & | input | ) |
Postfix Evaluation algorithm to compute the value from given input array.
input | vector of strings consisting of numbers and operations |
Definition at line 97 of file postfix_evaluation.cpp.
|
static |
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
Definition at line 126 of file postfix_evaluation.cpp.
|
static |
Test function 2 with input array {'100', '200', '+', '2', '/', '5', '*', '7', '+'}.
Definition at line 139 of file postfix_evaluation.cpp.
|
static |
Definition at line 147 of file postfix_evaluation.cpp.
|
static |
Definition at line 165 of file postfix_evaluation.cpp.
|
static |
Definition at line 176 of file postfix_evaluation.cpp.
|
static |
Definition at line 158 of file postfix_evaluation.cpp.
|
static |
Definition at line 187 of file postfix_evaluation.cpp.