usaa20/cv2/program.c

74 lines
1.7 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-26 13:58:37 +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-26 13:58:37 +00:00
//compare 2 elements
int compare(const void *a, const void *b){
2020-11-01 19:50:49 +00:00
//to the value const void a(b) we will assign the value of the pointer to the structure
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-26 13:58:37 +00:00
if (r == 0){ //if prizes are identical, than check if names are in alphabetical order
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-26 13:58:37 +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-26 13:58:37 +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;
}