38 return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
49void evaluate(
float a,
float b,
const std::string &operation,
50 std::stack<float> &
stack) {
52 const char *op = operation.c_str();
75 std::cout <<
"Operator not defined\n";
81float remove_from_stack(std::stack<float> &
stack) {
83 throw std::invalid_argument(
"Not enough operands");
98 std::stack<float>
stack;
100 for (
const auto &scan : input) {
105 const auto op2 = remove_from_stack(
stack);
106 const auto op1 = remove_from_stack(
stack);
112 const auto res = remove_from_stack(
stack);
113 if (!
stack.empty()) {
114 throw std::invalid_argument(
"Too many operands");
127 std::vector<std::string> input = {
"2",
"3",
"1",
"*",
"+",
"9",
"-"};
131 assert(answer == -4);
140 std::vector<std::string> input = {
"100",
"200",
"+",
"2",
"/",
144 assert(answer == 757);
147static void test_function_3() {
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 "+",
"+",
"+",
"+",
"+",
"+",
"+",
"+",
"+",
"+"};
155 assert(answer == 22);
158static void test_single_input() {
159 std::vector<std::string> input = {
"1"};
165static void test_not_enough_operands() {
166 std::vector<std::string> input = {
"+"};
170 }
catch (std::invalid_argument &) {
176static void test_not_enough_operands_empty_input() {
177 std::vector<std::string> input = {};
181 }
catch (std::invalid_argument &) {
187static void test_too_many_operands() {
188 std::vector<std::string> input = {
"1",
"2"};
192 }
catch (std::invalid_argument &) {
207 test_not_enough_operands();
208 test_not_enough_operands_empty_input();
209 test_too_many_operands();
211 std::cout <<
"\nTest implementations passed!\n";
for std::invalid_argument
void push(const value_type &item)
Functions for Postfix Expression algorithm.
void evaluate(float a, float b, const std::string &operation, std::stack< float > &stack)
Evaluate answer using given last two operands from and operation.
bool is_number(const std::string &s)
Checks if scanned string is a number.
float postfix_evaluation(const std::vector< std::string > &input)
Postfix Evaluation algorithm to compute the value from given input array.
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', '-'}.