|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <string> |
| 4 | +#include <memory> |
| 5 | + |
| 6 | +/** |
| 7 | + * @class Calculator |
| 8 | + * @brief A simple calculator class with basic arithmetic operations |
| 9 | + */ |
| 10 | +class Calculator { |
| 11 | +private: |
| 12 | + std::vector<double> history; |
| 13 | + |
| 14 | +public: |
| 15 | + /** |
| 16 | + * @brief Add two numbers |
| 17 | + * @param a First number |
| 18 | + * @param b Second number |
| 19 | + * @return Sum of a and b |
| 20 | + */ |
| 21 | + double add(double a, double b) { |
| 22 | + double result = a + b; |
| 23 | + history.push_back(result); |
| 24 | + return result; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @brief Subtract two numbers |
| 29 | + * @param a First number |
| 30 | + * @param b Second number |
| 31 | + * @return Difference of a and b |
| 32 | + */ |
| 33 | + double subtract(double a, double b) { |
| 34 | + double result = a - b; |
| 35 | + history.push_back(result); |
| 36 | + return result; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @brief Multiply two numbers |
| 41 | + * @param a First number |
| 42 | + * @param b Second number |
| 43 | + * @return Product of a and b |
| 44 | + */ |
| 45 | + double multiply(double a, double b) { |
| 46 | + double result = a * b; |
| 47 | + history.push_back(result); |
| 48 | + return result; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @brief Divide two numbers |
| 53 | + * @param a Dividend |
| 54 | + * @param b Divisor |
| 55 | + * @return Quotient of a and b |
| 56 | + * @throws std::invalid_argument if divisor is zero |
| 57 | + */ |
| 58 | + double divide(double a, double b) { |
| 59 | + if (b == 0) { |
| 60 | + throw std::invalid_argument("Division by zero is not allowed"); |
| 61 | + } |
| 62 | + double result = a / b; |
| 63 | + history.push_back(result); |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @brief Get the calculation history |
| 69 | + * @return Vector containing all previous results |
| 70 | + */ |
| 71 | + const std::vector<double>& getHistory() const { |
| 72 | + return history; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @brief Clear the calculation history |
| 77 | + */ |
| 78 | + void clearHistory() { |
| 79 | + history.clear(); |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +/** |
| 84 | + * @brief Template function to find maximum of two values |
| 85 | + * @tparam T Type of the values |
| 86 | + * @param a First value |
| 87 | + * @param b Second value |
| 88 | + * @return Maximum of a and b |
| 89 | + */ |
| 90 | +template<typename T> |
| 91 | +T findMax(T a, T b) { |
| 92 | + return (a > b) ? a : b; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * @brief Main function to demonstrate the Calculator class |
| 97 | + */ |
| 98 | +int main() { |
| 99 | + auto calculator = std::make_unique<Calculator>(); |
| 100 | + |
| 101 | + std::cout << "Testing Calculator class:" << std::endl; |
| 102 | + |
| 103 | + try { |
| 104 | + double result1 = calculator->add(10.5, 5.3); |
| 105 | + std::cout << "10.5 + 5.3 = " << result1 << std::endl; |
| 106 | + |
| 107 | + double result2 = calculator->multiply(4.0, 3.0); |
| 108 | + std::cout << "4.0 * 3.0 = " << result2 << std::endl; |
| 109 | + |
| 110 | + double result3 = calculator->divide(15.0, 3.0); |
| 111 | + std::cout << "15.0 / 3.0 = " << result3 << std::endl; |
| 112 | + |
| 113 | + // Test template function |
| 114 | + int maxInt = findMax(42, 37); |
| 115 | + std::cout << "Max of 42 and 37: " << maxInt << std::endl; |
| 116 | + |
| 117 | + // Display history |
| 118 | + std::cout << "\nCalculation history:" << std::endl; |
| 119 | + const auto& history = calculator->getHistory(); |
| 120 | + for (size_t i = 0; i < history.size(); ++i) { |
| 121 | + std::cout << "Result " << (i + 1) << ": " << history[i] << std::endl; |
| 122 | + } |
| 123 | + |
| 124 | + } catch (const std::exception& e) { |
| 125 | + std::cerr << "Error: " << e.what() << std::endl; |
| 126 | + } |
| 127 | + |
| 128 | + return 0; |
| 129 | +} |
0 commit comments