Loading...
Searching...
No Matches
Evaluation of Postfix Expression More...
#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <string>
Go to the source code of this file.
void others::postfix_expression::push (float operand, Stack *stack) Pushing operand, also called the number in the array to the stack.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.
◆ 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.
Definition at line 77 of file postfix_evaluation.cpp.
77 {
78 float c = 0;
79 const char *op = operation.c_str();
80 switch (*op) {
81 case '+':
82 c = a + b;
84 break;
85
86 case '-':
87 c = a - b;
89 break;
90
91 case '*':
92 c = a * b;
94 break;
95
96 case '/':
97 c = a / b;
99 break;
100
101 default:
102 std::cout << "Operator not defined\n";
103 break;
104 }
105}
for std::invalid_argument
void push(float operand, Stack *stack)
Pushing operand, also called the number in the array to the stack.
◆ is_number() bool others::postfix_expression::is_number ( const std::string & s )Checks if scanned string is a number.
Definition at line 65 of file postfix_evaluation.cpp.
65 {
66 return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
67}
◆ main()Main function.
Definition at line 171 of file postfix_evaluation.cpp.
171 {
174
175 std::cout << "\nTest implementations passed!\n";
176
177 return 0;
178}
static void test_function_2()
Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.
static void test_function_1()
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
◆ pop() float others::postfix_expression::pop ( Stack * stack )Popping operand, also called the number from the stack.
Definition at line 54 of file postfix_evaluation.cpp.
54 {
57 return operand;
58}
std::shared_ptr< Node< value_type > > stackTop
◆ 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.
Definition at line 115 of file postfix_evaluation.cpp.
115 {
117 int j = 0;
118
119 while (j < N) {
120 std::string scan = input[j];
123
124 } else {
127
128evaluate(op1, op2, scan, &
stack);
129 }
130 j++;
131 }
132
133std::cout <<
stack.stack[
stack.stackTop] <<
"\n";
134
136}
Creates an array to be used as stack for storing values.
char pop()
pop a byte out of stack variable
bool is_number(const std::string &s)
Checks if scanned string is a number.
◆ push() void others::postfix_expression::push ( float operand, Stack * stack )Pushing operand, also called the number in the array to the stack.
Definition at line 44 of file postfix_evaluation.cpp.
◆ test_function_1()Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
Definition at line 146 of file postfix_evaluation.cpp.
146 {
147 std::array<std::string, 7> input = {"2", "3", "1", "*", "+", "9", "-"};
148
150
151 assert(answer == -4);
152}
float postfix_evaluation(std::array< std::string, N > input)
Postfix Evaluation algorithm to compute the value from given input array.
◆ test_function_2()Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.
Definition at line 159 of file postfix_evaluation.cpp.
159 {
160 std::array<std::string, 9> input = {"100", "200", "+", "2", "/",
161 "5", "*", "7", "+"};
163
164 assert(answer == 757);
165}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4