The_Law

Untitled

Dec 18th, 2018
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. char *st[10001][30];
  7. int size_st = 0, kol = 0;
  8. char e[300000];
  9.  
  10. struct list {
  11.     char *s;
  12.     int num;
  13. }*big;
  14.  
  15. int compare(const void * s1, const void * s2){
  16.     const struct list *w = (const struct list *)s1;
  17.     const struct list *e = (const struct list *)s2;
  18.     if (w -> s[0] != '"'){
  19.         long long y = 0, x = 0;
  20.         int i = 0, j = 0;
  21.         if (w -> s[i] == '-') i++;
  22.         if (e -> s[j] == '-') j++;
  23.         while(w -> s[i] != '\0')
  24.             x = x * 10 + (w -> s[i++]) - '0';
  25.         while(e -> s[j] != '\0')
  26.             y = y * 10 + (e -> s[j++]) - '0';
  27.         if (w -> s[0] == '-') x *= -1;
  28.         if (e -> s[0] == '-') y *= -1;
  29.         return (x - y);
  30.     }
  31.     return strcmp(w -> s, e -> s);
  32. }
  33.  
  34. int main(void){
  35.     FILE *F;
  36.     F = fopen("input.txt", "r");
  37.     int n;
  38.     fscanf(F, "%d\n", &n);
  39.     while(fgets(e, 300000, F)){
  40.         int use = 0;
  41.         kol = 0;
  42.         int k = 0;
  43.         while(e[k] != '\n' && e[k] != '\0'){
  44.             int size_ = 1;
  45.              int now_ = 0;
  46.             while(e[k] != '\n' && e[k] != '\0'){
  47.                 if (!use && e[k] == ' '){
  48.                     k++;
  49.                     continue;
  50.                 }
  51.                 if (e[k] == '"')
  52.                     use = (use + 1) % 2;
  53.                 if (size_ - 1 == now_){
  54.                     size_ *= 2;
  55.                     st[size_st][kol] = realloc(st[size_st][kol], size_ *
  56.                                                sizeof(char));
  57.                 }
  58.                 if (e[k] == ';'){
  59.                     st[size_st][kol][now_++] = e[k++];
  60.                     st[size_st][kol][now_] = '\0';
  61.                     break;
  62.                 }
  63.                 else
  64.                     st[size_st][kol][now_++] = e[k++];
  65.             }
  66.             kol++;
  67.         }
  68.         size_st++;
  69.     }
  70.     big = malloc(10001 * sizeof(struct list));
  71.     struct list *we = big;
  72.     for (int i = 0; i < size_st; i++){
  73.         big -> num = i;
  74.         big -> s = malloc(sizeof(char) * ((int)strlen(st[i][n]) + 10));
  75.         strcpy(big -> s, st[i][n]);
  76.         big++;
  77.     }
  78.     fclose(F);
  79.     F = fopen("output.txt", "w");
  80.     qsort(we, size_st, sizeof(struct list), compare);
  81.     for (int i = 0; i < size_st; i++){
  82.         int f = we -> num;
  83.         for (int j = 0; j < kol; j++)
  84.             fprintf(F,"%s", st[f][j]);
  85.         fprintf(F, "\n");
  86.         we++;
  87.     }
  88.     fclose(F);
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment