1
1
/* *
2
2
* @file
3
- * @brief Evaluation of [Postfix Expression](https://en.wikipedia.org/wiki/Reverse_Polish_notation)
3
+ * @brief Evaluation of [Postfix
4
+ * Expression](https://en.wikipedia.org/wiki/Reverse_Polish_notation)
4
5
* @author [Darshana Sarma](https://github.com/Darshana-Sarma)
5
6
* @details
6
7
* Create a stack to store operands (or values).
11
12
* When the expression is ended, the number in the stack is the final answer
12
13
*/
13
14
#include < algorithm> // for all_of
14
- #include < array> // for std:: array
15
+ #include < array> // for array
15
16
#include < cassert> // for assert
16
17
#include < iostream> // for io operations
18
+ #include < stack> // for std::stack
17
19
#include < string> // for stof
18
20
19
21
/* *
@@ -26,36 +28,6 @@ namespace others {
26
28
* @brief Functions for Postfix Expression algorithm
27
29
*/
28
30
namespace postfix_expression {
29
- /* *
30
- * @brief Creates an array to be used as stack for storing values
31
- */
32
- class Stack {
33
- public:
34
- std::array<float , 20 > stack{}; // /< Array which will be used to store numbers in the input
35
- int stackTop = -1 ; // /< Represents the index of the last value added to array. -1 means array is empty
36
- };
37
-
38
- /* *
39
- * @brief Pushing operand, also called the number in the array to the stack
40
- * @param operand float value from the input array or evaluation
41
- * @param stack stack containing numbers
42
- * @returns none
43
- */
44
- void push (float operand, Stack *stack) {
45
- stack->stackTop ++;
46
- stack->stack [stack->stackTop ] = operand;
47
- }
48
-
49
- /* *
50
- * @brief Popping operand, also called the number from the stack
51
- * @param stack stack containing numbers
52
- * @returns operand float on top of stack
53
- */
54
- float pop (Stack *stack) {
55
- float operand = stack->stack [stack->stackTop ];
56
- stack->stackTop --;
57
- return operand;
58
- }
59
31
60
32
/* *
61
33
* @brief Checks if scanned string is a number
@@ -74,28 +46,29 @@ bool is_number(const std::string &s) {
74
46
* @param stack containing numbers
75
47
* @returns none
76
48
*/
77
- void evaluate (float a, float b, const std::string &operation, Stack *stack) {
49
+ void evaluate (float a, float b, const std::string &operation,
50
+ std::stack<float > &stack) {
78
51
float c = 0 ;
79
52
const char *op = operation.c_str ();
80
53
switch (*op) {
81
54
case ' +' :
82
- c = a + b; // Addition of numbers
83
- others::postfix_expression:: push (c, stack );
55
+ c = a + b; // Addition of numbers
56
+ stack. push (c);
84
57
break ;
85
58
86
59
case ' -' :
87
- c = a - b; // Subtraction of numbers
88
- others::postfix_expression:: push (c, stack );
60
+ c = a - b; // Subtraction of numbers
61
+ stack. push (c);
89
62
break ;
90
63
91
64
case ' *' :
92
- c = a * b; // Multiplication of numbers
93
- others::postfix_expression:: push (c, stack );
65
+ c = a * b; // Multiplication of numbers
66
+ stack. push (c);
94
67
break ;
95
68
96
69
case ' /' :
97
- c = a / b; // Division of numbers
98
- others::postfix_expression:: push (c, stack );
70
+ c = a / b; // Division of numbers
71
+ stack. push (c);
99
72
break ;
100
73
101
74
default :
@@ -113,31 +86,32 @@ void evaluate(float a, float b, const std::string &operation, Stack *stack) {
113
86
*/
114
87
template <std::size_t N>
115
88
float postfix_evaluation (std::array<std::string, N> input) {
116
- Stack stack;
89
+ std::stack< float > stack;
117
90
int j = 0 ;
118
91
119
92
while (j < N) {
120
93
std::string scan = input[j];
121
94
if (is_number (scan)) {
122
- push (std::stof (scan), &stack );
95
+ stack. push (std::stof (scan));
123
96
124
97
} else {
125
- float op2 = pop (&stack);
126
- float op1 = pop (&stack);
98
+ const float op2 = stack.top ();
99
+ stack.pop ();
100
+ const float op1 = stack.top ();
101
+ stack.pop ();
127
102
128
- evaluate (op1, op2, scan, & stack);
103
+ evaluate (op1, op2, scan, stack);
129
104
}
130
105
j++;
131
106
}
132
107
133
- std::cout << stack.stack [stack. stackTop ] << " \n " ;
108
+ std::cout << stack.top () << " \n " ;
134
109
135
- return stack.stack [stack. stackTop ] ;
110
+ return stack.top () ;
136
111
}
137
112
} // namespace postfix_expression
138
113
} // namespace others
139
114
140
-
141
115
/* *
142
116
* @brief Test function 1 with input array
143
117
* {'2', '3', '1', '*', '+', '9', '-'}
@@ -153,7 +127,7 @@ static void test_function_1() {
153
127
154
128
/* *
155
129
* @brief Test function 2 with input array
156
- * {'1 ', '2 ', '+', '2', '/', '5', '*', '7', '+'}
130
+ * {'100 ', '200 ', '+', '2', '/', '5', '*', '7', '+'}
157
131
* @returns none
158
132
*/
159
133
static void test_function_2 () {
@@ -164,13 +138,25 @@ static void test_function_2() {
164
138
assert (answer == 757 );
165
139
}
166
140
141
+ static void test_function_3 () {
142
+ std::array<std::string, 43 > input = {
143
+ " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" ,
144
+ " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" , " 1" ,
145
+ " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" ,
146
+ " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" , " +" };
147
+ float answer = others::postfix_expression::postfix_evaluation (input);
148
+
149
+ assert (answer == 22 );
150
+ }
151
+
167
152
/* *
168
153
* @brief Main function
169
154
* @returns 0 on exit
170
155
*/
171
156
int main () {
172
157
test_function_1 ();
173
158
test_function_2 ();
159
+ test_function_3 ();
174
160
175
161
std::cout << " \n Test implementations passed!\n " ;
176
162
0 commit comments