TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
postfix_evaluation.cpp File Reference

Evaluation of Postfix Expression More...

#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <string>
Include dependency graph for postfix_evaluation.cpp:

Go to the source code of this file.

Classes

class  others::postfix_expression::Stack
 Creates an array to be used as stack for storing values. More...
 

Namespaces

namespace  others
 for vector
 
namespace  postfix_expression
 Functions for Postfix Expression algorithm.
 

Functions

void others::postfix_expression::push (float operand, Stack *stack)
 Pushing operand, also called the number in the array to the stack.
 
float others::postfix_expression::pop (Stack *stack)
 Popping operand, also called the number from the stack.
 
bool others::postfix_expression::is_number (const std::string &s)
 Checks if scanned string is a number.
 
void others::postfix_expression::evaluate (float a, float b, const std::string &operation, Stack *stack)
 Evaluate answer using given last two operands from and operation.
 
template<std::size_t N>
float others::postfix_expression::postfix_evaluation (std::array< std::string, N > input)
 Postfix Evaluation algorithm to compute the value from given input array.
 
static void test_function_1 ()
 Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
 
static void test_function_2 ()
 Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.
 
int main ()
 Main function.
 

Detailed Description

Evaluation of Postfix Expression

Author
Darshana Sarma

Create a stack to store operands (or values). Scan the given expression and do following for every scanned element. If the element is a number, push it into the stack If the element is a operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack When the expression is ended, the number in the stack is the final answer

Definition in file postfix_evaluation.cpp.

Function Documentation

◆ evaluate()

void others::postfix_expression::evaluate ( float a,
float b,
const std::string & operation,
Stack * stack )

Evaluate answer using given last two operands from and operation.

Parameters
asecond last added operand which will be used for evaluation
blast added operand which will be used for evaluation
operationto be performed with respective floats
stackcontaining numbers
Returns
none

Definition at line 77 of file postfix_evaluation.cpp.

77 {
78 float c = 0;
79 const char *op = operation.c_str();
80 switch (*op) {
81 case '+':
82 c = a + b; // Addition of numbers
84 break;
85
86 case '-':
87 c = a - b; // Subtraction of numbers
89 break;
90
91 case '*':
92 c = a * b; // Multiplication of numbers
94 break;
95
96 case '/':
97 c = a / b; // Division of numbers
99 break;
100
101 default:
102 std::cout << "Operator not defined\n";
103 break;
104 }
105}
for std::invalid_argument
Definition stack.hpp:19
void push(float operand, Stack *stack)
Pushing operand, also called the number in the array to the stack.

◆ is_number()

bool others::postfix_expression::is_number ( const std::string & s)

Checks if scanned string is a number.

Parameters
sscanned string
Returns
bool boolean value if string is number

Definition at line 65 of file postfix_evaluation.cpp.

65 {
66 return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
67}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 171 of file postfix_evaluation.cpp.

171 {
174
175 std::cout << "\nTest implementations passed!\n";
176
177 return 0;
178}
static void test_function_2()
Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.
static void test_function_1()
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.

◆ pop()

float others::postfix_expression::pop ( Stack * stack)

Popping operand, also called the number from the stack.

Parameters
stackstack containing numbers
Returns
operand float on top of stack

Definition at line 54 of file postfix_evaluation.cpp.

54 {
55 float operand = stack->stack[stack->stackTop];
56 stack->stackTop--;
57 return operand;
58}
std::shared_ptr< Node< value_type > > stackTop
Definition stack.hpp:75

◆ postfix_evaluation()

template<std::size_t N>
float others::postfix_expression::postfix_evaluation ( std::array< std::string, N > input)

Postfix Evaluation algorithm to compute the value from given input array.

Template Parameters
Nnumber of array size
Parameters
inputArray of characters consisting of numbers and operations
Returns
stack[stackTop] returns the top value from the stack

Definition at line 115 of file postfix_evaluation.cpp.

115 {
116 Stack stack;
117 int j = 0;
118
119 while (j < N) {
120 std::string scan = input[j];
121 if (is_number(scan)) {
122 push(std::stof(scan), &stack);
123
124 } else {
125 float op2 = pop(&stack);
126 float op1 = pop(&stack);
127
128 evaluate(op1, op2, scan, &stack);
129 }
130 j++;
131 }
132
133 std::cout << stack.stack[stack.stackTop] << "\n";
134
135 return stack.stack[stack.stackTop];
136}
bool is_number(const T &input)
Utility function to verify if the given input is a number or not. This is very useful to prevent the ...
char stack[MAX]
void evaluate(float a, float b, const std::string &operation, Stack *stack)
Evaluate answer using given last two operands from and operation.

◆ push()

void others::postfix_expression::push ( float operand,
Stack * stack )

Pushing operand, also called the number in the array to the stack.

Parameters
operandfloat value from the input array or evaluation
stackstack containing numbers
Returns
none

Definition at line 44 of file postfix_evaluation.cpp.

44 {
45 stack->stackTop++;
46 stack->stack[stack->stackTop] = operand;
47}

◆ test_function_1()

static void test_function_1 ( )
static

Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.

Returns
none

Definition at line 146 of file postfix_evaluation.cpp.

146 {
147 std::array<std::string, 7> input = {"2", "3", "1", "*", "+", "9", "-"};
148
150
151 assert(answer == -4);
152}
float postfix_evaluation(std::array< std::string, N > input)
Postfix Evaluation algorithm to compute the value from given input array.

◆ test_function_2()

static void test_function_2 ( )
static

Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.

Returns
none

Definition at line 159 of file postfix_evaluation.cpp.

159 {
160 std::array<std::string, 9> input = {"100", "200", "+", "2", "/",
161 "5", "*", "7", "+"};
163
164 assert(answer == 757);
165}