funguje
This commit is contained in:
parent
e6bdf0315b
commit
7cf32dce0c
111
du2/program.c
111
du2/program.c
@ -0,0 +1,111 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#define LINE_SIZE 100
|
||||
#define LIST_SIZE 100
|
||||
|
||||
struct pizza
|
||||
{
|
||||
char name[LINE_SIZE];
|
||||
float prize;
|
||||
};
|
||||
|
||||
int read_item(struct pizza *item)
|
||||
{
|
||||
char name_buffer[100];
|
||||
char price_buffer[20];
|
||||
if (fgets(name_buffer, sizeof(name_buffer), stdin) == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
size_t len = strlen(name_buffer);
|
||||
if (len > 0 && name_buffer[len-1] == '\n')
|
||||
{
|
||||
name_buffer[len-1] = '\0';
|
||||
}
|
||||
if (strlen(name_buffer)==0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (fgets(price_buffer, sizeof(price_buffer), stdin)==NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
float prize;
|
||||
if (sscanf(price_buffer, "%f", &prize)!=1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (prize<=0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
strcpy(item->name, name_buffer);
|
||||
item->prize=prize;
|
||||
return 1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int compare (const void * a, const void * b)
|
||||
{
|
||||
const struct pizza* pizza_a=a;
|
||||
const float prize_a=pizza_a->prize;
|
||||
const char* name_a=pizza_a->name;
|
||||
const struct pizza* pizza_b=b;
|
||||
const float prize_b=pizza_b->prize;
|
||||
const char* name_b=pizza_b->name;
|
||||
float r=prize_a-prize_b;
|
||||
if (r>0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (r<0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int r2=strcmp(name_a,name_b);
|
||||
if (r2>0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (r2<0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main ()
|
||||
{
|
||||
struct pizza list[LIST_SIZE];
|
||||
memset(list,0,LIST_SIZE*sizeof(struct pizza));
|
||||
int count=read_pizza_list(list);
|
||||
qsort(list, count, sizeof(struct pizza), compare);
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
printf("%s %f\n", list[i].name, list[i].prize);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user