#include #include using namespace std; float calculateSum(unsigned int currentMonth, unsigned int previousMonth, float kwhPrice) { unsigned int delta; if (currentMonth < previousMonth) { delta = (9999 - previousMonth) + currentMonth; } else { delta = currentMonth - previousMonth; } return delta * kwhPrice; } template T checkValue(T lowerLimit, T upperLimit, const string& prompt) { T temp; cout << prompt << ": "; cin >> temp; if (cin.fail()) { cin.clear(); throw string("Некорректный тип данных!"); } else if (temp < lowerLimit || temp > upperLimit) { throw string("Некорректное значение! Значение должно быть больше " + to_string(lowerLimit) + " и меньше " + to_string(upperLimit) + "."); } else { return temp; } } int main() { unsigned int previousMonth, currentMonth; float kwhPrice; try { previousMonth = checkValue(0, 9999, "Предыдущий месяц"); currentMonth = checkValue(0, 9999, "Текущий месяц"); kwhPrice = checkValue(0, 10000, "Цена за киловатт-час"); const float result = calculateSum(currentMonth, previousMonth, kwhPrice); cout << "Вы заплатите: " << result << endl; } catch (const string& errorMessage) { cout << "Ошибка: " << errorMessage << endl; } return 0; }