Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7.  
  8.     FILE *file = fopen("input.txt", "r");
  9.     int length;
  10.     char *buffer;
  11.     int *numericArray;
  12.  
  13.     if (file) {
  14.  
  15.         fseek(file, 0, SEEK_END);
  16.         length = ftell(file);
  17.  
  18.         buffer = malloc(length);
  19.         if (buffer == NULL) {
  20.             return EXIT_FAILURE;
  21.         }
  22.  
  23.         fseek(file, 0, SEEK_SET);
  24.         fread(buffer, 1, length, file);
  25.  
  26.         fclose(file);
  27.  
  28.         char *bufferCopy;
  29.         strcpy(bufferCopy, buffer);
  30.  
  31.         char *nextNumber = strtok(bufferCopy, ",");
  32.  
  33.         int arrayLength = 0;
  34.         while (nextNumber != NULL) {
  35.             nextNumber = strtok(NULL, ",");
  36.             arrayLength++;
  37.         }
  38.  
  39.         numericArray = malloc(sizeof(int) * arrayLength);
  40.         if (numericArray == NULL) {
  41.             return EXIT_FAILURE;
  42.         }
  43.  
  44.         int i = 0;
  45.         nextNumber = strtok(buffer, ",");
  46.         while (nextNumber != NULL) {
  47.             numericArray[i] = atoi(nextNumber);
  48.             nextNumber = strtok(NULL, ",");
  49.             i++;
  50.         }
  51.  
  52.         for (i = 0; i < arrayLength; i++) {
  53.             int j;
  54.             for (j = i + 1; j < arrayLength; j++) {
  55.                 if (compare(numericArray[i], numericArray[j])) {
  56.                     int k;
  57.                     for (k = j; k < arrayLength - 1; k++) {
  58.                         numericArray[k] = numericArray[k+1];
  59.                     }
  60.                     arrayLength--;
  61.                     numericArray = realloc(numericArray, sizeof(int) * arrayLength);
  62.                     if (numericArray == NULL) {
  63.                         return EXIT_FAILURE;
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.  
  69.         file = fopen("output.txt", "w");
  70.         for (i = 0; i < arrayLength; i++) {
  71.             fprintf(file, "%d", numericArray[i]);
  72.             if (i < arrayLength - 1) {
  73.                 fprintf(file, ",", numericArray[i]);
  74.             }
  75.         }
  76.         fclose(file);
  77.  
  78.     }
  79.  
  80.     return EXIT_SUCCESS;
  81.  
  82. }
  83.  
  84. int compare(int a, int b) {
  85.  
  86.     if (a == b) {
  87.         return 1;
  88.     }
  89.     else {
  90.         return 0;
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement