Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions others/postfix_evaluation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
* When the expression is ended, the number in the stack is the final answer
*/
#include <algorithm> // for all_of
#include <array> // for array
#include <cassert> // for assert
#include <iostream> // for io operations
#include <stack> // for std::stack
#include <string> // for stof
#include <vector> // for std::vector

/**
* @namespace others
Expand Down Expand Up @@ -80,17 +80,13 @@ void evaluate(float a, float b, const std::string &operation,
/**
* @brief Postfix Evaluation algorithm to compute the value from given input
* array
* @tparam N number of array size
* @param input Array of characters consisting of numbers and operations
* @param input vector of strings consisting of numbers and operations
* @returns stack[stackTop] returns the top value from the stack
*/
template <std::size_t N>
float postfix_evaluation(std::array<std::string, N> input) {
float postfix_evaluation(const std::vector<std::string> &input) {
std::stack<float> stack;
int j = 0;

while (j < N) {
std::string scan = input[j];
for (const auto &scan : input) {
if (is_number(scan)) {
stack.push(std::stof(scan));

Expand All @@ -102,7 +98,6 @@ float postfix_evaluation(std::array<std::string, N> input) {

evaluate(op1, op2, scan, stack);
}
j++;
}

std::cout << stack.top() << "\n";
Expand All @@ -118,7 +113,7 @@ float postfix_evaluation(std::array<std::string, N> input) {
* @returns none
*/
static void test_function_1() {
std::array<std::string, 7> input = {"2", "3", "1", "*", "+", "9", "-"};
std::vector<std::string> input = {"2", "3", "1", "*", "+", "9", "-"};

float answer = others::postfix_expression::postfix_evaluation(input);

Expand All @@ -131,15 +126,15 @@ static void test_function_1() {
* @returns none
*/
static void test_function_2() {
std::array<std::string, 9> input = {"100", "200", "+", "2", "/",
"5", "*", "7", "+"};
std::vector<std::string> input = {"100", "200", "+", "2", "/",
"5", "*", "7", "+"};
float answer = others::postfix_expression::postfix_evaluation(input);

assert(answer == 757);
}

static void test_function_3() {
std::array<std::string, 43> input = {
std::vector<std::string> input = {
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
Expand Down
Loading