Skip to content

Commit 857672b

Browse files
Revert "fix: handle wrong inputs in postfix_evaluation (TheAlgorithms#3005)"
This reverts commit 2ea98ce.
1 parent 63e557c commit 857672b

File tree

1 file changed

+7
-62
lines changed

1 file changed

+7
-62
lines changed

others/postfix_evaluation.cpp

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,6 @@ void evaluate(float a, float b, const std::string &operation,
7777
}
7878
}
7979

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-
9180
/**
9281
* @brief Postfix Evaluation algorithm to compute the value from given input
9382
* array
@@ -102,18 +91,18 @@ float postfix_evaluation(const std::vector<std::string> &input) {
10291
stack.push(std::stof(scan));
10392

10493
} 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();
10798

10899
evaluate(op1, op2, scan, stack);
109100
}
110101
}
111102

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();
117106
}
118107
} // namespace postfix_expression
119108
} // namespace others
@@ -155,46 +144,6 @@ static void test_function_3() {
155144
assert(answer == 22);
156145
}
157146

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-
198147
/**
199148
* @brief Main function
200149
* @returns 0 on exit
@@ -203,10 +152,6 @@ int main() {
203152
test_function_1();
204153
test_function_2();
205154
test_function_3();
206-
test_single_input();
207-
test_not_enough_operands();
208-
test_not_enough_operands_empty_input();
209-
test_too_many_operands();
210155

211156
std::cout << "\nTest implementations passed!\n";
212157

0 commit comments

Comments
 (0)