usaa24/cv2/program.c

84 lines
1.4 KiB
C
Raw Permalink Normal View History

2024-10-07 12:27:25 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct pizza
{
char* name;
float price;
};
int comparator(const void* a, const void* b)
{
struct pizza A = *(struct pizza*)a;
struct pizza B = *(struct pizza*)b;
if (A.price < B.price)
{
return -1;
}
if (A.price == B.price)
{
2024-10-07 12:35:16 +00:00
return strcmp(A.name, B.name);
2024-10-07 12:27:25 +00:00
}
if (A.price > B.price)
{
return 1;
}
}
int main(int argc, char const *argv[])
{
int itemc = 0;
char find[20];
struct pizza menu[20];
char buf[50];
float price = 0;
memset(buf, 0, 50);
memset(find, 0, 20);
while (fgets(buf, 50, stdin) != NULL)
{
buf[strcspn(buf, "\n")] = '\0';
if (scanf("%f", &price) != 1)
{
break;
}
getchar();
struct pizza piteml =
{
.name = (char*)malloc(strlen(buf) * sizeof(char*)),
.price = price
};
strcpy(piteml.name,buf);
menu[itemc] = piteml;
itemc++;
if (itemc >= 20)
{
break;
}
}
qsort(menu, itemc, sizeof(struct pizza), comparator);
for (int i = 0; i < itemc; i++)
{
printf("%s\n%.6f\n", menu[i].name, menu[i].price);
}
for (int i = 0; i < itemc; i++)
{
free(menu[i].name);
}
return 0;
}