2021-10-14 13:20:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define SIZE 100
|
2021-10-14 14:33:47 +00:00
|
|
|
#define NAMESIZE 50
|
2021-10-14 13:20:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct menu {
|
|
|
|
char name[SIZE];
|
|
|
|
float price;
|
|
|
|
};
|
|
|
|
|
2021-10-14 14:42:58 +00:00
|
|
|
// function to compare names alphabetically
|
|
|
|
int compare_name(const void* c1, const void* c2);
|
|
|
|
|
|
|
|
// function to compare floats
|
|
|
|
int compare_float(const void* c1, const void* c2);
|
|
|
|
|
2021-10-14 13:20:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct menu food_database[SIZE];
|
|
|
|
memset(food_database, 0, SIZE*sizeof(struct menu));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int meal_counter = 0;
|
|
|
|
char temp[20];
|
|
|
|
char* ptr1 = NULL;
|
|
|
|
char* ptr2 = NULL;
|
|
|
|
while(1){
|
2021-10-14 14:42:58 +00:00
|
|
|
// load name into struct
|
2021-10-14 13:20:13 +00:00
|
|
|
ptr1 = fgets(food_database[meal_counter].name, NAMESIZE, stdin);
|
|
|
|
if ( ptr1 == NULL ){
|
|
|
|
break;
|
|
|
|
}
|
2021-10-14 14:42:58 +00:00
|
|
|
// load price into struct
|
2021-10-14 13:20:13 +00:00
|
|
|
ptr2 = fgets(temp, 20, stdin);
|
|
|
|
food_database[meal_counter].price = strtof(temp, NULL);
|
2021-10-14 14:38:47 +00:00
|
|
|
if ( ptr2 == NULL || food_database[meal_counter].price == 0){
|
2021-10-14 13:20:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
meal_counter++;
|
|
|
|
}
|
2021-10-14 14:42:58 +00:00
|
|
|
// first sort by name, then by price
|
2021-10-14 13:20:13 +00:00
|
|
|
qsort(food_database,meal_counter,sizeof(struct menu),compare_name);
|
2021-10-14 14:33:47 +00:00
|
|
|
qsort(food_database,meal_counter,sizeof(struct menu),compare_float);
|
2021-10-14 14:42:58 +00:00
|
|
|
// print results
|
2021-10-14 13:20:13 +00:00
|
|
|
for(int i=0; i<meal_counter; i++){
|
|
|
|
printf("%s",food_database[i].name);
|
|
|
|
printf("%f\n",food_database[i].price);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-10-14 14:42:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
int compare_float(const void* c1, const void* c2){
|
|
|
|
struct menu* p1 = (struct menu* ) c1;
|
|
|
|
struct menu* p2 = (struct menu* ) c2;
|
|
|
|
if( p1->price > p2->price ){
|
|
|
|
return 1;
|
|
|
|
}else if( p1->price < p2->price ){
|
|
|
|
return -1;
|
|
|
|
}else{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare_name(const void* c1, const void* c2){
|
|
|
|
struct menu* p1 = (struct menu* ) c1;
|
|
|
|
struct menu* p2 = (struct menu* ) c2;
|
|
|
|
return strcmp(p1->name,p2->name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|