Skip to content

Instantly share code, notes, and snippets.

@PoSayDone
Created November 22, 2023 11:12
Show Gist options
  • Save PoSayDone/8cf03cd187b665d545d97a1f2a8c2308 to your computer and use it in GitHub Desktop.
Save PoSayDone/8cf03cd187b665d545d97a1f2a8c2308 to your computer and use it in GitHub Desktop.
эксептн
#include <iostream>
#include <stdexcept>
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<typename T>
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<unsigned int>(0, 9999, "Предыдущий месяц");
currentMonth = checkValue<unsigned int>(0, 9999, "Текущий месяц");
kwhPrice = checkValue<float>(0, 10000, "Цена за киловатт-час");
const float result = calculateSum(currentMonth, previousMonth, kwhPrice);
cout << "Вы заплатите: " << result << endl;
}
catch (const string& errorMessage) {
cout << "Ошибка: " << errorMessage << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment