Skip to content

Commit 8c0db63

Browse files
committed
fix: remove potential segv from postfix_evaluation.cpp
1 parent 43ce636 commit 8c0db63

File tree

1 file changed

+36
-50
lines changed

1 file changed

+36
-50
lines changed

others/postfix_evaluation.cpp

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @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)
45
* @author [Darshana Sarma](https://github.com/Darshana-Sarma)
56
* @details
67
* Create a stack to store operands (or values).
@@ -11,9 +12,10 @@
1112
* When the expression is ended, the number in the stack is the final answer
1213
*/
1314
#include <algorithm> // for all_of
14-
#include <array> // for std::array
15+
#include <array> // for array
1516
#include <cassert> // for assert
1617
#include <iostream> // for io operations
18+
#include <stack> // for std::stack
1719
#include <string> // for stof
1820

1921
/**
@@ -26,36 +28,6 @@ namespace others {
2628
* @brief Functions for Postfix Expression algorithm
2729
*/
2830
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-
}
5931

6032
/**
6133
* @brief Checks if scanned string is a number
@@ -74,28 +46,29 @@ bool is_number(const std::string &s) {
7446
* @param stack containing numbers
7547
* @returns none
7648
*/
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) {
7851
float c = 0;
7952
const char *op = operation.c_str();
8053
switch (*op) {
8154
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);
8457
break;
8558

8659
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);
8962
break;
9063

9164
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);
9467
break;
9568

9669
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);
9972
break;
10073

10174
default:
@@ -113,31 +86,32 @@ void evaluate(float a, float b, const std::string &operation, Stack *stack) {
11386
*/
11487
template <std::size_t N>
11588
float postfix_evaluation(std::array<std::string, N> input) {
116-
Stack stack;
89+
std::stack<float> stack;
11790
int j = 0;
11891

11992
while (j < N) {
12093
std::string scan = input[j];
12194
if (is_number(scan)) {
122-
push(std::stof(scan), &stack);
95+
stack.push(std::stof(scan));
12396

12497
} 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();
127102

128-
evaluate(op1, op2, scan, &stack);
103+
evaluate(op1, op2, scan, stack);
129104
}
130105
j++;
131106
}
132107

133-
std::cout << stack.stack[stack.stackTop] << "\n";
108+
std::cout << stack.top() << "\n";
134109

135-
return stack.stack[stack.stackTop];
110+
return stack.top();
136111
}
137112
} // namespace postfix_expression
138113
} // namespace others
139114

140-
141115
/**
142116
* @brief Test function 1 with input array
143117
* {'2', '3', '1', '*', '+', '9', '-'}
@@ -153,7 +127,7 @@ static void test_function_1() {
153127

154128
/**
155129
* @brief Test function 2 with input array
156-
* {'1', '2', '+', '2', '/', '5', '*', '7', '+'}
130+
* {'100', '200', '+', '2', '/', '5', '*', '7', '+'}
157131
* @returns none
158132
*/
159133
static void test_function_2() {
@@ -164,13 +138,25 @@ static void test_function_2() {
164138
assert(answer == 757);
165139
}
166140

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+
167152
/**
168153
* @brief Main function
169154
* @returns 0 on exit
170155
*/
171156
int main() {
172157
test_function_1();
173158
test_function_2();
159+
test_function_3();
174160

175161
std::cout << "\nTest implementations passed!\n";
176162

0 commit comments

Comments
 (0)