Advertisement
max22155

Untitled

Dec 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct area{
  6.     _Bool type;//0== int used
  7.     int val;
  8.     char *str;
  9. };
  10.  
  11. struct line{
  12.     struct area *area;
  13. };
  14.  
  15.  
  16. int N;
  17. void create(struct line** line)
  18. {
  19.     //first
  20.     (*line)->area=malloc(sizeof(struct area)*24);
  21.     //second
  22.     for(int i=0;i<24;i++)
  23.     {
  24.         ((*line)->area)[i].str=malloc(100);
  25.     }
  26.  
  27. }
  28.  
  29. int cmp(struct line *a,struct line *b ){
  30.     if(!a->area[N].type) {
  31.         return strcmp(a->area[N].str,b->area[N].str);
  32.     }
  33.     if(a->area[N].val<b->area[N].val)return 1;
  34.     if(a->area[N].val<b->area[N].val) return 0;
  35.     return -1;
  36. }
  37.  
  38.  
  39.  
  40. int main(void) {
  41.     FILE *input = fopen("input.txt", "r");
  42.     FILE *output = fopen("output.txt", "w");
  43.  
  44.     struct line * table=malloc(10000* sizeof(struct line));
  45.  
  46.     fscanf(input,"%d",&N);
  47.     char c =0;
  48.     int len=0;
  49.     int iter=0;
  50.     int fieldn=0;
  51.     create(&table);
  52.     while((fscanf(input, "%c", &c) != EOF))
  53.     {
  54.         if(c==' ')continue;
  55.         if(c=='"')
  56.         {
  57.             table[iter].area[fieldn].type=1;
  58.             int j=0;
  59.             fscanf(input, "%c", &c);
  60.             while(c!='"')
  61.             {
  62.                table[iter].area[fieldn].str[j]=c;
  63.                 fscanf(input, "%c", &c);
  64.                j++;
  65.             }
  66.             table[iter].area[fieldn].str[j]='\0';
  67.         }
  68.         if(c>='0'&&c<='9')
  69.         {
  70.             table[iter].area[fieldn].type=0;
  71.             ungetc(c,input);
  72.             fscanf(input,"%d",&table[iter].area[fieldn].val);
  73.         }
  74.         if(c==';')
  75.         {
  76.             fieldn++;
  77.         }
  78.         if(c=='\n')
  79.         {
  80.             len=fieldn;
  81.             fieldn=0;
  82.             iter++;
  83.         }
  84.     }
  85.  
  86.     qsort(table,iter,sizeof(struct line),cmp);
  87.  
  88.     for(int i=0;i<iter;i++)
  89.     {
  90.         for(int j=0;j<len;j++)
  91.         {
  92.             if(table[i].area[j].type==1)
  93.             {
  94.                 fprintf(output,"%d",table[i].area[j].val);
  95.             }
  96.             else
  97.             {
  98.                 char br= '"';
  99.                 fprintf(output,"%c",br);
  100.                 int k=0;
  101.             while(table[i].area[j].str[k]!='\0')
  102.             {
  103.                 fprintf(output,"%c",table[i].area[j].str[k]);
  104.             }
  105.                 fprintf(output,"%d",table[i].area[j].val);
  106.                 fprintf(output,"%c",br);
  107.             }
  108.             fprintf(output,";");
  109.         }
  110.         fprintf(output,"\n");
  111.     }
  112.     fclose(input);
  113.     fclose(output);
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement