usaa20/cv2/program.c

74 lines
1.6 KiB
C
Raw Normal View History

2020-10-05 12:30:37 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LINESIZE 100
struct pizza{
char name[LINESIZE];
float prize;
};
2020-10-24 17:06:11 +00:00
//read the name and prize
//push them to stack
2020-10-05 12:30:37 +00:00
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;
}
}
2020-10-24 17:06:11 +00:00
//compare 2 elements
2020-10-21 14:48:39 +00:00
int compare(const void *a, const void *b){
2020-10-05 12:30:37 +00:00
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);
2020-10-24 17:06:11 +00:00
if (r == 0){ //if prizes are identical, than check if names are in alphabetical order
2020-10-21 14:48:39 +00:00
r = strcmp(pizza_a->name, pizza_b->name);
}
2020-10-05 12:30:37 +00:00
return r;
}
int main(){
struct pizza jedalny_listok[100];
memset(jedalny_listok, 0 , sizeof(struct pizza)*100);
struct pizza item;
int counter = 0;
2020-10-24 17:06:11 +00:00
//write menu to array
2020-10-05 12:30:37 +00:00
while(stdin, read_pizza(&item)){
strcpy(jedalny_listok[counter].name, item.name);
jedalny_listok[counter].prize = item.prize;
counter++;
}
2020-10-24 17:06:11 +00:00
2020-10-21 14:48:39 +00:00
qsort(jedalny_listok, counter, sizeof(struct pizza), compare);
2020-10-05 12:30:37 +00:00
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;
}