Filage

lab10

Dec 8th, 2023
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int TakeNum(const int MIN, const int MAX) {
  7.     const string ERROR_CHOISE = "Проверьте корректность введнных данных!\n";
  8.     bool isIncorrect;
  9.     int num;
  10.     do {
  11.         isIncorrect = false;
  12.         cin >> num;
  13.         if (cin.fail()) {
  14.             isIncorrect = true;
  15.             cout << ERROR_CHOISE;
  16.             cin.clear();
  17.             while (cin.get() != '\n');
  18.         }
  19.         if (!isIncorrect && cin.get() != '\n') {
  20.             cin.clear();
  21.             while (cin.get() != '\n');
  22.             cout << ERROR_CHOISE;
  23.             isIncorrect = true;
  24.         }
  25.         if (!isIncorrect && (num < MIN || num > MAX)) {
  26.             isIncorrect = true;
  27.             cout << ERROR_CHOISE;
  28.         }
  29.     } while (isIncorrect);
  30.     return num;
  31. }
  32.  
  33. // function for the second task
  34. void swap(int *x, int *y) {
  35.     int temp = *x;
  36.     *x = *y;
  37.     *y = temp;
  38. }
  39.  
  40.  
  41. // function for the fourth task
  42. void amstrongNumber(int k, int& first, int& second) {
  43.     int count = 0, temp, sum, digit, i, j = 0;
  44.     bool isTwo = false;
  45.     for(int i = 11; i < k + 1; i++) {
  46.         sum = 0;
  47.         count = 0;
  48.         temp = i;
  49.         while (temp > 0) {
  50.             temp /= 10;
  51.             count++;
  52.         }
  53.         temp = i;
  54.         while (temp > 0) {
  55.             digit = temp % 10;
  56.             sum += pow(digit, count);
  57.             temp /= 10;
  58.         }
  59.         if (sum == i && j == 0) {
  60.             first = sum;
  61.             j++;
  62.         }
  63.         else if (sum == i && j == 1) {
  64.             second = sum;
  65.             j++;
  66.         }
  67.     }
  68. }
  69. int main() {
  70.     setlocale(LC_ALL, "");
  71.  
  72.     // First task
  73.     cout << "Задание 1\n";
  74.     long value1 = 200000, value2;
  75.     long* yk = &value1;
  76.     cout << *yk << endl;
  77.     value2 = *yk;
  78.     cout << value2 << endl;
  79.     cout << &value1 << endl;
  80.     cout << yk << endl;
  81.  
  82.     // Second task
  83.     cout << "\nЗадание 2\n";
  84.     int a = 3, b = 5;
  85.     swap(a, b);
  86.     cout << a << " and " << b << endl;
  87.  
  88.     // Third task
  89.     cout << "\nЗадание 3\n";
  90.     int* Ptr;  
  91.     a = 7; Ptr = &a;
  92.     cout << "Адрес а = " << & a << "\nЗначение указателя Ptr = " << Ptr;
  93.     cout << "\nЗначение а = " << a << "\nЗначение, нак которое указывает Ptr = " << *Ptr;
  94.     cout << "\n&*Ptr = " << &*Ptr << "\n*&Ptr = " << *&Ptr << endl;
  95.  
  96.     // Fourth task
  97.     cout << "\nЗадание 4\n";
  98.     int k = 500, first = 0, second = 0;
  99.     amstrongNumber(k, first, second);
  100.     if (first == 0 && second == 0)
  101.         cout << "В данном диапазоне таких чисел нет";
  102.     else if (first != 0 && second == 0)
  103.         cout << "В данном диапазоне только одно число Амстронга = " << first;
  104.     else
  105.         cout << "В данном диапазоне первое число Амстронга = " << first << ", а второе = " << second;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment