Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- constexpr int FRIEND_NUMBERS_FIND_MAX = 32000;
- constexpr int FRIEND_ARRAY_SIZE = 100;
- constexpr int PRINT_TYPE_MIN = 0;
- constexpr int PRINT_TYPE_MAX = 2;
- int getSumOfDivers(const int num)
- {
- int sum = 0;
- for (int i = 1; i < num; i++) {
- if (num % i == 0)
- sum += i;
- }
- return sum;
- }
- int calculate(int** friends)
- {
- int j = 0;
- for (int i = 2; i < (FRIEND_NUMBERS_FIND_MAX + 1); i++)
- {
- int potFriend = getSumOfDivers(i);
- if (i != potFriend && i == getSumOfDivers(potFriend))
- {
- friends[0][j] = i;
- friends[1][j] = potFriend;
- j++;
- }
- }
- return j;
- }
- bool isTextFile(const string &filePath)
- {
- bool isTxt;
- isTxt = (filePath.length() > 4 &&
- filePath[filePath.length() - 4] == '.' &&
- filePath[filePath.length() - 3] == 't' &&
- filePath[filePath.length() - 2] == 'x' &&
- filePath[filePath.length() - 1] == 't');
- return isTxt;
- }
- bool checkFileAvailability(const string &filePath, const bool read)
- {
- ios::openmode mode;
- if (read)
- mode = ifstream::in;
- else
- mode = ifstream::app;
- fstream file(filePath, mode);
- bool available;
- available = file.good();
- file.close();
- if (available && !isTextFile(filePath))
- available = false;
- return available;
- }
- int takeIntValueFromConsole(const string& text)
- {
- bool isInCorrect;
- int value;
- isInCorrect = true;
- while (isInCorrect)
- {
- cout << text;
- cin >> value;
- isInCorrect = false;
- if (cin.fail())
- {
- cout << "Введите число, а не строку или что-то иное!\n";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- isInCorrect = true;
- }
- }
- return value;
- }
- int takeIntValueInRangeFromConsole(const string& text, const int min, const int max)
- {
- bool isInCorrect;
- int value;
- isInCorrect = true;
- value = 0;
- while (isInCorrect)
- {
- value = takeIntValueFromConsole(text);
- isInCorrect = false;
- if (value < min || value > max)
- {
- cout << "Число должно находится в границах от " << min << " до " << max << "\n";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- isInCorrect = true;
- }
- }
- return value;
- }
- string takeCorrectFile(const bool input)
- {
- bool isInCorrect;
- string value;
- isInCorrect = true;
- while(isInCorrect) {
- if (input)
- cout << "Введите путь до входного файла: ";
- else
- cout << "Введите путь до выходного файла: ";
- cin >> value;
- isInCorrect = false;
- if (!checkFileAvailability(value, input)) {
- isInCorrect = true;
- cout << "Путь ведёт до файла, который недоступен или который не является текстовым файлом!\n";
- }
- }
- return value;
- }
- bool saveResultIntoFile(const string &filePath, int** friends, const int length)
- {
- bool saved;
- saved = true;
- ofstream file(filePath);
- if (file.good())
- {
- for (int i = 0; i < length; i++)
- file << friends[0][i] << " " << friends[1][i] << "\n";
- }
- else
- saved = false;
- file.close();
- return saved;
- }
- void printResultIntoConsole(int** friends, const int length)
- {
- cout << "\nДружественные числа до " << FRIEND_NUMBERS_FIND_MAX << ":\n";
- for (int i = 0; i < length; i++)
- cout << friends[0][i] << " " << friends[1][i] << "\n";
- cout << "\n";
- }
- void printResult(int** friends, const int length)
- {
- bool saved;
- int type;
- cout << "\nКуда вывести результат решения?\n";
- cout << "0 - Только консоль\n";
- cout << "1 - Только в файл\n";
- cout << "2 - И в файл, и в консоль\n";
- type = takeIntValueInRangeFromConsole("Выбранный вариант вывода: ", PRINT_TYPE_MIN, PRINT_TYPE_MAX);
- saved = false;
- string output;
- switch (type)
- {
- case 0:
- printResultIntoConsole(friends, length);
- break;
- case 1:
- output = takeCorrectFile(false);
- saved = saveResultIntoFile(output, friends, length);
- break;
- case 2:
- output = takeCorrectFile(false);
- saved = saveResultIntoFile(output, friends, length);
- printResultIntoConsole(friends, length);
- break;
- }
- if (saved)
- cout << "Результат записан в выходной файл!" << "\n";
- }
- int main()
- {
- cout << "2. Вывести все дружественные числа до 32000.\n";
- int** friends = new int*[2];
- friends[0] = new int[FRIEND_ARRAY_SIZE];
- friends[1] = new int[FRIEND_ARRAY_SIZE];
- int length;
- length = calculate(friends);
- printResult(friends, length);
- delete[] friends[0];
- delete[] friends[1];
- delete[] friends;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment