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 <cassert> // for assert
16#include <iostream> // for io operations
17#include <stack> // for std::stack
18#include <string> // for stof
19#include <vector> // for std::vector
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
86float postfix_evaluation(const std::vector<std::string> &input) {
87 std::stack<float> stack;
88
89 for (const auto &scan : input) {
90 if (is_number(scan)) {
91 stack.push(std::stof(scan));
92
93 } else {
94 const float op2 = stack.top();
95 stack.pop();
96 const float op1 = stack.top();
97 stack.pop();
98
99 evaluate(op1, op2, scan, stack);
100 }
101 }
102
103 std::cout << stack.top() << "\n";
104
105 return stack.top();
106}
107} // namespace postfix_expression
108} // namespace others
109
115static void test_function_1() {
116 std::vector<std::string> input = {"2", "3", "1", "*", "+", "9", "-"};
117
119
120 assert(answer == -4);
121}
122
128static void test_function_2() {
129 std::vector<std::string> input = {"100", "200", "+", "2", "/",
130 "5", "*", "7", "+"};
132
133 assert(answer == 757);
134}
135
136static void test_function_3() {
137 std::vector<std::string> input = {
138 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
139 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
140 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
141 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+"};
143
144 assert(answer == 22);
145}
146
151int main() {
154 test_function_3();
155
156 std::cout << "\nTest implementations passed!\n";
157
158 return 0;
159}
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.
float postfix_evaluation(const std::vector< std::string > &input)
Postfix Evaluation algorithm to compute the value from given input array.
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', '-'}.
int main()
Main function.