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 <cassert>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
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.
float others::postfix_expression::postfix_evaluation (const std::vector< std::string > &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 151 of file postfix_evaluation.cpp.

151 {
154 test_function_3();
155
156 std::cout << "\nTest implementations passed!\n";
157
158 return 0;
159}
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()

float others::postfix_expression::postfix_evaluation ( const std::vector< std::string > & input)

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

Parameters
inputvector of strings consisting of numbers and operations
Returns
stack[stackTop] returns the top value from the stack

Definition at line 86 of file postfix_evaluation.cpp.

86 {
87 std::stack<float> stack;
88
89 for (const auto &scan : input) {
90 if (is_number(scan)) {
91 stack.push(std::stof(scan));
92
93 } else {
94 const float op2 = stack.top();
95 stack.pop();
96 const float op1 = stack.top();
97 stack.pop();
98
99 evaluate(op1, op2, scan, stack);
100 }
101 }
102
103 std::cout << stack.top() << "\n";
104
105 return stack.top();
106}
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 115 of file postfix_evaluation.cpp.

115 {
116 std::vector<std::string> input = {"2", "3", "1", "*", "+", "9", "-"};
117
119
120 assert(answer == -4);
121}
float postfix_evaluation(const std::vector< std::string > &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 128 of file postfix_evaluation.cpp.

128 {
129 std::vector<std::string> input = {"100", "200", "+", "2", "/",
130 "5", "*", "7", "+"};
132
133 assert(answer == 757);
134}

◆ test_function_3()

void test_function_3 ( )
static

Definition at line 136 of file postfix_evaluation.cpp.

136 {
137 std::vector<std::string> input = {
138 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
139 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
140 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
141 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+"};
143
144 assert(answer == 22);
145}