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
14#include <algorithm> // for all_of
15#include <array> // for array
16#include <cassert> // for assert
17#include <iostream> // for io operations
18#include <stack> // for std::stack
19#include <string> // for stof
20
25namespace others {
30namespace postfix_expression {
31
37bool is_number(const std::string &s) {
38 return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
39}
40
49void evaluate(float a, float b, const std::string &operation,
50 std::stack<float> &stack) {
51 float c = 0;
52 const char *op = operation.c_str();
53 switch (*op) {
54 case '+':
55 c = a + b; // Addition of numbers
56 stack.push(c);
57 break;
58
59 case '-':
60 c = a - b; // Subtraction of numbers
61 stack.push(c);
62 break;
63
64 case '*':
65 c = a * b; // Multiplication of numbers
66 stack.push(c);
67 break;
68
69 case '/':
70 c = a / b; // Division of numbers
71 stack.push(c);
72 break;
73
74 default:
75 std::cout << "Operator not defined\n";
76 break;
77 }
78}
79
87template <std::size_t N>
88float postfix_evaluation(std::array<std::string, N> input) {
89 std::stack<float> stack;
90 int j = 0;
91
92 while (j < N) {
93 std::string scan = input[j];
94 if (is_number(scan)) {
95 stack.push(std::stof(scan));
96
97 } else {
98 const float op2 = stack.top();
99 stack.pop();
100 const float op1 = stack.top();
101 stack.pop();
102
103 evaluate(op1, op2, scan, stack);
104 }
105 j++;
106 }
107
108 std::cout << stack.top() << "\n";
109
110 return stack.top();
111}
112} // namespace postfix_expression
113} // namespace others
114
120static void test_function_1() {
121 std::array<std::string, 7> input = {"2", "3", "1", "*", "+", "9", "-"};
122
124
125 assert(answer == -4);
126}
127
133static void test_function_2() {
134 std::array<std::string, 9> input = {"100", "200", "+", "2", "/",
135 "5", "*", "7", "+"};
137
138 assert(answer == 757);
139}
140
141static void test_function_3() {
142 std::array<std::string, 43> input = {
143 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
144 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
145 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
146 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+"};
148
149 assert(answer == 22);
150}
151
156int main() {
159 test_function_3();
160
161 std::cout << "\nTest implementations passed!\n";
162
163 return 0;
164}
for std::invalid_argument
Definition stack.hpp:19
void pop()
Definition stack.hpp:62
void push(const value_type &item)
Definition stack.hpp:47
value_type top() const
Definition stack.hpp:56
for vector
Functions for Postfix Expression algorithm.
void evaluate(float a, float b, const std::string &operation, std::stack< float > &stack)
Evaluate answer using given last two operands from and operation.
bool is_number(const std::string &s)
Checks if scanned string is a number.
static void test_function_2()
Test function 2 with input array {'100', '200', '+', '2', '/', '5', '*', '7', '+'}...
static void test_function_1()
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
float postfix_evaluation(std::array< std::string, N > input)
Postfix Evaluation algorithm to compute the value from given input array.
int main()
Main function.