TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
postfix_evaluation.cpp
Go to the documentation of this file.
1
13#include <algorithm> // for all_of
14#include <array> // for std::array
15#include <cassert> // for assert
16#include <iostream> // for io operations
17#include <string> // for stof
18
23namespace others {
28namespace postfix_expression {
32class Stack {
33 public:
34 std::array<float, 20> stack{};
35 int stackTop = -1;
36};
37
44void push(float operand, Stack *stack) {
45 stack->stackTop++;
46 stack->stack[stack->stackTop] = operand;
47}
48
54float pop(Stack *stack) {
55 float operand = stack->stack[stack->stackTop];
56 stack->stackTop--;
57 return operand;
58}
59
65bool is_number(const std::string &s) {
66 return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
67}
68
77void evaluate(float a, float b, const std::string &operation, Stack *stack) {
78 float c = 0;
79 const char *op = operation.c_str();
80 switch (*op) {
81 case '+':
82 c = a + b; // Addition of numbers
84 break;
85
86 case '-':
87 c = a - b; // Subtraction of numbers
89 break;
90
91 case '*':
92 c = a * b; // Multiplication of numbers
94 break;
95
96 case '/':
97 c = a / b; // Division of numbers
99 break;
100
101 default:
102 std::cout << "Operator not defined\n";
103 break;
104 }
105}
106
114template <std::size_t N>
115float postfix_evaluation(std::array<std::string, N> input) {
116 Stack stack;
117 int j = 0;
118
119 while (j < N) {
120 std::string scan = input[j];
121 if (is_number(scan)) {
122 push(std::stof(scan), &stack);
123
124 } else {
125 float op2 = pop(&stack);
126 float op1 = pop(&stack);
127
128 evaluate(op1, op2, scan, &stack);
129 }
130 j++;
131 }
132
133 std::cout << stack.stack[stack.stackTop] << "\n";
134
135 return stack.stack[stack.stackTop];
136}
137} // namespace postfix_expression
138} // namespace others
139
140
146static void test_function_1() {
147 std::array<std::string, 7> input = {"2", "3", "1", "*", "+", "9", "-"};
148
149 float answer = others::postfix_expression::postfix_evaluation(input);
150
151 assert(answer == -4);
152}
153
159static void test_function_2() {
160 std::array<std::string, 9> input = {"100", "200", "+", "2", "/",
161 "5", "*", "7", "+"};
162 float answer = others::postfix_expression::postfix_evaluation(input);
163
164 assert(answer == 757);
165}
166
171int main() {
174
175 std::cout << "\nTest implementations passed!\n";
176
177 return 0;
178}
Creates an array to be used as stack for storing values.
int stackTop
Represents the index of the last value added to array. -1 means array is empty.
for std::invalid_argument
Definition stack.hpp:19
std::shared_ptr< Node< value_type > > stackTop
Definition stack.hpp:75
for vector
Functions for Postfix Expression algorithm.
char stack[MAX]
void evaluate(float a, float b, const std::string &operation, Stack *stack)
Evaluate answer using given last two operands from and operation.
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', '-'}.
void push(float operand, Stack *stack)
Pushing operand, also called the number in the array to the stack.
float postfix_evaluation(std::array< std::string, N > input)
Postfix Evaluation algorithm to compute the value from given input array.
int main()
Main function.