Skip to content

Commit c3b2bf8

Browse files
Revert "refactor: input size should not be a template argument in postfix_evaluation (TheAlgorithms#2996)"
This reverts commit b6d0448.
1 parent c411a40 commit c3b2bf8

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

others/postfix_evaluation.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
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
1516
#include <cassert> // for assert
1617
#include <iostream> // for io operations
1718
#include <stack> // for std::stack
1819
#include <string> // for stof
19-
#include <vector> // for std::vector
2020

2121
/**
2222
* @namespace others
@@ -80,13 +80,17 @@ 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-
* @param input vector of strings consisting of numbers and operations
83+
* @tparam N number of array size
84+
* @param input Array of characters consisting of numbers and operations
8485
* @returns stack[stackTop] returns the top value from the stack
8586
*/
86-
float postfix_evaluation(const std::vector<std::string> &input) {
87+
template <std::size_t N>
88+
float postfix_evaluation(std::array<std::string, N> input) {
8789
std::stack<float> stack;
90+
int j = 0;
8891

89-
for (const auto &scan : input) {
92+
while (j < N) {
93+
std::string scan = input[j];
9094
if (is_number(scan)) {
9195
stack.push(std::stof(scan));
9296

@@ -98,6 +102,7 @@ float postfix_evaluation(const std::vector<std::string> &input) {
98102

99103
evaluate(op1, op2, scan, stack);
100104
}
105+
j++;
101106
}
102107

103108
std::cout << stack.top() << "\n";
@@ -113,7 +118,7 @@ float postfix_evaluation(const std::vector<std::string> &input) {
113118
* @returns none
114119
*/
115120
static void test_function_1() {
116-
std::vector<std::string> input = {"2", "3", "1", "*", "+", "9", "-"};
121+
std::array<std::string, 7> input = {"2", "3", "1", "*", "+", "9", "-"};
117122

118123
float answer = others::postfix_expression::postfix_evaluation(input);
119124

@@ -126,15 +131,15 @@ static void test_function_1() {
126131
* @returns none
127132
*/
128133
static void test_function_2() {
129-
std::vector<std::string> input = {"100", "200", "+", "2", "/",
130-
"5", "*", "7", "+"};
134+
std::array<std::string, 9> input = {"100", "200", "+", "2", "/",
135+
"5", "*", "7", "+"};
131136
float answer = others::postfix_expression::postfix_evaluation(input);
132137

133138
assert(answer == 757);
134139
}
135140

136141
static void test_function_3() {
137-
std::vector<std::string> input = {
142+
std::array<std::string, 43> input = {
138143
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
139144
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
140145
"+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",

0 commit comments

Comments
 (0)