diff --git a/du3/program.c b/du3/program.c index 564e72e..6d1397e 100644 --- a/du3/program.c +++ b/du3/program.c @@ -9,35 +9,36 @@ struct listok { float cena; }; -void add_listok(struct listok *p, int *n){ - if (fgets(p[*n].nazov, 101, stdin) == NULL){ - exit(0); +int add_listok(struct listok *p, int *n) { + if (fgets(p[*n].nazov, 101, stdin) == NULL) { + return 0; } p[*n].nazov[strcspn(p[*n].nazov, "\n")] = 0; - if (scanf("%f", &p[*n].cena) != 1){ - exit(0); + if (scanf("%f", &p[*n].cena) != 1) { + return 0; } getchar(); *n = *n + 1; + return 1; } -int compare(struct listok *a, struct listok *b){ - if (a->cena > b->cena){ +int compare(struct listok *a, struct listok *b) { + if (a->cena > b->cena) { return 1; } - if (a->cena < b->cena){ + if (a->cena < b->cena) { return -1; } return strcmp(a->nazov, b->nazov); } -void sort_listok(struct listok *p, int n){ - for (int i = 0; i < n - 1; i++){ - for (int j = i + 1; j < n; j++){ - if (compare(&p[i], &p[j]) > 0){ +void sort_listok(struct listok *p, int n) { + for (int i = 0; i < n - 1; i++) { + for (int j = i + 1; j < n; j++) { + if (compare(&p[i], &p[j]) > 0) { struct listok tmp = p[i]; p[i] = p[j]; p[j] = tmp; @@ -46,19 +47,18 @@ void sort_listok(struct listok *p, int n){ } } -void print_listok(struct listok *p, int n){ - for (int i = 0; i < n; i++){ +void print_listok(struct listok *p, int n) { + for (int i = 0; i < n; i++) { printf("%s\n", p[i].nazov); printf("%f\n", p[i].cena); } } -int main(){ +int main() { struct listok p[LIST_SIZE]; int n = 0; - while (1){ - add_listok(p, &n); + while (add_listok(p, &n)) { } sort_listok(p, n);