@@ -77,17 +77,6 @@ void evaluate(float a, float b, const std::string &operation,
77
77
}
78
78
}
79
79
80
- namespace {
81
- float remove_from_stack (std::stack<float > &stack) {
82
- if (stack.empty ()) {
83
- throw std::invalid_argument (" Not enough operands" );
84
- }
85
- const auto res = stack.top ();
86
- stack.pop ();
87
- return res;
88
- }
89
- } // namespace
90
-
91
80
/* *
92
81
* @brief Postfix Evaluation algorithm to compute the value from given input
93
82
* array
@@ -102,18 +91,18 @@ float postfix_evaluation(const std::vector<std::string> &input) {
102
91
stack.push (std::stof (scan));
103
92
104
93
} else {
105
- const auto op2 = remove_from_stack (stack);
106
- const auto op1 = remove_from_stack (stack);
94
+ const float op2 = stack.top ();
95
+ stack.pop ();
96
+ const float op1 = stack.top ();
97
+ stack.pop ();
107
98
108
99
evaluate (op1, op2, scan, stack);
109
100
}
110
101
}
111
102
112
- const auto res = remove_from_stack (stack);
113
- if (!stack.empty ()) {
114
- throw std::invalid_argument (" Too many operands" );
115
- }
116
- return res;
103
+ std::cout << stack.top () << " \n " ;
104
+
105
+ return stack.top ();
117
106
}
118
107
} // namespace postfix_expression
119
108
} // namespace others
@@ -155,46 +144,6 @@ static void test_function_3() {
155
144
assert (answer == 22 );
156
145
}
157
146
158
- static void test_single_input () {
159
- std::vector<std::string> input = {" 1" };
160
- float answer = others::postfix_expression::postfix_evaluation (input);
161
-
162
- assert (answer == 1 );
163
- }
164
-
165
- static void test_not_enough_operands () {
166
- std::vector<std::string> input = {" +" };
167
- bool throws = false ;
168
- try {
169
- others::postfix_expression::postfix_evaluation (input);
170
- } catch (std::invalid_argument &) {
171
- throws = true ;
172
- }
173
- assert (throws);
174
- }
175
-
176
- static void test_not_enough_operands_empty_input () {
177
- std::vector<std::string> input = {};
178
- bool throws = false ;
179
- try {
180
- others::postfix_expression::postfix_evaluation (input);
181
- } catch (std::invalid_argument &) {
182
- throws = true ;
183
- }
184
- assert (throws);
185
- }
186
-
187
- static void test_too_many_operands () {
188
- std::vector<std::string> input = {" 1" , " 2" };
189
- bool throws = false ;
190
- try {
191
- others::postfix_expression::postfix_evaluation (input);
192
- } catch (std::invalid_argument &) {
193
- throws = true ;
194
- }
195
- assert (throws);
196
- }
197
-
198
147
/* *
199
148
* @brief Main function
200
149
* @returns 0 on exit
@@ -203,10 +152,6 @@ int main() {
203
152
test_function_1 ();
204
153
test_function_2 ();
205
154
test_function_3 ();
206
- test_single_input ();
207
- test_not_enough_operands ();
208
- test_not_enough_operands_empty_input ();
209
- test_too_many_operands ();
210
155
211
156
std::cout << " \n Test implementations passed!\n " ;
212
157
0 commit comments