12
12
* When the expression is ended, the number in the stack is the final answer
13
13
*/
14
14
#include < algorithm> // for all_of
15
- #include < array> // for array
16
15
#include < cassert> // for assert
17
16
#include < iostream> // for io operations
18
17
#include < stack> // for std::stack
19
18
#include < string> // for stof
19
+ #include < vector> // for std::vector
20
20
21
21
/* *
22
22
* @namespace others
@@ -77,37 +77,43 @@ 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
+
80
91
/* *
81
92
* @brief Postfix Evaluation algorithm to compute the value from given input
82
93
* array
83
- * @tparam N number of array size
84
- * @param input Array of characters consisting of numbers and operations
94
+ * @param input vector of strings consisting of numbers and operations
85
95
* @returns stack[stackTop] returns the top value from the stack
86
96
*/
87
- template <std::size_t N>
88
- float postfix_evaluation (std::array<std::string, N> input) {
97
+ float postfix_evaluation (const std::vector<std::string> &input) {
89
98
std::stack<float > stack;
90
- int j = 0 ;
91
99
92
- while (j < N) {
93
- std::string scan = input[j];
100
+ for (const auto &scan : input) {
94
101
if (is_number (scan)) {
95
102
stack.push (std::stof (scan));
96
103
97
104
} else {
98
- const float op2 = stack.top ();
99
- stack.pop ();
100
- const float op1 = stack.top ();
101
- stack.pop ();
105
+ const auto op2 = remove_from_stack (stack);
106
+ const auto op1 = remove_from_stack (stack);
102
107
103
108
evaluate (op1, op2, scan, stack);
104
109
}
105
- j++;
106
110
}
107
111
108
- std::cout << stack.top () << " \n " ;
109
-
110
- return stack.top ();
112
+ const auto res = remove_from_stack (stack);
113
+ if (!stack.empty ()) {
114
+ throw std::invalid_argument (" Too many operands" );
115
+ }
116
+ return res;
111
117
}
112
118
} // namespace postfix_expression
113
119
} // namespace others
@@ -118,7 +124,7 @@ float postfix_evaluation(std::array<std::string, N> input) {
118
124
* @returns none
119
125
*/
120
126
static void test_function_1 () {
121
- std::array <std::string, 7 > input = {" 2" , " 3" , " 1" , " *" , " +" , " 9" , " -" };
127
+ std::vector <std::string> input = {" 2" , " 3" , " 1" , " *" , " +" , " 9" , " -" };
122
128
123
129
float answer = others::postfix_expression::postfix_evaluation (input);
124
130
@@ -131,15 +137,15 @@ static void test_function_1() {
131
137
* @returns none
132
138
*/
133
139
static void test_function_2 () {
134
- std::array <std::string, 9 > input = {" 100" , " 200" , " +" , " 2" , " /" ,
135
- " 5" , " *" , " 7" , " +" };
140
+ std::vector <std::string> input = {" 100" , " 200" , " +" , " 2" , " /" ,
141
+ " 5" , " *" , " 7" , " +" };
136
142
float answer = others::postfix_expression::postfix_evaluation (input);
137
143
138
144
assert (answer == 757 );
139
145
}
140
146
141
147
static void test_function_3 () {
142
- std::array <std::string, 43 > input = {
148
+ std::vector <std::string> input = {
143
149
" 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" ,
144
150
" 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" ,
145
151
" +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" ,
@@ -149,6 +155,46 @@ static void test_function_3() {
149
155
assert (answer == 22 );
150
156
}
151
157
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
+
152
198
/* *
153
199
* @brief Main function
154
200
* @returns 0 on exit
@@ -157,6 +203,10 @@ int main() {
157
203
test_function_1 ();
158
204
test_function_2 ();
159
205
test_function_3 ();
206
+ test_single_input ();
207
+ test_not_enough_operands ();
208
+ test_not_enough_operands_empty_input ();
209
+ test_too_many_operands ();
160
210
161
211
std::cout << " \n Test implementations passed!\n " ;
162
212
0 commit comments