Advertisement
dxvmxnd

1dopc++

Dec 4th, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. void outputTask() {
  8. cout << "Данная программа находит все числе Мерсена (число, которое может быть представлено 2p - 1, где p - простое число), которые меньше введенного N." << endl;
  9. }
  10.  
  11. void outputNum(int number) {
  12. cout << "Введенное число: " << number << endl;
  13. }
  14.  
  15. string inputPath() {
  16. string path;
  17. bool isNotCorrect;
  18.  
  19. do {
  20. isNotCorrect = false;
  21. cin >> path;
  22. ifstream fin(path);
  23. if (fin.is_open() and (size(path) > 4) and ((path[size(path) - 1] == 't') and (path[size(path) - 2] == 'x') and (path[size(path) - 3] == 't') and (path[size(path) - 4] == '.'))) {
  24. cout << "Файл успешно открыт!" << endl;
  25. }
  26. else {
  27. cout << "Ошибка открытия файла!" << endl;
  28. isNotCorrect = true;
  29. }
  30. fin.close();
  31. } while (isNotCorrect);
  32.  
  33.  
  34. return path;
  35. }
  36.  
  37. int inputFromConsole() {
  38. bool isNotCorrect;
  39. int number;
  40.  
  41. do {
  42. isNotCorrect = false;
  43. cout << "Введите натуральное число: " << endl;
  44. cin >> number;
  45. if((cin.fail() || (number < 1))) {
  46. isNotCorrect = true;
  47. cout << "Ошибка! Повторите попытку." << endl;
  48. cin.clear();
  49. while(cin.get() != '\n');
  50. }
  51. } while(isNotCorrect);
  52.  
  53. return number;
  54. }
  55.  
  56. void inputNumFromFile(int& number, string path) {
  57. ifstream fin(path);
  58. fin >> number;
  59. cout << "Ввод с файла..." << endl;
  60. if (fin.fail() || (number < 1)) {
  61. cout << "Ошибка! Введите число с клавиатуры. " << endl;
  62. fin.clear();
  63. while(fin.get() != '\n');
  64. number = inputFromConsole();
  65. }
  66. fin.close();
  67. }
  68.  
  69. int inputFromFile() {
  70. string path;
  71. int number;
  72.  
  73. cout << "Введите путь файла с данными: " << endl;
  74. path = inputPath();
  75. inputNumFromFile(number, path);
  76. outputNum(number);
  77.  
  78. return number;
  79. }
  80.  
  81. bool isFromFile() {
  82. bool isNotCorrect;
  83. int chooseNum;
  84.  
  85. do {
  86. isNotCorrect = false;
  87. cout << "Выберите, откуда вводить данные: 1, если с файла; 0, если с консоли" << endl;
  88. cin >> chooseNum;
  89. if (cin.fail() or ((chooseNum != 1) and (chooseNum != 0))) {
  90. cout << "Ошибка ввода!" << endl;
  91. isNotCorrect = true;
  92. cin.clear();
  93. while(cin.get() != '\n');
  94. }
  95. } while(isNotCorrect);
  96.  
  97. return (chooseNum == 1);
  98.  
  99. }
  100.  
  101. int inputNumber() {
  102. int number;
  103.  
  104. if (isFromFile()) {
  105. number = inputFromFile();
  106. }
  107. else {
  108. number = inputFromConsole();
  109. }
  110.  
  111. return number;
  112. }
  113.  
  114. void checkForNumbers(int number, int*& answerNumbers, int& countOfNumbers) {
  115. countOfNumbers = 0;
  116. int maxExponent = 0;
  117. int* newAnswerNumbers;
  118. int mersenneNumber;
  119.  
  120. while ((1 << maxExponent) - 1 < number) {
  121. maxExponent++;
  122. }
  123.  
  124. for (int i = 0; i < maxExponent; ++i) {
  125. mersenneNumber = (1 << i) - 1;
  126. if (mersenneNumber < number) {
  127. countOfNumbers++;
  128. newAnswerNumbers = new int[countOfNumbers];
  129. for (int j = 0; j < countOfNumbers - 1; ++j) {
  130. newAnswerNumbers[j] = answerNumbers[j];
  131. }
  132. newAnswerNumbers[countOfNumbers - 1] = mersenneNumber;
  133. delete[] answerNumbers;
  134. answerNumbers = newAnswerNumbers;
  135. }
  136. }
  137. }
  138.  
  139. void outputAnswer(int* answerNumbers, int countOfNumbers) {
  140. string path;
  141.  
  142. cout << "Числа мерсена: " << endl;
  143. for(int i = 0; i < countOfNumbers; i++) {
  144. cout << answerNumbers[i] << " ";
  145. }
  146.  
  147. cout << "\nВведите путь файла для вывода: " << endl;
  148. path = inputPath();
  149. ofstream fout(path);
  150. fout << "Числа мерсена: " << endl;
  151. for(int i = 0; i < countOfNumbers; i++) {
  152. fout << answerNumbers[i] << " ";
  153. }
  154.  
  155. fout.close();
  156. cout << "Данные успешно записаны в файл." << endl;
  157. }
  158.  
  159.  
  160.  
  161. int main() {
  162. system("chcp 1251");
  163.  
  164. int countOfNumbers;
  165. int* answerNumbers;
  166. int number;
  167.  
  168. outputTask();
  169. number = inputNumber();
  170. checkForNumbers(number, answerNumbers, countOfNumbers);
  171. outputAnswer(answerNumbers, countOfNumbers);
  172.  
  173. delete[] answerNumbers;
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement