74 lines
1.6 KiB
C
74 lines
1.6 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
#define LINESIZE 100
|
|
|
|
struct pizza{
|
|
char name[LINESIZE];
|
|
float prize;
|
|
};
|
|
|
|
//read the name and prize
|
|
//push them to stack
|
|
|
|
int read_pizza(struct pizza* item){
|
|
|
|
char line1[LINESIZE];
|
|
char line2[LINESIZE];
|
|
|
|
memset(line1, 0, LINESIZE);
|
|
memset(line2, 0, LINESIZE);
|
|
char *l1 = fgets(line1,LINESIZE,stdin);
|
|
char *l2 = fgets(line2,LINESIZE,stdin);
|
|
|
|
float value = strtof(line2, &l2);
|
|
if(value == 0.0F){
|
|
return 0;
|
|
}
|
|
if(l1 != NULL && line1[1] != 0){
|
|
item->prize = value;
|
|
strcpy(item->name, line1);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
//compare 2 elements
|
|
|
|
int compare(const void *a, const void *b){
|
|
|
|
struct pizza* pizza_a = (void*) a;
|
|
struct pizza* pizza_b = (void*) b;
|
|
float r = (pizza_a->prize > pizza_b->prize) - (pizza_a->prize < pizza_b->prize);
|
|
if (r == 0){ //if prizes are identical, than check if names are in alphabetical order
|
|
r = strcmp(pizza_a->name, pizza_b->name);
|
|
}
|
|
return r;
|
|
}
|
|
|
|
int main(){
|
|
|
|
struct pizza jedalny_listok[100];
|
|
memset(jedalny_listok, 0 , sizeof(struct pizza)*100);
|
|
|
|
struct pizza item;
|
|
int counter = 0;
|
|
|
|
//write menu to array
|
|
while(stdin, read_pizza(&item)){
|
|
strcpy(jedalny_listok[counter].name, item.name);
|
|
jedalny_listok[counter].prize = item.prize;
|
|
counter++;
|
|
}
|
|
|
|
qsort(jedalny_listok, counter, sizeof(struct pizza), compare);
|
|
|
|
int i = 0;
|
|
for(i = 0; i < counter; i++){
|
|
printf("%s", jedalny_listok[i].name);
|
|
printf("%f", jedalny_listok[i].prize);
|
|
printf("\n");
|
|
}
|
|
return 0;
|
|
}
|