usaa24/cv2/program.c

94 lines
1.5 KiB
C
Raw Normal View History

2024-10-07 19:20:02 +00:00
#include <stdio.h>
#include <string.h>
#define LINE_SIZE 100
#define LIST_SIZE 200
struct pizza
{
char name[LINE_SIZE];
float prize;
};
int read_pizza_list(struct pizza* list);
int compare_pizza (const void * a, const void * b)
{
struct pizza* p1=(struct pizza*)a;
struct pizza* p2=(struct pizza*)b;
if( p1->prize > p2->prize )
{
return 1;
}
if( p1->prize < p2->prize )
{
return -1;
}
return strcmp(p1->name, p2->name);
}
int main()
{
struct pizza list[LIST_SIZE];
memset(list,0,LIST_SIZE*sizeof(struct pizza));
int n=read_pizza_list(list);
printf("%d",n);
for(int k=0; k<n; k++)
printf("\n%s %f",list[k].name, list[k].prize);
return 0;
}
int read_item(struct pizza *r)
{
char*p;
int st;
do
{
p=fgets(r->name, 299, stdin);
}while(p!=NULL && p[0]=='\n');
if(p==NULL) {return 0;}
st=scanf("%f", &r->prize);
if(st==1)
{return 1;}
else
{return 0;}
}
int read_pizza_list(struct pizza* list)
{
int counter = 0;
for (int i=0; i< LIST_SIZE; i++)
{
struct pizza item;
memset(&item,0,sizeof(struct pizza));
int r = read_item(&item);
if (r)
{
memcpy(&list[i],&item,sizeof(struct pizza));
counter += 1;
}
else
{
break;
}
}
return counter;
}