usaa25/du3/program.c
2025-10-16 17:03:53 +00:00

69 lines
1.1 KiB
C

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define LIST_SIZE 100
struct listok {
char nazov[101];
float cena;
};
void add_listok(struct listok *p, int *n){
if (fgets(p[*n].nazov, 101, stdin) == NULL){
exit(0);
}
p[*n].nazov[strcspn(p[*n].nazov, "\n")] = 0;
if (scanf("%f", &p[*n].cena) != 1){
exit(0);
}
getchar();
*n = *n + 1;
}
int compare(struct listok *a, struct listok *b){
if (a->cena > b->cena){
return 1;
}
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){
struct listok tmp = p[i];
p[i] = p[j];
p[j] = tmp;
}
}
}
}
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(){
struct listok p[LIST_SIZE];
int n = 0;
while (1){
add_listok(p, &n);
}
sort_listok(p, n);
print_listok(p, n);
return 0;
}