Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- char *st[10001][30];
- int size_st = 0, kol = 0;
- char e[300000];
- struct list {
- char *s;
- int num;
- }*big;
- int compare(const void * s1, const void * s2){
- const struct list *w = (const struct list *)s1;
- const struct list *e = (const struct list *)s2;
- if (w -> s[0] != '"'){
- long long y = 0, x = 0;
- int i = 0, j = 0;
- if (w -> s[i] == '-') i++;
- if (e -> s[j] == '-') j++;
- while(w -> s[i] != '\0')
- x = x * 10 + (w -> s[i++]) - '0';
- while(e -> s[j] != '\0')
- y = y * 10 + (e -> s[j++]) - '0';
- if (w -> s[0] == '-') x *= -1;
- if (e -> s[0] == '-') y *= -1;
- return (x - y);
- }
- return strcmp(w -> s, e -> s);
- }
- int main(void){
- FILE *F;
- F = fopen("input.txt", "r");
- int n;
- fscanf(F, "%d\n", &n);
- while(fgets(e, 300000, F)){
- int use = 0;
- kol = 0;
- int k = 0;
- while(e[k] != '\n' && e[k] != '\0'){
- int size_ = 1;
- int now_ = 0;
- while(e[k] != '\n' && e[k] != '\0'){
- if (!use && e[k] == ' '){
- k++;
- continue;
- }
- if (e[k] == '"')
- use = (use + 1) % 2;
- if (size_ - 1 == now_){
- size_ *= 2;
- st[size_st][kol] = realloc(st[size_st][kol], size_ *
- sizeof(char));
- }
- if (e[k] == ';'){
- st[size_st][kol][now_++] = e[k++];
- st[size_st][kol][now_] = '\0';
- break;
- }
- else
- st[size_st][kol][now_++] = e[k++];
- }
- kol++;
- }
- size_st++;
- }
- big = malloc(10001 * sizeof(struct list));
- struct list *we = big;
- for (int i = 0; i < size_st; i++){
- big -> num = i;
- big -> s = malloc(sizeof(char) * ((int)strlen(st[i][n]) + 10));
- strcpy(big -> s, st[i][n]);
- big++;
- }
- fclose(F);
- F = fopen("output.txt", "w");
- qsort(we, size_st, sizeof(struct list), compare);
- for (int i = 0; i < size_st; i++){
- int f = we -> num;
- for (int j = 0; j < kol; j++)
- fprintf(F,"%s", st[f][j]);
- fprintf(F, "\n");
- we++;
- }
- fclose(F);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment