Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include <limits.h>
  5.  
  6. #define true 1
  7. #define false 0
  8.  
  9. int main()
  10. {
  11.     FILE *f = fopen("kol.dat", "r");
  12.     if (f == NULL) {
  13.         printf("No such file in this dirrectory");
  14.         exit(1);
  15.     }
  16.     int * nums = (int*)malloc(sizeof(int));
  17.     char c = fgetc(f);
  18.     char oldc = ' ';
  19.     int i = 0;
  20.     nums[i] = 0;
  21.     int max = INT_MIN;
  22.  
  23.     while (true)
  24.     {
  25.         if (c == '-')
  26.         {
  27.             c = fgetc(f);
  28.             nums[i] = -1 * (c - '0');
  29.         }
  30.         else if (c == EOF || c == '\n') {
  31.             if (oldc == ' ')
  32.                 realloc(nums, (--i + 1) * sizeof(int));
  33.             break;
  34.         }
  35.         else if (c == ' ')
  36.         {
  37.             if (oldc == ' ') continue;
  38.             realloc(nums, (++i + 1) * sizeof(int));
  39.             nums[i] = 0;
  40.             if (nums[i - 1] > max) max = nums[i - 1];
  41.         }
  42.         else
  43.         {
  44.             nums[i] = nums[i] * 10 + (c - '0');
  45.         }
  46.         oldc = c;
  47.         c = fgetc(f);
  48.     }
  49.  
  50.     fclose(f);
  51.  
  52.     FILE *fw = fopen("kol.dat", "w");
  53.  
  54.     for (int j = 0; j < i + 1; j++){
  55.         fprintf(fw, "%d ", nums[j] >= 0 ? nums[j] : max);
  56.     }
  57.     fclose(fw);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement