1212 * When the expression is ended, the number in the stack is the final answer
1313 */
1414#include < algorithm> // for all_of
15- #include < array> // for array
1615#include < cassert> // for assert
1716#include < iostream> // for io operations
1817#include < stack> // for std::stack
1918#include < string> // for stof
19+ #include < vector> // for std::vector
2020
2121/* *
2222 * @namespace others
@@ -80,17 +80,13 @@ void evaluate(float a, float b, const std::string &operation,
8080/* *
8181 * @brief Postfix Evaluation algorithm to compute the value from given input
8282 * array
83- * @tparam N number of array size
84- * @param input Array of characters consisting of numbers and operations
83+ * @param input vector of strings consisting of numbers and operations
8584 * @returns stack[stackTop] returns the top value from the stack
8685 */
87- template <std::size_t N>
88- float postfix_evaluation (std::array<std::string, N> input) {
86+ float postfix_evaluation (const std::vector<std::string> &input) {
8987 std::stack<float > stack;
90- int j = 0 ;
9188
92- while (j < N) {
93- std::string scan = input[j];
89+ for (const auto &scan : input) {
9490 if (is_number (scan)) {
9591 stack.push (std::stof (scan));
9692
@@ -102,7 +98,6 @@ float postfix_evaluation(std::array<std::string, N> input) {
10298
10399 evaluate (op1, op2, scan, stack);
104100 }
105- j++;
106101 }
107102
108103 std::cout << stack.top () << " \n " ;
@@ -118,7 +113,7 @@ float postfix_evaluation(std::array<std::string, N> input) {
118113 * @returns none
119114 */
120115static void test_function_1 () {
121- std::array <std::string, 7 > input = {" 2" , " 3" , " 1" , " *" , " +" , " 9" , " -" };
116+ std::vector <std::string> input = {" 2" , " 3" , " 1" , " *" , " +" , " 9" , " -" };
122117
123118 float answer = others::postfix_expression::postfix_evaluation (input);
124119
@@ -131,15 +126,15 @@ static void test_function_1() {
131126 * @returns none
132127 */
133128static void test_function_2 () {
134- std::array <std::string, 9 > input = {" 100" , " 200" , " +" , " 2" , " /" ,
135- " 5" , " *" , " 7" , " +" };
129+ std::vector <std::string> input = {" 100" , " 200" , " +" , " 2" , " /" ,
130+ " 5" , " *" , " 7" , " +" };
136131 float answer = others::postfix_expression::postfix_evaluation (input);
137132
138133 assert (answer == 757 );
139134}
140135
141136static void test_function_3 () {
142- std::array <std::string, 43 > input = {
137+ std::vector <std::string> input = {
143138 " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" ,
144139 " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" ,
145140 " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" ,
0 commit comments