CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 09:27:02 GMT
content-type: text/html; charset=UTF-8
server: cloudflare
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
vary: accept-encoding
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: _csrf-frontend=e20909bbaa015fca7540b2bef6382f7cea04adb0e6eb8f002a6a3ba63a7ab32da%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%2260tQOCnfRvZskNmZH3eEB04M8BNqBgI-%22%3B%7D; HttpOnly; Path=/
cf-ray: 98cd51c11c89c1bf-BLR
Lab2Challenge2CPP - Pastebin.com
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;
- }
Tags:
cpp
Advertisement
Add Comment
Please, Sign In to add comment
-
✅ Make $2500 in 20 minutes⭐⭐⭐ 8
JavaScript | 3 sec ago | 0.24 KB
-
⭐✅ Swapzone Glitch ✅ Working⭐⭐⭐ P
JavaScript | 4 sec ago | 0.24 KB
-
⭐ Free Crypto Method ✅ NEVER SEEN BEFORE ⭐⭐⭐
JavaScript | 11 sec ago | 0.24 KB
-
⭐⭐⭐Make $15OO in 2O minutesV E⭐⭐
Java | 12 sec ago | 0.10 KB
-
⭐⭐Exchange Exploit⭐⭐ 4
JavaScript | 14 sec ago | 0.24 KB
-
✅ Make $2500 in 20 minutes⭐⭐⭐ N
JavaScript | 16 sec ago | 0.24 KB
-
✅⭐ Make huge profits on trading ✅ NEVER SEEN...
JavaScript | 20 sec ago | 0.24 KB
-
⭐⭐⭐MAKE $900 INSTANTLY⭐⭐
Java | 24 sec ago | 0.10 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand