Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
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:

Classes

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

Namespaces

 others
 Other algorithms.
 
 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. More...
 
float others::postfix_expression::pop (Stack *stack)
 Popping operand, also called the number from the stack. More...
 
bool others::postfix_expression::is_number (const std::string &s)
 Checks if scanned string is a number. More...
 
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. More...
 
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. More...
 
static void test_function_1 ()
 Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}. More...
 
static void test_function_2 ()
 Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}. More...
 
int main ()
 Main function. More...
 

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

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
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 }
Here is the call graph for this function:

◆ 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
65  {
66  return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
67 }
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
171  {
172  test_function_1();
173  test_function_2();
174 
175  std::cout << "\nTest implementations passed!\n";
176 
177  return 0;
178 }
Here is the call graph for this function:

◆ 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
54  {
55  float operand = stack->stack[stack->stackTop];
56  stack->stackTop--;
57  return operand;
58 }
Here is the call graph for this function:

◆ 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
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 }
Here is the call graph for this function:

◆ 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
44  {
45  stack->stackTop++;
46  stack->stack[stack->stackTop] = operand;
47 }
Here is the call graph for this function:

◆ test_function_1()

static void test_function_1 ( )
static

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

Returns
none
146  {
147  std::array<std::string, 7> input = {"2", "3", "1", "*", "+", "9", "-"};
148 
150 
151  assert(answer == -4);
152 }

◆ test_function_2()

static void test_function_2 ( )
static

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

Returns
none
159  {
160  std::array<std::string, 9> input = {"100", "200", "+", "2", "/",
161  "5", "*", "7", "+"};
163 
164  assert(answer == 757);
165 }
std::string
STL class.
std::all_of
T all_of(T... args)
others::postfix_expression::is_number
bool is_number(const std::string &s)
Checks if scanned string is a number.
Definition: postfix_evaluation.cpp:65
test_function_2
static void test_function_2()
Test function 2 with input array {'1', '2', '+', '2', '/', '5', '*', '7', '+'}.
Definition: postfix_evaluation.cpp:159
std::cout
std::string::c_str
T c_str(T... args)
others::postfix_expression::postfix_evaluation
float postfix_evaluation(std::array< std::string, N > input)
Postfix Evaluation algorithm to compute the value from given input array.
Definition: postfix_evaluation.cpp:115
std::array
STL class.
test_function_1
static void test_function_1()
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
Definition: postfix_evaluation.cpp:146
others::postfix_expression::evaluate
void evaluate(float a, float b, const std::string &operation, Stack *stack)
Evaluate answer using given last two operands from and operation.
Definition: postfix_evaluation.cpp:77
std::stof
T stof(T... args)
stack::stack
stack()
Definition: stack.h:41
std::string::begin
T begin(T... args)
stack
Definition: stack.h:26
std::string::empty
T empty(T... args)
stack::stackTop
node< Type > * stackTop
Definition: stack.h:146
std::string::end
T end(T... args)
stack
char stack[MAX]
Definition: paranthesis_matching.cpp:20
push
void push(char ch)
push byte to stack variable
Definition: paranthesis_matching.cpp:26
pop
char pop()
pop a byte out of stack variable
Definition: paranthesis_matching.cpp:29