usaa20/cv2/program.c
Maryna Kravtsova d9759f6075 program
2020-10-05 14:30:37 +02:00

80 lines
1.8 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LINESIZE 100
struct pizza{
char name[LINESIZE];
float prize;
};
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);
//line1[strlen(line1)-1] = '\0';
//line2[strlen(line2)-1] = '\0';
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;
}
}
int comparename(const void *a, const void *b){
struct pizza* pizza_a = (void*) a;
struct pizza* pizza_b = (void*) b;
return strcmp(pizza_a->name, pizza_b->name);
}
int compareprize(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);
return r;
}
int main(){
struct pizza jedalny_listok[100];
memset(jedalny_listok, 0 , sizeof(struct pizza)*100);
struct pizza item;
int counter = 0;
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), compareprize);
//qsort(jedalny_listok, counter, sizeof(struct pizza), comparename);
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;
}