usaa24/cv2/program.c

109 lines
1.8 KiB
C
Raw Permalink Normal View History

2024-10-07 19:20:02 +00:00
#include <stdio.h>
#include <string.h>
2024-10-07 19:49:07 +00:00
#include <stdbool.h>
2024-10-07 20:05:46 +00:00
/// @. @ @
2024-10-07 19:20:02 +00:00
#define LINE_SIZE 100
#define LIST_SIZE 200
struct pizza
{
char name[LINE_SIZE];
2024-10-07 19:49:07 +00:00
float prize;
2024-10-07 19:20:02 +00:00
};
2024-10-07 19:49:07 +00:00
int compare_pizza (struct pizza p1, struct pizza p2)
2024-10-07 19:20:02 +00:00
{
2024-10-07 19:49:07 +00:00
if( p1.prize > p2.prize )
2024-10-07 19:20:02 +00:00
{
return 1;
2024-10-07 19:49:07 +00:00
}
if( p1.prize < p2.prize )
2024-10-07 19:20:02 +00:00
{
return -1;
}
2024-10-07 19:49:07 +00:00
return strcmp(p1.name, p2.name);
}
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;
}
}
2024-10-07 19:20:02 +00:00
2024-10-07 19:49:07 +00:00
return counter;
}
void sort_pizza_list(struct pizza* list, int n)
{
bool sorted;
struct pizza tmp;
for(int k=0; k<n; k++)
{
sorted=true;
for(int i=0; i<n-1-k; i++)
{
2024-10-07 20:05:46 +00:00
if(compare_pizza(list[i], list[i+1])>0)
2024-10-07 19:49:07 +00:00
{
tmp=list[i];
list[i]=list[i+1];
list[i+1]=tmp;
sorted=false;
}
}
if(sorted)
{
break;
}
}
}
2024-10-07 19:20:02 +00:00
int main()
{
struct pizza list[LIST_SIZE];
memset(list,0,LIST_SIZE*sizeof(struct pizza));
int n=read_pizza_list(list);
2024-10-07 19:49:07 +00:00
sort_pizza_list(&list,n);
2024-10-07 19:20:02 +00:00
for(int k=0; k<n; k++)
2024-10-07 20:05:46 +00:00
printf("%s%.6f\n",list[k].name, list[k].prize);
2024-10-07 19:20:02 +00:00
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;}
2024-10-07 19:49:07 +00:00
st=scanf("%f", &r->prize);
2024-10-07 19:20:02 +00:00
if(st==1)
2024-10-07 19:49:07 +00:00
{return 1;}
2024-10-07 19:20:02 +00:00
else
{return 0;}
}