From f52e431ea07c91f59e9f6878f107b32408f891b4 Mon Sep 17 00:00:00 2001 From: Latt3x Date: Thu, 23 Jul 2026 19:28:46 +0300 Subject: [PATCH] Add files via upload --- calcnew.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 calcnew.cpp diff --git a/calcnew.cpp b/calcnew.cpp new file mode 100644 index 0000000..942cad3 --- /dev/null +++ b/calcnew.cpp @@ -0,0 +1,62 @@ +#include +#include + +class Calculator { +private: + int currentValue; + +public: + + Calculator(int initialValue = 0) : currentValue(initialValue) {} + + + static int calculate(const int& value1, const int& value2, const char& operation) { + switch (operation) { + case '+': return value1 + value2; + case '-': return value1 - value2; + case '*': return value1 * value2; + case '/': + if (value2 == 0) { + throw std::invalid_argument("Error: Division by zero!"); + } + return value1 / value2; + default: + throw std::invalid_argument("Error: Unknown operation!"); + } + } + + Calculator& calculate(const int& value, const char& operation) { + currentValue = calculate(currentValue, value, operation); + return *this; + } + + + int GetCurrentValue() const { + return currentValue; + } + + + Calculator& Reset(int newValue = 0) { + currentValue = newValue; + return *this; + } +}; +int main() { + + int staticResult = Calculator::calculate(10, 10, '+'); + std::cout << "Static result: " << staticResult << std::endl; + + + Calculator my_calc; + + + int chainResult = my_calc.calculate(2, '+') + .calculate(4, '+') + .calculate(5, '-') + .calculate(10, '*') + .GetCurrentValue(); + + std::cout << "Chain result: " << chainResult << std::endl; + + return 0; +} \ No newline at end of file