Advertisement
DaniDori

2.1

Nov 16th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <sstream>
  6. #include <climits>
  7. #include <limits>
  8. using namespace std;
  9.  
  10. // 1.3.1. Создание текстового файла.
  11. void createFile(const string& filename, const vector<vector<int>>& data) {
  12. ofstream outfile(filename);
  13.  
  14. if (!outfile.is_open()) {
  15. cerr << "Не удалось создать файл." << endl;
  16. return;
  17. }
  18.  
  19. for (const auto& row : data) {
  20. for (const int num : row) {
  21. outfile << num << " ";
  22. }
  23. outfile << "\n";
  24. }
  25. outfile.close();
  26. }
  27.  
  28. // 1.3.2. Вывод содержимого текстового файла.
  29. void displayFileContent(const string& filename) {
  30. ifstream infile(filename);
  31.  
  32. if (!infile.is_open()) {
  33. cerr << "Не удалось открыть файл." << endl;
  34. return;
  35. }
  36.  
  37. string line;
  38. while (getline(infile, line)) {
  39. cout << line << "\n";
  40. }
  41. infile.close();
  42. }
  43.  
  44. // 1.3.3. Добавление новой строки в конец файла.
  45. void appendToFile(const string& filename, const vector<int>& data) {
  46. ofstream outfile(filename, ios::app);
  47.  
  48. if (!outfile.is_open()) {
  49. cerr << "Не удалось открыть файл." << endl;
  50. return;
  51. }
  52.  
  53. for (const int num : data) {
  54. outfile << num << " ";
  55. }
  56. outfile << "\n";
  57. outfile.close();
  58. }
  59.  
  60. // 1.3.4. Прочитать значение числа, указав его порядковый номер в файле.
  61. int readNumberByIndex(const string& filename, size_t index) {
  62. ifstream infile(filename);
  63.  
  64. if (!infile.is_open()) {
  65. cerr << "Не удалось открыть файл." << endl;
  66. return -1;
  67. }
  68.  
  69. size_t count = 0;
  70. string line;
  71. while (getline(infile, line)) {
  72. istringstream ss(line);
  73. int num;
  74. while (ss >> num) {
  75. if (count == index) {
  76. infile.close();
  77. return num;
  78. }
  79. count++;
  80. }
  81. }
  82. infile.close();
  83. cerr << "Индекс вне диапазона." << endl;
  84. return -1;
  85. }
  86.  
  87. // 1.3.5. Определить количество чисел в файле.
  88. size_t countNumbersInFile(const string& filename) {
  89. ifstream infile(filename);
  90.  
  91. if (!infile.is_open()) {
  92. cerr << "Не удалось открыть файл." << endl;
  93. return 0;
  94. }
  95.  
  96. size_t count = 0;
  97. string line;
  98. while (getline(infile, line)) {
  99. istringstream ss(line);
  100. int num;
  101. while (ss >> num) {
  102. count++;
  103. }
  104. }
  105. infile.close();
  106. return count;
  107. }
  108.  
  109. // 10. Создать новый файл из значений исходного, умножив каждое четное число на максимальное число в файле.
  110. void createModifiedFile(const string& inputFilename, const string& outputFilename) {
  111. ifstream infile(inputFilename);
  112.  
  113. if (!infile.is_open()) {
  114. cerr << "Не удалось открыть файл." << endl;
  115. return;
  116. }
  117.  
  118. int maxNumber = INT_MIN;
  119. string line;
  120. while (getline(infile, line)) {
  121. istringstream ss(line);
  122. int num;
  123. while (ss >> num) {
  124. if (num > maxNumber) {
  125. maxNumber = num;
  126. }
  127. }
  128. }
  129. infile.close();
  130.  
  131. infile.open(inputFilename);
  132. ofstream outfile(outputFilename);
  133.  
  134. while (getline(infile, line)) {
  135. istringstream ss(line);
  136. int num;
  137. while (ss >> num) {
  138. if (num % 2 == 0) {
  139. outfile << num * maxNumber << " ";
  140. }
  141. else {
  142. outfile << num << " ";
  143. }
  144. }
  145. outfile << "\n";
  146. }
  147.  
  148. infile.close();
  149. outfile.close();
  150. }
  151.  
  152. int main() {
  153. setlocale(LC_ALL, "Russian");
  154. string filename;
  155. cout << "Введите имя файла: ";
  156. cin >> filename;
  157.  
  158. while (true) {
  159. cout << "\n--- Меню ---\n";
  160. cout << "1. Создать файл\n";
  161. cout << "2. Показать содержимое файла\n";
  162. cout << "3. Добавить строку в файл\n";
  163. cout << "4. Прочитать число по индексу\n";
  164. cout << "5. Узнать количество чисел в файле\n";
  165. cout << "6. Создать модифицированный файл\n";
  166. cout << "7. Выход\n";
  167. cout << "Выберите действие: ";
  168.  
  169. int choice;
  170. cin >> choice;
  171.  
  172. switch (choice) {
  173. case 1: {
  174. vector<vector<int>> data = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
  175. createFile(filename, data);
  176. break;
  177. }
  178. case 2: {
  179. displayFileContent(filename);
  180. break;
  181. }
  182. case 3: {
  183. cout << "Введите числа для добавления (через пробел): ";
  184. vector<int> newData;
  185. int value;
  186. while (cin >> value) {
  187. newData.push_back(value);
  188. }
  189. cin.clear();
  190. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  191. appendToFile(filename, newData);
  192. break;
  193. }
  194. case 4: {
  195. size_t index;
  196. cout << "Введите индекс числа: ";
  197. cin >> index;
  198. int num = readNumberByIndex(filename, index);
  199. if (num != -1) {
  200. cout << "Число: " << num << "\n";
  201. }
  202. break;
  203. }
  204. case 5: {
  205. size_t count = countNumbersInFile(filename);
  206. cout << "Количество чисел: " << count << "\n";
  207. break;
  208. }
  209. case 6: {
  210. string outputFilename;
  211. cout << "Введите имя выходного файла: ";
  212. cin >> outputFilename;
  213. createModifiedFile(filename, outputFilename);
  214. break;
  215. }
  216. case 7:
  217. return 0;
  218. default:
  219. cout << "Неверный выбор.\n";
  220. }
  221. }
  222. return 0;
  223. }
  224.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement