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
@@ -80,17 +80,13 @@ void evaluate(float a, float b, const std::string &operation,
80
80
/* *
81
81
* @brief Postfix Evaluation algorithm to compute the value from given input
82
82
* 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
85
84
* @returns stack[stackTop] returns the top value from the stack
86
85
*/
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) {
89
87
std::stack<float > stack;
90
- int j = 0 ;
91
88
92
- while (j < N) {
93
- std::string scan = input[j];
89
+ for (const auto &scan : input) {
94
90
if (is_number (scan)) {
95
91
stack.push (std::stof (scan));
96
92
@@ -102,7 +98,6 @@ float postfix_evaluation(std::array<std::string, N> input) {
102
98
103
99
evaluate (op1, op2, scan, stack);
104
100
}
105
- j++;
106
101
}
107
102
108
103
std::cout << stack.top () << " \n " ;
@@ -118,7 +113,7 @@ float postfix_evaluation(std::array<std::string, N> input) {
118
113
* @returns none
119
114
*/
120
115
static 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" , " -" };
122
117
123
118
float answer = others::postfix_expression::postfix_evaluation (input);
124
119
@@ -131,15 +126,15 @@ static void test_function_1() {
131
126
* @returns none
132
127
*/
133
128
static 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" , " +" };
136
131
float answer = others::postfix_expression::postfix_evaluation (input);
137
132
138
133
assert (answer == 757 );
139
134
}
140
135
141
136
static void test_function_3 () {
142
- std::array <std::string, 43 > input = {
137
+ std::vector <std::string> input = {
143
138
" 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" ,
144
139
" 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" ,
145
140
" +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" ,
0 commit comments