Advertisement
Guest User

exam

a guest
Jan 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5.     long long* arr;
  6.     char c, flag=0;
  7.     long long cur, amount=0, i,j;
  8.     FILE *input = fopen("input.txt", "r");
  9.     if (input == NULL) // проверка открытия файла
  10.     {
  11.         fclose(input);
  12.         return 1;
  13.     }
  14.     while (!feof(input)) // подсчет запятых
  15.     {
  16.         c = fgetc(input);
  17.         if (c == ',')
  18.         {
  19.             amount++;
  20.         }
  21.  
  22.     }
  23.     arr = (long long*)malloc((amount + 1) * sizeof(long long));
  24.     if (arr == NULL)
  25.     {
  26.         fclose(input);
  27.         free(arr);
  28.         return 1;
  29.     }
  30.     fseek(input, 0, SEEK_SET); // установка текущей позиции на начало файла
  31.     FILE *output = fopen("output.txt", "w");
  32.     if (output == NULL) // проверка открытия файла
  33.     {
  34.         fclose(input);
  35.         fclose(output);
  36.         free(arr);
  37.         return 1;
  38.     }
  39.     for (i = 0; i <= amount; i++)
  40.     {
  41.         fscanf(input, "%lld%*c", &cur);
  42.         for (j = 0; j <= amount; j++)
  43.         {
  44.  
  45.             if (cur == arr[j])
  46.             {
  47.                 flag = 1;
  48.                 break;
  49.             }
  50.  
  51.         }
  52.         if (flag == 0)
  53.         {
  54.             arr[i] = cur;
  55.             if (i == 0)
  56.             {
  57.                 fprintf(output, "%lld", cur); // для корректности вывода первого значения
  58.                 continue;
  59.             }
  60.             fprintf(output, ",%lld", cur);
  61.         }
  62.         flag = 0;
  63.     }
  64.     fclose(input);
  65.     fclose(output);
  66.     free(arr);
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement