Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- string inputPath()
- {
- bool isIncorrect;
- string path;
- cout << "Введите абсолютную ссылку на файл\n";
- do
- {
- isIncorrect = false;
- cin >> path;
- ifstream file(path);
- if (!file.is_open())
- {
- cout << "Файл не найден! Введите абсолютную ссылку на файл\n";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- int findSumOfDividers(int number)
- {
- int sum = 0;
- for (int i = 1; i < number; i++)
- {
- if (number % i == 0)
- {
- sum += i;
- }
- }
- return sum;
- }
- void outputToFile(string path)
- {
- ofstream file(path);
- int firstNumber;
- int secondNumber;
- bool notRepeatNumbers = true;
- for (int i = 1; i < 32000; i++) {
- firstNumber = findSumOfDividers(i);
- secondNumber = findSumOfDividers(firstNumber);
- if ((i == secondNumber) && (secondNumber != firstNumber)) {
- if (notRepeatNumbers)
- {
- cout << firstNumber << " и " << secondNumber << endl;
- file << firstNumber << " и " << secondNumber << endl;
- notRepeatNumbers = false;
- }
- else
- {
- notRepeatNumbers = true;
- }
- }
- }
- file.close();
- cout << "Информация успешно записана в файл!";
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- cout <<"Программа выводит все дружественные числа до 32000\n";
- string path = inputPath();
- outputToFile(path);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement