11/* *
22 * @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)
54 * @author [Darshana Sarma](https://github.com/Darshana-Sarma)
65 * @details
76 * Create a stack to store operands (or values).
1211 * When the expression is ended, the number in the stack is the final answer
1312 */
1413#include < algorithm> // for all_of
15- #include < array> // for array
14+ #include < array> // for std:: array
1615#include < cassert> // for assert
1716#include < iostream> // for io operations
18- #include < stack> // for std::stack
1917#include < string> // for stof
2018
2119/* *
@@ -28,6 +26,36 @@ namespace others {
2826 * @brief Functions for Postfix Expression algorithm
2927 */
3028namespace 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+ }
3159
3260/* *
3361 * @brief Checks if scanned string is a number
@@ -46,29 +74,28 @@ bool is_number(const std::string &s) {
4674 * @param stack containing numbers
4775 * @returns none
4876 */
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) {
5178 float c = 0 ;
5279 const char *op = operation.c_str ();
5380 switch (*op) {
5481 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 );
5784 break ;
5885
5986 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 );
6289 break ;
6390
6491 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 );
6794 break ;
6895
6996 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 );
7299 break ;
73100
74101 default :
@@ -86,32 +113,31 @@ void evaluate(float a, float b, const std::string &operation,
86113 */
87114template <std::size_t N>
88115float postfix_evaluation (std::array<std::string, N> input) {
89- std::stack< float > stack;
116+ Stack stack;
90117 int j = 0 ;
91118
92119 while (j < N) {
93120 std::string scan = input[j];
94121 if (is_number (scan)) {
95- stack. push (std::stof (scan));
122+ push (std::stof (scan), &stack );
96123
97124 } 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);
102127
103- evaluate (op1, op2, scan, stack);
128+ evaluate (op1, op2, scan, & stack);
104129 }
105130 j++;
106131 }
107132
108- std::cout << stack.top () << " \n " ;
133+ std::cout << stack.stack [stack. stackTop ] << " \n " ;
109134
110- return stack.top () ;
135+ return stack.stack [stack. stackTop ] ;
111136}
112137} // namespace postfix_expression
113138} // namespace others
114139
140+
115141/* *
116142 * @brief Test function 1 with input array
117143 * {'2', '3', '1', '*', '+', '9', '-'}
@@ -127,7 +153,7 @@ static void test_function_1() {
127153
128154/* *
129155 * @brief Test function 2 with input array
130- * {'100 ', '200 ', '+', '2', '/', '5', '*', '7', '+'}
156+ * {'1 ', '2 ', '+', '2', '/', '5', '*', '7', '+'}
131157 * @returns none
132158 */
133159static void test_function_2 () {
@@ -138,25 +164,13 @@ static void test_function_2() {
138164 assert (answer == 757 );
139165}
140166
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-
152167/* *
153168 * @brief Main function
154169 * @returns 0 on exit
155170 */
156171int main () {
157172 test_function_1 ();
158173 test_function_2 ();
159- test_function_3 ();
160174
161175 std::cout << " \n Test implementations passed!\n " ;
162176
0 commit comments