From ed02a8bd1d25747f7a715f01b4116a5d3c8b9a5b Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Wed, 13 Mar 2024 00:08:21 +0000 Subject: [PATCH] feat: support compute last variable and result --- Calculator.cpp | 12 +++++++++--- Calculator.hpp | 4 +++- CalculatorTests.cpp | 30 +++++++++++++++--------------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/Calculator.cpp b/Calculator.cpp index d8f15f6..5a3e521 100644 --- a/Calculator.cpp +++ b/Calculator.cpp @@ -67,14 +67,20 @@ void Calculator::undoOperation(std::size_t count) noexcept _operations.erase(_operations.end() - static_cast(count), _operations.end()); } -auto Calculator::result() noexcept -> double +auto Calculator::computeLast() noexcept -> std::pair { Workspace workspace; Visitor visitor{_operations, workspace}; for (auto& operation : std::views::reverse(_operations)) { 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; } diff --git a/Calculator.hpp b/Calculator.hpp index 585d477..29004dd 100644 --- a/Calculator.hpp +++ b/Calculator.hpp @@ -32,7 +32,9 @@ public: void addOperation(std::string result, Operand lhs, Operand rhs, Operator binary_operator) noexcept; void undoOperation(std::size_t count) noexcept; - auto result() noexcept -> double; + + auto computeLast() noexcept -> std::pair; + auto computeLastValue() noexcept -> double; private: std::vector _operations; diff --git a/CalculatorTests.cpp b/CalculatorTests.cpp index f43ef40..89116fb 100644 --- a/CalculatorTests.cpp +++ b/CalculatorTests.cpp @@ -11,13 +11,13 @@ TEST_CASE("Calculator - can add operations", "[calculator-challenge]") Calculator calculator; 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); - CHECK(calculator.result() == 6.0); + CHECK(calculator.computeLastValue() == 6.0); 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]") @@ -25,29 +25,29 @@ TEST_CASE("Calculator - can undo operations", "[calculator-challenge]") Calculator calculator; 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); - CHECK(calculator.result() == -4.0); + CHECK(calculator.computeLastValue() == -4.0); SECTION("one by one") { calculator.undoOperation(1); - CHECK(calculator.result() == 15.0); + CHECK(calculator.computeLastValue() == 15.0); calculator.undoOperation(1); - CHECK(calculator.result() == 0.0); + CHECK(calculator.computeLastValue() == 0.0); } SECTION("all") { calculator.undoOperation(2); - CHECK(calculator.result() == 0.0); + CHECK(calculator.computeLastValue() == 0.0); } SECTION("more than was added") { 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.addOperation("a", IntegerOperand{1}, IntegerOperand{2}, addiction); - CHECK(calculator.result() == 3); + CHECK(calculator.computeLastValue() == 3); calculator.addOperation("b", VariableOperand{"a"}, IntegerOperand{10}, addiction); - CHECK(calculator.result() == 13); + CHECK(calculator.computeLastValue() == 13); 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]") @@ -70,11 +70,11 @@ TEST_CASE("Calculator - if a variable is missing the last available is returned" Calculator calculator; 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); - CHECK(calculator.result() == 3); + CHECK(calculator.computeLastValue() == 3); calculator.addOperation("a", VariableOperand{"c"}, IntegerOperand{10}, addiction); - CHECK(calculator.result() == 3.0); + CHECK(calculator.computeLastValue() == 3.0); }