usaa21/cv2/program.c

72 lines
1.4 KiB
C
Raw Normal View History

2021-10-14 21:21:36 +00:00
#include <stdio.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define velkost strlen
#define size 100
struct jedlo {
char name[size];
float price;
};
int compare(const void* p1,const void*p2);
int main() {
struct jedlo databaza[size];
char input[100]= {0};
float price = 0;
int counter = 0;
int helper = 2;
int counter2 = 0;
int yes = 0;
while(fgets(input, sizeof(input), stdin) && strcmp( input, "\n" ) != 0 ){
if(helper %2 == 0){
strcpy(databaza[counter].name, input);
yes++;
}
else{
price = strtof(input, NULL);
databaza[counter].price = price;
yes++;
}
if(yes == 2){
counter++;
yes = 0;
}
counter2++;
helper++;
}
2021-10-14 21:53:24 +00:00
counter2 = (counter2 / 2);
2021-10-14 21:21:36 +00:00
qsort(databaza, counter2,sizeof(struct jedlo),compare);
2021-10-14 21:53:24 +00:00
for(int z=0; z<counter2; z++){
printf("%s", databaza[z].name);
printf("%f\n", databaza[z].price);
2021-10-14 21:21:36 +00:00
}
2021-10-14 21:53:24 +00:00
2021-10-14 21:21:36 +00:00
}
int compare(const void* p1,const void*p2){
2021-10-14 21:53:24 +00:00
struct jedlo s1 = *(const struct jedlo*)p1;
struct jedlo s2 = *(const struct jedlo*)p2;
int order;
2021-10-14 21:21:36 +00:00
2021-10-14 21:53:24 +00:00
if(s1.price > s2.price){
order = 1;
2021-10-14 21:21:36 +00:00
}
2021-10-14 21:53:24 +00:00
else if(s1.price < s2.price){
order = -1;
2021-10-14 21:21:36 +00:00
}
2021-10-14 21:53:24 +00:00
else {
order = 0;
}
return order;
2021-10-14 21:21:36 +00:00
}