Evaluation of Postfix Expression
More...
#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <string>
|
| 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...
|
| |
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
◆ 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
79 const char *op = operation.
c_str();
◆ is_number()
| bool others::postfix_expression::is_number |
( |
const std::string & |
s | ) |
|
Checks if scanned string is a number.
- Parameters
-
- Returns
- bool boolean value if string is number
◆ main()
Main function.
- Returns
- 0 on exit
175 std::cout <<
"\nTest implementations passed!\n";
◆ 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
◆ postfix_evaluation()
Postfix Evaluation algorithm to compute the value from given input array.
- Template Parameters
-
- Parameters
-
| input | Array of characters consisting of numbers and operations |
- Returns
- stack[stackTop] returns the top value from the stack
◆ 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
◆ test_function_1()
| static void test_function_1 |
( |
| ) |
|
|
static |
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
- Returns
- none
151 assert(answer == -4);
◆ test_function_2()
| static void test_function_2 |
( |
| ) |
|
|
static |
Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.
- Returns
- none
164 assert(answer == 757);
bool is_number(const std::string &s)
Checks if scanned string is a number.
Definition: postfix_evaluation.cpp:65
static void test_function_2()
Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.
Definition: postfix_evaluation.cpp:159
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
static void test_function_1()
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
Definition: postfix_evaluation.cpp:146
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