Advertisement
Guest User

Zadanie KR

a guest
Jun 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.78 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <locale.h>
  5. #include <math.h>
  6. #include <ctype.h>
  7. #define filename "myfile.txt"
  8. void createfile();
  9. void task();
  10. void reading();
  11. void print();
  12.  
  13. int main()
  14.  
  15. {
  16.  
  17.     setlocale(LC_ALL, "rus");
  18.  
  19.     setlocale(LC_NUMERIC, "eng");
  20.  
  21.     printf("Выберите задание a или b: ");
  22.  
  23.     char c;
  24.  
  25.     scanf("%c", &c);
  26.  
  27.     switch (c)
  28.  
  29.     {
  30.  
  31.     case 'a':
  32.  
  33.         createfile();
  34.  
  35.         task();
  36.  
  37.         print();
  38.  
  39.         break;
  40.  
  41.     case 'b':
  42.  
  43.         reading();
  44.  
  45.         break;
  46.  
  47.     default:
  48.  
  49.         printf("Вы ввели несуществующий пункт в меню\n");
  50.  
  51.         break;
  52.  
  53.     }
  54.  
  55.     getchar();
  56.  
  57.     getchar();
  58.  
  59.     return 0;
  60.  
  61. }
  62.  
  63. void createfile()
  64.  
  65. {
  66.  
  67.     FILE *f;
  68.  
  69.     int n, i;
  70.  
  71.     float num;
  72.  
  73.     if ((f = fopen(filename, "wb")) == NULL)
  74.  
  75.     {
  76.  
  77.         printf("Не удалось создать файл");
  78.  
  79.         return -1;
  80.  
  81.     }
  82.  
  83.     printf("Введите количество чисел: ");
  84.  
  85.     scanf("%d", &n);
  86.  
  87.     for (i = 0; i < n; i++)
  88.  
  89.     {
  90.  
  91.         printf("%d-е число = ", i + 1);
  92.  
  93.         scanf("%f", &num);
  94.  
  95.         fwrite(&num, sizeof(float), 1, f);
  96.  
  97.     }
  98.  
  99.     printf("\n");
  100.  
  101.     fclose(f);
  102.  
  103. }
  104.  
  105. void task()
  106.  
  107. {
  108.  
  109.     FILE *f;
  110.  
  111.     float num, max;
  112.  
  113.     int i, n = 0;
  114.     int position = 0;
  115.     char *b = " ";
  116.  
  117.     int N;
  118.  
  119.     if ((f = fopen(filename, "rb+")) == NULL)
  120.  
  121.     {
  122.  
  123.         printf("Не удалось открыть файл");
  124.  
  125.         return -1;
  126.  
  127.     }
  128.  
  129.     while (!feof(f))
  130.  
  131.     {
  132.  
  133.         fread(&num, sizeof(float), 1, f);
  134.  
  135.         if (!feof(f))
  136.  
  137.         {
  138.  
  139.             n++;
  140.  
  141.         }
  142.  
  143.     }
  144.  
  145.     for (i = 1; i <= n; i++)
  146.  
  147.     {
  148.  
  149.         fseek(f, (i - 1) * sizeof(float), SEEK_SET);
  150.  
  151.         fread(&num, sizeof(float), 1, f);
  152.  
  153.         if (i == 1)
  154.  
  155.         {
  156.  
  157.             max = fabs(num);
  158.  
  159.         }
  160.  
  161.         if (i > 1)
  162.  
  163.         {
  164.  
  165.             if (fabs(num) > fabs(max)){
  166.  
  167.                 max = num;
  168.                 position = i;
  169.             }
  170.         }
  171.  
  172.     }
  173.  
  174.     printf("Max = %.4f\n", max);
  175.  
  176.     if (position == n)
  177.     {
  178.         chsize(fileno(f), (n - 1) * sizeof(float));
  179.         fclose(f);
  180.         return 0;
  181.     }
  182.     fseek(f, position*sizeof(float), SEEK_SET);
  183.  
  184.     for (i = position-1; i < n; i++) {
  185.  
  186.         fflush(f);
  187.  
  188.         fread(&num, sizeof(float), 1, f);
  189.  
  190.         fseek(f, i*sizeof(float), SEEK_SET);
  191.  
  192.         fwrite(&num, sizeof(float), 1, f);
  193.  
  194.         fseek(f, (i + 2)*sizeof(int), SEEK_SET);
  195.  
  196.     }
  197.  
  198.     chsize(fileno(f), (n - 1) * sizeof(float));
  199.     fclose(f);
  200.  
  201. }
  202.  
  203. void print()
  204.  
  205. {
  206.  
  207.     FILE *f;
  208.  
  209.     float num;
  210.  
  211.     if ((f = fopen(filename, "rb")) == NULL)
  212.  
  213.     {
  214.  
  215.         printf("Не удалось открыть файл");
  216.  
  217.         return -1;
  218.  
  219.     }
  220.  
  221.     printf("Файл после изменений:\n");
  222.  
  223.     while (!feof(f))
  224.  
  225.     {
  226.  
  227.         fread(&num, sizeof(float), 1, f);
  228.  
  229.         if (!feof(f))
  230.  
  231.             printf("%.4f ", num);
  232.  
  233.     }
  234.  
  235.     printf("\n\n");
  236.  
  237.     fclose(f);
  238.  
  239. }
  240.  
  241. void reading()
  242. {
  243.     FILE *fr, *fc;
  244.     char strsrs[255], s[255];
  245.     char *a = NULL;
  246.     char *b = NULL;
  247.     char *o;
  248.     char *p;
  249.     int i, k = 1024;
  250.     char *c = (char*)malloc(k * sizeof(char)); // создает массив, в который мы будем записывать данные из файла
  251.     char *adresc; // укаазатель на массив, в который будут записываться данные из файла
  252.     if ((fr = fopen("text.txt", "r")) == NULL)
  253.     {
  254.         printf("Не удалось открыть файл");
  255.         return -1;
  256.     }
  257.     else
  258.     {
  259.         printf("Файл для считывания успешно открыт\n");
  260.     }
  261.     if ((fc = fopen("data.txt", "w")) == NULL)
  262.     {
  263.         printf("Не удалось открыть файл");
  264.         return -1;
  265.     }
  266.     else
  267.     {
  268.         printf("Файл для записи успешно открыт\n");
  269.     }
  270.     while (1) //построчное чтение строк из файла
  271.     {
  272.         int i = 0;
  273.         adresc = fgets(c, 255, fr); // чтение одной строки из файла
  274.         if (adresc == NULL) // проверка на конец файла
  275.         {
  276.             if (feof(fr) != 0)
  277.             {
  278.                 printf("\nThe end of the file\n");
  279.                 break;
  280.             }
  281.             else
  282.             {
  283.                 printf("Mistake was found");
  284.                 break;
  285.             }
  286.         }
  287.         int count = 0;
  288.         char *b = (char*)malloc(strlen(c)* sizeof(char));
  289.         char *a = (char*)malloc(strlen(c) * sizeof(char));
  290.         strcpy(a, c);
  291.         for (i; i < strlen(c); i++)
  292.         {
  293.             if ((c[i] >= 'A') && (c[i] <= 'z'))
  294.             {
  295.                 p = strtok(a, " ");
  296.                 while (p)
  297.                 {
  298.  
  299.                     strcpy(b, p);
  300.                     for (i = 0; i < strlen(b); i++)
  301.                     {
  302.                         if ((b[i] >= '0') && (b[i] <= '9'))
  303.                         {
  304.                             count++;
  305.                             fputs(p, fc);
  306.                             fprintf(fc, " ");
  307.                             break;
  308.                         }
  309.                     }
  310.                     if (count == 0)
  311.                     {
  312.                         for (i = strlen(b); i >= 0; i--)
  313.                         {
  314.                             b[i + 1] = b[i];
  315.                         }
  316.                         b[0] = '[';
  317.                         b[strlen(b) + 1] = '\0';
  318.                         b[strlen(b)] = ']';
  319.  
  320.                         fputs(b, fc);
  321.                         fprintf(fc, " ");
  322.  
  323.                     }
  324.                     count = 0;
  325.                     p = strtok(NULL, " ");
  326.                 }
  327.             }
  328.  
  329.             break;
  330.         }
  331.  
  332.         fprintf(fc, "\n");
  333.  
  334.     }
  335.     free(a);
  336.     free(b);
  337.     fclose(fr);
  338.  
  339.     fclose(fc);
  340.  
  341.     free(c);
  342.  
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement