feat: support compute last variable and result

This commit is contained in:
jfmcarreira committed 2024-03-13 00:08:21 +00:00
1 parent affe74097e
commit ed02a8bd1d
3 files changed
+27 -19

No files matched your search

+9 -3
View File
@@ -67,14 +67,20 @@ void Calculator::undoOperation(std::size_t count) noexcept
_operations.erase(_operations.end() - static_cast<std::int64_t>(count), _operations.end()); _operations.erase(_operations.end() - static_cast<std::int64_t>(count), _operations.end());
} }
auto Calculator::result() noexcept -> double auto Calculator::computeLast() noexcept -> std::pair<std::string, double>
{ {
Workspace workspace; Workspace workspace;
Visitor visitor{_operations, workspace}; Visitor visitor{_operations, workspace};
for (auto& operation : std::views::reverse(_operations)) { for (auto& operation : std::views::reverse(_operations)) {
auto result = compute(visitor, operation); auto result = compute(visitor, operation);
if (result.has_value()) return result.value(); if (result.has_value()) return std::make_pair(operation.variable, result.value());
} }
return 0.0; return std::make_pair("na", 0.0);
}
auto Calculator::computeLastValue() noexcept -> double
{
auto [_, result] = computeLast();
return result;
} }
+3 -1
View File
@@ -32,7 +32,9 @@ public:
void addOperation(std::string result, Operand lhs, Operand rhs, Operator binary_operator) noexcept; void addOperation(std::string result, Operand lhs, Operand rhs, Operator binary_operator) noexcept;
void undoOperation(std::size_t count) noexcept; void undoOperation(std::size_t count) noexcept;
auto result() noexcept -> double;
auto computeLast() noexcept -> std::pair<std::string, double>;
auto computeLastValue() noexcept -> double;
private: private:
std::vector<Operation> _operations; std::vector<Operation> _operations;
+15 -15
View File
@@ -11,13 +11,13 @@ TEST_CASE("Calculator - can add operations", "[calculator-challenge]")
Calculator calculator; Calculator calculator;
calculator.addOperation("a", IntegerOperand{5}, IntegerOperand{10}, addiction); calculator.addOperation("a", IntegerOperand{5}, IntegerOperand{10}, addiction);
CHECK(calculator.result() == 15.0); CHECK(calculator.computeLastValue() == 15.0);
calculator.addOperation("b", IntegerOperand{1}, IntegerOperand{5}, addiction); calculator.addOperation("b", IntegerOperand{1}, IntegerOperand{5}, addiction);
CHECK(calculator.result() == 6.0); CHECK(calculator.computeLastValue() == 6.0);
calculator.addOperation("c", IntegerOperand{1}, IntegerOperand{5}, subtraction); calculator.addOperation("c", IntegerOperand{1}, IntegerOperand{5}, subtraction);
CHECK(calculator.result() == -4.0); CHECK(calculator.computeLastValue() == -4.0);
} }
TEST_CASE("Calculator - can undo operations", "[calculator-challenge]") TEST_CASE("Calculator - can undo operations", "[calculator-challenge]")
@@ -25,29 +25,29 @@ TEST_CASE("Calculator - can undo operations", "[calculator-challenge]")
Calculator calculator; Calculator calculator;
calculator.addOperation("a", IntegerOperand{5}, IntegerOperand{10}, addiction); calculator.addOperation("a", IntegerOperand{5}, IntegerOperand{10}, addiction);
CHECK(calculator.result() == 15.0); CHECK(calculator.computeLastValue() == 15.0);
calculator.addOperation("b", IntegerOperand{1}, IntegerOperand{5}, subtraction); calculator.addOperation("b", IntegerOperand{1}, IntegerOperand{5}, subtraction);
CHECK(calculator.result() == -4.0); CHECK(calculator.computeLastValue() == -4.0);
SECTION("one by one") SECTION("one by one")
{ {
calculator.undoOperation(1); calculator.undoOperation(1);
CHECK(calculator.result() == 15.0); CHECK(calculator.computeLastValue() == 15.0);
calculator.undoOperation(1); calculator.undoOperation(1);
CHECK(calculator.result() == 0.0); CHECK(calculator.computeLastValue() == 0.0);
} }
SECTION("all") SECTION("all")
{ {
calculator.undoOperation(2); calculator.undoOperation(2);
CHECK(calculator.result() == 0.0); CHECK(calculator.computeLastValue() == 0.0);
} }
SECTION("more than was added") SECTION("more than was added")
{ {
calculator.undoOperation(50); calculator.undoOperation(50);
CHECK(calculator.result() == 0.0); CHECK(calculator.computeLastValue() == 0.0);
} }
} }
@@ -56,13 +56,13 @@ TEST_CASE("Calculator - can add operations with variables", "[calculator-challen
Calculator calculator; Calculator calculator;
calculator.addOperation("a", IntegerOperand{1}, IntegerOperand{2}, addiction); calculator.addOperation("a", IntegerOperand{1}, IntegerOperand{2}, addiction);
CHECK(calculator.result() == 3); CHECK(calculator.computeLastValue() == 3);
calculator.addOperation("b", VariableOperand{"a"}, IntegerOperand{10}, addiction); calculator.addOperation("b", VariableOperand{"a"}, IntegerOperand{10}, addiction);
CHECK(calculator.result() == 13); CHECK(calculator.computeLastValue() == 13);
calculator.addOperation("b", VariableOperand{"a"}, IntegerOperand{2}, addiction); calculator.addOperation("b", VariableOperand{"a"}, IntegerOperand{2}, addiction);
CHECK(calculator.result() == 5); CHECK(calculator.computeLastValue() == 5);
} }
TEST_CASE("Calculator - if a variable is missing the last available is returned", "[calculator-challenge]") TEST_CASE("Calculator - if a variable is missing the last available is returned", "[calculator-challenge]")
@@ -70,11 +70,11 @@ TEST_CASE("Calculator - if a variable is missing the last available is returned"
Calculator calculator; Calculator calculator;
calculator.addOperation("a", VariableOperand{"c"}, IntegerOperand{10}, addiction); calculator.addOperation("a", VariableOperand{"c"}, IntegerOperand{10}, addiction);
CHECK(calculator.result() == 0.0); CHECK(calculator.computeLastValue() == 0.0);
calculator.addOperation("a", IntegerOperand{1}, IntegerOperand{2}, addiction); calculator.addOperation("a", IntegerOperand{1}, IntegerOperand{2}, addiction);
CHECK(calculator.result() == 3); CHECK(calculator.computeLastValue() == 3);
calculator.addOperation("a", VariableOperand{"c"}, IntegerOperand{10}, addiction); calculator.addOperation("a", VariableOperand{"c"}, IntegerOperand{10}, addiction);
CHECK(calculator.result() == 3.0); CHECK(calculator.computeLastValue() == 3.0);
} }