From 43ecf577d1a0a284a1ada285398cb5edf9dff826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Cabra?= Date: Fri, 25 Oct 2019 10:43:09 +0000 Subject: [PATCH] Aktualizovat "program.c --- program.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/program.c b/program.c index e69de29..fb7bef7 100644 --- a/program.c +++ b/program.c @@ -0,0 +1,151 @@ +#include +#include +#include + +#define LINE_SIZE 100 +#define LIST_SIZE 20 + +struct pizza{ + char name[LINE_SIZE]; + float prize; +}; + +int read_item(struct pizza* item); +int read_pizza_list(struct pizza* list); + +int compare_pizza(const void * left, const void * right) { + const struct pizza * a = (const struct pizza *) left; + const struct pizza * b = (const struct pizza *) right; + if (a->prize > b->prize){ + return 1; + } + else if (a->prize < b->prize){ + return -1; + } + else{ + return 0; + } +} + + +int main(int argc, char *argv[]) { + + struct pizza list[LIST_SIZE]; + memset(list,0,LIST_SIZE*sizeof(struct pizza)); + + int counter = 0; + for (int i=0; i< LIST_SIZE; i++){ + struct pizza item; + memset(&item,0,sizeof(struct pizza)); + // Nacitajte polozku do pomocnej premennej + // Na nacitanie pouzite vlastny kod + int r = read_item(&item); + if (r){ + // Ak sa nacitanie podarilo, skopirujte polozku do pola + memcpy(&list[i],&item,sizeof(struct pizza)); + // Spocitame uspesne nacitane polozky + counter += 1; + } + else{ + // Ak sa nacitanie nepodarilo, nasli sme poslednu polozku + // Prerusim nacitanie + break; + } + } + qsort (list, counter, sizeof(struct pizza), compare_pizza); + int i=0; + for (i = 0; i < counter; i++) { + printf("%s%.6f\n", list[i].name, list[i].prize); + } + + return 0; + + +} + +int read_item(struct pizza* item){ + + char line[LINE_SIZE]; + char line2[LINE_SIZE]; + memset(line,0,LINE_SIZE); + memset(line2,0,LINE_SIZE); + while (1){ + char *r = fgets(line,LINE_SIZE,stdin); + if(r != NULL && line[1] != 0){ + int i=0; + //printf("%s",line); + while(line[i]!='\n' || line[i]!='\r'){ + if(line[i]=='\n'){ + //printf("Koniec n riadku\n"); + break; + } + else if(line[i]=='\r'){ + //printf("Koniec r riadku\n"); + break; + } + else if((line[i]>='a' && line[i]<='z') || (line[i]>='A' && line[i]<='Z') || line[i]==' '){ + //printf("%c\n",line[i]); + i++; + } + else{ + //printf("NeplatnĂ˝ znak %c\n",line[i]); + return 0; + } + } + i=0; + char *r2 = fgets(line2,LINE_SIZE,stdin); + if(r2 != NULL && line2[1] != 0){ + int i=0; + int comma=0; + int comma_number=0; + //printf("%s",line); + while(line2[i]!='\n' || line2[i]!='\r' || line2[i]!='\0'){ + if(line2[i]=='\n'){ + //printf("Koniec n riadku\n"); + break; + } + else if(line2[i]=='\r'){ + //printf("Koniec r riadku\n"); + break; + } + else if(line2[i]=='\0'){ + //printf("Koniec r riadku\n"); + break; + } + else if((line2[i]>='0' && line2[i]<='9') || line2[i]=='.'){ + //printf("%c\n",line[i]); + if(line2[i]=='.'){ + comma++; + } + if(line2[i]!='.' && comma){ + comma_number++; + } + i++; + } + else{ + //printf("NeplatnĂ˝ znak %c\n",line[i]); + return 0; + } + } + i=0; + //printf("Comma %d\n",comma); + //printf("Comma number %d\n",comma_number); + if(comma!=1)return 0; + if(comma_number!=2)return 0; + float value = strtof(line2,NULL); + if (value == 0.0F){ + return 0; + } + item->prize = value; + strcpy(item->name, line); + return 1; + } + else{ + return 0; + } + } + else{ + return 0; + } + } +} \ No newline at end of file