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 ()
static void test_single_input ()
static void test_not_enough_operands ()
static void test_not_enough_operands_empty_input ()
static void test_too_many_operands ()
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 202 of file postfix_evaluation.cpp.

202 {
205 test_function_3();
206 test_single_input();
207 test_not_enough_operands();
208 test_not_enough_operands_empty_input();
209 test_too_many_operands();
210
211 std::cout << "\nTest implementations passed!\n";
212
213 return 0;
214}
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 97 of file postfix_evaluation.cpp.

97 {
98 std::stack<float> stack;
99
100 for (const auto &scan : input) {
101 if (is_number(scan)) {
102 stack.push(std::stof(scan));
103
104 } else {
105 const auto op2 = remove_from_stack(stack);
106 const auto op1 = remove_from_stack(stack);
107
108 evaluate(op1, op2, scan, stack);
109 }
110 }
111
112 const auto res = remove_from_stack(stack);
113 if (!stack.empty()) {
114 throw std::invalid_argument("Too many operands");
115 }
116 return res;
117}
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 126 of file postfix_evaluation.cpp.

126 {
127 std::vector<std::string> input = {"2", "3", "1", "*", "+", "9", "-"};
128
130
131 assert(answer == -4);
132}
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 139 of file postfix_evaluation.cpp.

139 {
140 std::vector<std::string> input = {"100", "200", "+", "2", "/",
141 "5", "*", "7", "+"};
143
144 assert(answer == 757);
145}

◆ test_function_3()

void test_function_3 ( )
static

Definition at line 147 of file postfix_evaluation.cpp.

147 {
148 std::vector<std::string> input = {
149 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
150 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
151 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
152 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+"};
154
155 assert(answer == 22);
156}

◆ test_not_enough_operands()

void test_not_enough_operands ( )
static

Definition at line 165 of file postfix_evaluation.cpp.

165 {
166 std::vector<std::string> input = {"+"};
167 bool throws = false;
168 try {
170 } catch (std::invalid_argument &) {
171 throws = true;
172 }
173 assert(throws);
174}

◆ test_not_enough_operands_empty_input()

void test_not_enough_operands_empty_input ( )
static

Definition at line 176 of file postfix_evaluation.cpp.

176 {
177 std::vector<std::string> input = {};
178 bool throws = false;
179 try {
181 } catch (std::invalid_argument &) {
182 throws = true;
183 }
184 assert(throws);
185}

◆ test_single_input()

void test_single_input ( )
static

Definition at line 158 of file postfix_evaluation.cpp.

158 {
159 std::vector<std::string> input = {"1"};
161
162 assert(answer == 1);
163}

◆ test_too_many_operands()

void test_too_many_operands ( )
static

Definition at line 187 of file postfix_evaluation.cpp.

187 {
188 std::vector<std::string> input = {"1", "2"};
189 bool throws = false;
190 try {
192 } catch (std::invalid_argument &) {
193 throws = true;
194 }
195 assert(throws);
196}