Advertisement
VladimirKostovsky

lab_clear_C_beta

Dec 11th, 2023
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.79 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <locale.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. int main() {
  8.     setlocale(LC_ALL, "Russian");
  9.  
  10.     int choice;
  11.     char inputFileName[100];
  12.     char outputFileName[100];
  13.  
  14.     printf("Выберите задачу (1 для задачи 4.1, 2 для задачи 4.2): ");
  15.     scanf("%d", &choice);
  16.  
  17.     printf("Введите имя входного файла: ");
  18.     scanf("%s", inputFileName);
  19.  
  20.     char tempOutputFileName[100];
  21.     strcpy(tempOutputFileName, inputFileName); // Копируем имя входного файла во временную переменную
  22.     char* dot = strrchr(tempOutputFileName, '.'); // Находим последнюю точку в имени файла
  23.     if (dot) {
  24.         *dot = '\0'; // Удаляем расширение, установив нулевой символ на место точки
  25.     }
  26.  
  27.     strcat(tempOutputFileName, ".out"); // Добавляем расширение .out к временному имени файла
  28.  
  29.     // Проверяем, имеет ли выходное имя файла расширение .out, иначе добавляем его
  30.     strcpy(outputFileName, tempOutputFileName);
  31.  
  32.     FILE* inputFile;
  33.     FILE* outputFile;
  34.  
  35.     switch (choice) {
  36.     case 1: {
  37.         char targetSymbol;
  38.  
  39.         printf("Введите символ для поиска: ");
  40.         scanf(" %c", &targetSymbol);
  41.  
  42.         inputFile = fopen(inputFileName, "r");
  43.         if (inputFile == NULL) {
  44.             perror("Ошибка открытия входного файла");
  45.             return 1;
  46.         }
  47.  
  48.         outputFile = fopen(outputFileName, "w");
  49.         if (outputFile == NULL) {
  50.             perror("Ошибка открытия выходного файла");
  51.             fclose(inputFile);
  52.             return 1;
  53.         }
  54.  
  55.         char buffer[1000];
  56.         while (fgets(buffer, sizeof(buffer), inputFile) != NULL) {
  57.             if (strchr(buffer, targetSymbol) != NULL || strchr(buffer, targetSymbol - 32) != NULL || strchr(buffer, targetSymbol + 32) != NULL) {
  58.                 fputs(buffer, outputFile);
  59.             }
  60.         }
  61.  
  62.         fclose(inputFile);
  63.         fclose(outputFile);
  64.  
  65.         printf("Задача 4.1 завершена. Результат записан в файл %s\n", outputFileName);
  66.         break;
  67.     }
  68.     case 2: {
  69.         char lastChar = '\0'; // Инициализируем lastChar начальным значением
  70.         inputFile = fopen(inputFileName, "r");
  71.         if (inputFile == NULL) {
  72.             perror("Ошибка открытия входного файла");
  73.             return 1;
  74.         }
  75.  
  76.         outputFile = fopen(outputFileName, "w");
  77.         if (outputFile == NULL) {
  78.             perror("Ошибка открытия выходного файла");
  79.             fclose(inputFile);
  80.             return 1;
  81.         }
  82.  
  83.         int maxReplacements = 5; // Пример значения по умолчанию
  84.         printf("Максимальное количество замен по умолчанию: %d\n", maxReplacements);
  85.         printf("Предупреждение: Превышение максимального количества замен будет игнорироваться системой.\n");
  86.         printf("Введите новое максимальное количество замен: ");
  87.         scanf("%d", &maxReplacements);
  88.  
  89.         int replacementCount = 0; // Счетчик замен
  90.         int ch;
  91.         int nextCh;
  92.  
  93.         while ((nextCh = fgetc(inputFile)) != EOF) {
  94.             ch = nextCh;
  95.  
  96.             if (ch == ' ') {
  97.                 if (replacementCount < maxReplacements) {
  98.                     fputc(lastChar, outputFile);
  99.                     replacementCount++;
  100.                 }
  101.                 else {
  102.                     fputc(' ', outputFile);
  103.                 }
  104.             }
  105.             else if (ch == '\n') {
  106.                 fputc(lastChar, outputFile);
  107.                 fputc(ch, outputFile);
  108.                 replacementCount = 0;
  109.                 lastChar = '\0';
  110.             }
  111.             else {
  112.                 lastChar = ch;
  113.                 fputc(ch, outputFile);
  114.             }
  115.         }
  116.  
  117.         if (ch != '\n' && ch != EOF) {
  118.             fputc(lastChar, outputFile);
  119.         }
  120.  
  121.         fclose(inputFile);
  122.         fclose(outputFile);
  123.  
  124.         printf("Задача 4.2 завершена. Результат записан в файл %s\n", outputFileName);
  125.         break;
  126.     }
  127.     default:
  128.         printf("Некорректный выбор. Используйте 1 для задачи 4.1 или 2 для задачи 4.2\n");
  129.         return 1;
  130.     }
  131.  
  132.     return 0;
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement