usaa25/du2/program.c
2025-10-01 10:27:30 +02:00

58 lines
1.1 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LINE_SIZE 150
#define LIST_SIZE 100
struct pizza{
char name[LINE_SIZE];
float price;
};
int porovnaj(const void *a,const void *b){
const struct pizza *p1 = (const struct pizza *)a;
const struct pizza *p2 = (const struct pizza *)b;
if(p1->price < p2-> price) {
return -1;
}
if(p1->price > p2-> price) {
return 1;
}
return strcmp(p1->name, p2->name);
}
int main() {
struct pizza list[LIST_SIZE];
int buf = 0;
while(buf < LIST_SIZE){
char meno_buf[LINE_SIZE];
char cena_buf[LINE_SIZE];
if(!fgets(meno_buf, LINE_SIZE, stdin)){
break;
}
meno_buf[strcspn(meno_buf, "\n")] = 0;
if(!fgets(cena_buf, LINE_SIZE, stdin)) break;
cena_buf[strcspn(cena_buf, "\n")] = 0;
if ( strlen(cena_buf) == 0) {
break;
}
float cena;
if (sscanf(cena_buf, "%f", &cena) != 1 || cena == 0) {
break;
}
strncpy(list[buf].name, meno_buf, LINE_SIZE);
list[buf].price= price;
count++;
}
qsort(list, buf, sizeof(struct pizza), porovnaj);
for(int i = 0; i < buf;i++){
printf("%s\n%.6f\n", list[i].name, list[i].price);
}
return 0;
}