Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <values.h>
- typedef struct
- {
- double number;
- char *text;
- } phrases;
- void read_input(phrases **list, int *length)
- {
- char *line = NULL;
- int line_length;
- size_t line_size;
- int maxlength = 8;
- line_length = getline(&line, &line_size, stdin);
- *list = (phrases*) malloc (maxlength * sizeof(phrases));
- while (line_length != 1 && line_length != -1)
- {
- double number;
- char *text = (char*) malloc (line_length * sizeof(char));
- if (*length == maxlength)
- {
- maxlength *= 2;
- *list = (phrases*) realloc (*list, maxlength * sizeof(phrases));
- }
- if (sscanf(line, "%lf:%[^\n]", &number, text) != 2)
- {
- free(line);
- for (int i = 0; i < *length; i++)
- {
- free((*list[i]).text);
- }
- free(*list);
- free(text);
- printf("Nespravny vstup.\n");
- exit(1);
- }
- free(line);
- line = NULL;
- (*list)[*length].number = number;
- (*list)[*length].text = text;
- (*length)++;
- line_length = getline(&line, &line_size, stdin);
- }
- free(line);
- }
- void swap(phrases *a, phrases *b)
- {
- double number = (*a).number;
- (*a).number = (*b).number;
- (*b).number = number;
- char *text = (*a).text;
- (*a).text = (*b).text;
- (*b).text = text;
- }
- void sort(phrases *list, int length)
- {
- for (int i = 0; i < length; i++)
- {
- for (int u = 0; u < length - i - 1; u++)
- {
- if ((list[u]).number < (list[u + 1]).number)
- {
- swap(&(list[u]), &(list[u + 1]));
- }
- }
- }
- }
- int main()
- {
- int length = 0;
- phrases *list = NULL;
- printf("Casto hledane fraze:\n");
- read_input(&list, &length);
- printf("%d\n", length);
- sort(list, length);
- for (int i = 0; i < length; i++)
- {
- printf("%lf:%s\n", list[i].number, list[i].text);
- }
- printf("Hledani:\n");
- while(!feof(stdin))
- {
- char *line = NULL;
- int line_length;
- size_t line_size;
- line_length = getline(&line, &line_size, stdin);
- char *word = (char*) malloc (line_length * sizeof(char));
- scanf("%[^\n]", word);
- printf("%s\n", word);
- free(word);
- free(line);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement