diff --git a/others/postfix_evaluation.cpp b/others/postfix_evaluation.cpp index 722f0d330a..9132b9b03e 100644 --- a/others/postfix_evaluation.cpp +++ b/others/postfix_evaluation.cpp @@ -12,11 +12,11 @@ * When the expression is ended, the number in the stack is the final answer */ #include // for all_of -#include // for array #include // for assert #include // for io operations #include // for std::stack #include // for stof +#include // for std::vector /** * @namespace others @@ -80,17 +80,13 @@ void evaluate(float a, float b, const std::string &operation, /** * @brief Postfix Evaluation algorithm to compute the value from given input * array - * @tparam N number of array size - * @param input Array of characters consisting of numbers and operations + * @param input vector of strings consisting of numbers and operations * @returns stack[stackTop] returns the top value from the stack */ -template -float postfix_evaluation(std::array input) { +float postfix_evaluation(const std::vector &input) { std::stack stack; - int j = 0; - while (j < N) { - std::string scan = input[j]; + for (const auto &scan : input) { if (is_number(scan)) { stack.push(std::stof(scan)); @@ -102,7 +98,6 @@ float postfix_evaluation(std::array input) { evaluate(op1, op2, scan, stack); } - j++; } std::cout << stack.top() << "\n"; @@ -118,7 +113,7 @@ float postfix_evaluation(std::array input) { * @returns none */ static void test_function_1() { - std::array input = {"2", "3", "1", "*", "+", "9", "-"}; + std::vector input = {"2", "3", "1", "*", "+", "9", "-"}; float answer = others::postfix_expression::postfix_evaluation(input); @@ -131,15 +126,15 @@ static void test_function_1() { * @returns none */ static void test_function_2() { - std::array input = {"100", "200", "+", "2", "/", - "5", "*", "7", "+"}; + std::vector input = {"100", "200", "+", "2", "/", + "5", "*", "7", "+"}; float answer = others::postfix_expression::postfix_evaluation(input); assert(answer == 757); } static void test_function_3() { - std::array input = { + std::vector input = { "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",