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