Advertisement
Guest User

Autocomplete2

a guest
Jan 24th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <values.h>
  6.  
  7. typedef struct
  8. {
  9.     double number;
  10.     char *text;
  11. } phrases;
  12.  
  13. void read_input(phrases **list, int *length)
  14. {
  15.     char *line = NULL;
  16.     int line_length;
  17.     size_t line_size;
  18.     int maxlength = 8;
  19.     line_length = getline(&line, &line_size, stdin);
  20.     *list = (phrases*) malloc (maxlength * sizeof(phrases));
  21.     while (line_length != 1 && line_length != -1)
  22.     {
  23.         double number;
  24.         char *text = (char*) malloc (line_length * sizeof(char));
  25.         if (*length == maxlength)
  26.         {
  27.             maxlength *= 2;
  28.             *list = (phrases*) realloc (*list, maxlength * sizeof(phrases));
  29.         }
  30.         if (sscanf(line, "%lf:%[^\n]", &number, text) != 2)
  31.         {
  32.             free(line);
  33.             for (int i = 0; i < *length; i++)
  34.             {
  35.                 free((*list[i]).text);
  36.             }
  37.             free(*list);
  38.             free(text);
  39.             printf("Nespravny vstup.\n");
  40.             exit(1);
  41.         }
  42.         free(line);
  43.         line = NULL;
  44.         (*list)[*length].number = number;
  45.         (*list)[*length].text = text;
  46.         (*length)++;
  47.         line_length = getline(&line, &line_size, stdin);
  48.     }
  49.     free(line);
  50. }
  51.  
  52. void swap(phrases *a, phrases *b)
  53. {
  54.     double number = (*a).number;
  55.     (*a).number = (*b).number;
  56.     (*b).number = number;
  57.     char *text = (*a).text;
  58.     (*a).text = (*b).text;
  59.     (*b).text = text;
  60. }
  61.  
  62. void sort(phrases *list, int length)
  63. {
  64.     for (int i = 0; i < length; i++)
  65.     {
  66.         for (int u = 0; u < length - i - 1; u++)
  67.         {
  68.             if ((list[u]).number < (list[u + 1]).number)
  69.             {
  70.                 swap(&(list[u]), &(list[u + 1]));
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76. int main()
  77. {
  78.     int length = 0;
  79.     phrases *list = NULL;
  80.     printf("Casto hledane fraze:\n");
  81.     read_input(&list, &length);
  82.     printf("%d\n", length);
  83.     sort(list, length);
  84.     for (int i = 0; i < length; i++)
  85.     {
  86.         printf("%lf:%s\n", list[i].number, list[i].text);
  87.     }
  88.  
  89.     printf("Hledani:\n");
  90.     while(!feof(stdin))
  91.     {
  92.         char *line = NULL;
  93.         int line_length;
  94.         size_t line_size;
  95.         line_length = getline(&line, &line_size, stdin);
  96.         char *word = (char*) malloc (line_length * sizeof(char));
  97.         scanf("%[^\n]", word);
  98.         printf("%s\n", word);
  99.         free(word);
  100.         free(line);
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement