Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. #define MAX_STACK 2048
  5.  
  6. struct info {
  7.     char name[21], surname[21], patronymic[21];
  8.     unsigned long long phone;
  9. };
  10.  
  11. int comp(const struct info first, const struct info second, const char *order) {
  12.     int a = 0;
  13.     for (int i = 0; i < 4; i++) {
  14.         switch (order[i]) {
  15.             case 'n':
  16.                 a = strcmp(first.name, second.name);
  17.                 break;
  18.             case 's':
  19.                 a = strcmp(first.surname, second.surname);
  20.                 break;
  21.             case 'p':
  22.                 a = strcmp(first.patronymic, second.patronymic);
  23.                 break;
  24.             case 'h' :
  25.                 if (first.phone < second.phone) {
  26.                     a = -1;
  27.                 } else if (first.phone > second.phone) {
  28.                     a = 1;
  29.                 } else {
  30.                     a = 0;
  31.                 }
  32.                 break;
  33.         }
  34.         if (a < 0) { return -1; }
  35.         else if (a > 0) { return 1; }
  36.     }
  37.     return 0;
  38. }
  39.  
  40. void qsort_it(struct info *a, int size, char *order) {
  41.     int stack_left[MAX_STACK], stack_right[MAX_STACK];
  42.     int position = 0;
  43.     stack_left[0] = 0;
  44.     stack_right[0] = size - 1;
  45.     while (position >= 0) {
  46.         int left = stack_left[position];
  47.         int right = stack_right[position--];
  48.         while (left < right) {
  49.             int pivot_position = (left + right) >> 1;
  50.             int i = left;
  51.             int j = right;
  52.             struct info pivot = a[pivot_position];
  53.             while (i <= j) {
  54.                 while (comp(a[i], pivot, order) < 0) i++;
  55.                 while (comp(pivot, a[j], order) < 0) j--;
  56.                 if (i <= j) {
  57.                     struct info temp = a[i];
  58.                     a[i++] = a[j];
  59.                     a[j--] = temp;
  60.                 }
  61.             }
  62.             if (i < pivot_position) {
  63.                 if (i < right) {
  64.                     stack_left[++position] = i;
  65.                     stack_right[position] = right;
  66.                 }
  67.                 right = j;
  68.             } else {
  69.                 if (j > left) {
  70.                     stack_left[++position] = left;
  71.                     stack_right[position] = j;
  72.                 }
  73.                 left = i;
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. int main(int argc, char **argv) {
  80.     char *order = "nsph"; // n-name, s-surname, p-patronymic, h-phone. Место в строке определяет приоритет поля
  81.     const char *fin, *fout;
  82.     fin = argv[1];
  83.     fout = argv[2];
  84.     FILE *fp;
  85.     if ((fp = fopen(fin, "r")) == NULL) {
  86.         perror("fopen() ");
  87.         return 1;
  88.     }
  89.     struct info *data = malloc(sizeof(struct info));
  90.     if (!data) {
  91.         perror("malloc() ");
  92.         fclose(fp);
  93.         return 2;
  94.     }
  95.     int size = 1;
  96.     int position = 0;
  97.     while (fscanf(fp, "%s%s%s%llu", data[position].name, data[position].surname, data[position].patronymic,
  98.                   &data[position].phone) != EOF) {
  99.         position++;
  100.         if (position + 1 >= size) {
  101.             struct info *tmp = realloc(data, sizeof(struct info) * size * 2 + 1);
  102.             if (!tmp) {
  103.                 perror("realloc() ");
  104.                 fclose(fp);
  105.                 free(data);
  106.                 return 2;
  107.             } else {
  108.                 data = tmp;
  109.             }
  110.             size = size * 2 + 1;
  111.         }
  112.     }
  113.     fclose(fp);
  114.     qsort_it(data, position, order);
  115.     if ((fp = fopen(fout, "w")) == NULL) {
  116.         perror("fopen() ");
  117.         free(data);
  118.         return 1;
  119.     }
  120.     for (int j = 0; j < position; j++) {
  121.         fprintf(fp, "%s %s %s %llu\n", data[j].name, data[j].surname, data[j].patronymic, data[j].phone);
  122.     }
  123.     free(data);
  124.     fclose(fp);
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement