This commit is contained in:
Ivan Leichenko 2024-10-07 14:27:25 +02:00
parent 52075c4d57
commit 6345ef4aef

83
cv2/program.c Normal file
View File

@ -0,0 +1,83 @@
#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)
{
return 0;
}
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;
}