Advertisement
Vanya_Shestakov

laba2.2 (C++)

Oct 6th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. string inputPath()
  8. {
  9.     bool isIncorrect;
  10.     string path;
  11.  
  12.     cout << "Введите абсолютную ссылку на файл\n";
  13.     do
  14.     {
  15.         isIncorrect = false;
  16.         cin >> path;
  17.         ifstream file(path);
  18.         if (!file.is_open())
  19.         {
  20.             cout << "Файл не найден! Введите абсолютную ссылку на файл\n";
  21.             isIncorrect = true;
  22.         }
  23.     } while (isIncorrect);
  24.     return path;
  25. }
  26.  
  27. int findSumOfDividers(int number)
  28. {
  29.     int sum = 0;
  30.     for (int i = 1; i < number; i++)
  31.     {
  32.         if (number % i == 0)
  33.         {
  34.             sum += i;
  35.         }
  36.     }
  37.     return sum;
  38. }
  39.  
  40. void outputToFile(string path)
  41. {
  42.     ofstream file(path);
  43.  
  44.     int firstNumber;
  45.     int secondNumber;
  46.     bool notRepeatNumbers = true;
  47.  
  48.     for (int i = 1; i < 32000; i++) {
  49.         firstNumber = findSumOfDividers(i);
  50.         secondNumber = findSumOfDividers(firstNumber);
  51.  
  52.         if ((i == secondNumber) && (secondNumber != firstNumber)) {
  53.             if (notRepeatNumbers)
  54.             {
  55.                 cout << firstNumber << " и " << secondNumber << endl;
  56.                 file << firstNumber << " и " << secondNumber << endl;
  57.                 notRepeatNumbers = false;
  58.             }
  59.             else
  60.             {
  61.                 notRepeatNumbers = true;
  62.             }
  63.         }
  64.     }
  65.     file.close();
  66.     cout << "Информация успешно записана в файл!";
  67. }
  68.  
  69. int main()
  70. {
  71.     setlocale(LC_ALL, "Russian");
  72.     cout <<"Программа выводит все дружественные числа до 32000\n";
  73.  
  74.     string path = inputPath();
  75.     outputToFile(path);
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement