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 <stack>
#include <string>
Include dependency graph for postfix_evaluation.cpp:

Go to the source code of this file.

Namespaces

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

Functions

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, std::stack< float > &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 {'100', '200', '+', '2', '/', '5', '*', '7', '+'}.
static void test_function_3 ()
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,
std::stack< float > & 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 49 of file postfix_evaluation.cpp.

50 {
51 float c = 0;
52 const char *op = operation.c_str();
53 switch (*op) {
54 case '+':
55 c = a + b; // Addition of numbers
56 stack.push(c);
57 break;
58
59 case '-':
60 c = a - b; // Subtraction of numbers
61 stack.push(c);
62 break;
63
64 case '*':
65 c = a * b; // Multiplication of numbers
66 stack.push(c);
67 break;
68
69 case '/':
70 c = a / b; // Division of numbers
71 stack.push(c);
72 break;
73
74 default:
75 std::cout << "Operator not defined\n";
76 break;
77 }
78}
for std::invalid_argument
Definition stack.hpp:19
void push(const value_type &item)
Definition stack.hpp:47

◆ 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 37 of file postfix_evaluation.cpp.

37 {
38 return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
39}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 156 of file postfix_evaluation.cpp.

156 {
159 test_function_3();
160
161 std::cout << "\nTest implementations passed!\n";
162
163 return 0;
164}
static void test_function_2()
Test function 2 with input array {'100', '200', '+', '2', '/', '5', '*', '7', '+'}...
static void test_function_1()
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.

◆ 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 88 of file postfix_evaluation.cpp.

88 {
89 std::stack<float> stack;
90 int j = 0;
91
92 while (j < N) {
93 std::string scan = input[j];
94 if (is_number(scan)) {
95 stack.push(std::stof(scan));
96
97 } else {
98 const float op2 = stack.top();
99 stack.pop();
100 const float op1 = stack.top();
101 stack.pop();
102
103 evaluate(op1, op2, scan, stack);
104 }
105 j++;
106 }
107
108 std::cout << stack.top() << "\n";
109
110 return stack.top();
111}
void pop()
Definition stack.hpp:62
value_type top() const
Definition stack.hpp:56
char stack[MAX]
bool is_number(const std::string &s)
Checks if scanned string is a number.

◆ test_function_1()

void test_function_1 ( )
static

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

Returns
none

Definition at line 120 of file postfix_evaluation.cpp.

120 {
121 std::array<std::string, 7> input = {"2", "3", "1", "*", "+", "9", "-"};
122
124
125 assert(answer == -4);
126}
float postfix_evaluation(std::array< std::string, N > input)
Postfix Evaluation algorithm to compute the value from given input array.

◆ test_function_2()

void test_function_2 ( )
static

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

Returns
none

Definition at line 133 of file postfix_evaluation.cpp.

133 {
134 std::array<std::string, 9> input = {"100", "200", "+", "2", "/",
135 "5", "*", "7", "+"};
137
138 assert(answer == 757);
139}

◆ test_function_3()

void test_function_3 ( )
static

Definition at line 141 of file postfix_evaluation.cpp.

141 {
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 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+"};
148
149 assert(answer == 22);
150}