Filage

oaip 1.1

Sep 13th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int TakeNum(const int MIN, const int MAX) {
  6.     const string ERROR_CHOISE = "Проверьте корректность введнных данных!\n";
  7.     bool isIncorrect;
  8.     int num;
  9.     do {
  10.         isIncorrect = false;
  11.         cin >> num;
  12.         if (cin.fail()) {
  13.             isIncorrect = true;
  14.             cout << ERROR_CHOISE;
  15.             cin.clear();
  16.             while (cin.get() != '\n');
  17.         }
  18.         if (!isIncorrect && cin.get() != '\n') {
  19.             cin.clear();
  20.             while (cin.get() != '\n');
  21.             cout << ERROR_CHOISE;
  22.             isIncorrect = true;
  23.         }
  24.         if (!isIncorrect && (num < MIN || num > MAX)) {
  25.             isIncorrect = true;
  26.             cout << ERROR_CHOISE;
  27.         }
  28.     } while (isIncorrect);
  29.     return num;
  30. }
  31.  
  32. int AnyToTen(int number, int base) {
  33.     int count = 0;
  34.     int result = 0;
  35.     do {
  36.         int mod = number % 10;
  37.         result += pow(base, count) * mod;
  38.         count++;
  39.         number /= 10;
  40.     } while (number);
  41.     return result;
  42. }
  43.  
  44. int main() {
  45.     setlocale(LC_ALL, "Rus");
  46.     int num, n, result;
  47.     cout << "Задание 1\n";
  48.     cout << "Введите число A\n";
  49.     num = TakeNum(1, 2147483647);
  50.     cout << "Введите степень счисления n\n";
  51.     n = TakeNum(1, 16);
  52.     result = AnyToTen(num, n);
  53.     cout << "Число, предшествующее введенному = " << result - 1;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment