From bd37d6064127369da9508f55643434adab12ff3b Mon Sep 17 00:00:00 2001 From: Michal Utlak Date: Tue, 5 Mar 2024 23:46:22 +0100 Subject: [PATCH] ak to nepojde, skacem z okna --- cv3/program.c | 50 ++++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 66f9daa..777036a 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -4,45 +4,39 @@ int main() { float pole[50]; int opakovania = 0; float cislo = 0; - float skenovanie; - int zla_hodnota = 0; - int zly_index = -1; + char line[100]; + int validny = 1; while (opakovania < 50) { - skenovanie = scanf("%f", &cislo); - - if (skenovanie == 1) { - pole[opakovania] = cislo; - opakovania++; - } else if (skenovanie == EOF) { - if (zla_hodnota) { - if (zly_index == 0) { - printf("Nepodarilo sa nacitat zaklad x\n"); - } else { - printf("Nepodarilo sa nacitat polynom na %d mieste.\n", zly_index); - } - return 0; + if (fgets(line, sizeof(line), stdin) != NULL) { + if (sscanf(line, "%f", &cislo) == 1) { + pole[opakovania] = cislo; + opakovania++; + } else if (line[0] == '\n') { + break; } else { + if (opakovania == 0) { + printf("Nepodarilo sa nacitat zaklad x.\n"); + return 0; + } + printf("Nepodarilo sa nacitat polynom na %d mieste.\n", opakovania); + opakovania = 0; + validny = 0; break; } } else { - zla_hodnota = 1; - zly_index = opakovania; - while (getchar() != '\n'); + break; } } - if (opakovania == 0) { - printf("Nepodarilo sa nacitat zaklad x.\n"); - return 0; - } + if (validny) { + float vysledok = pole[1]; + for (int i = 2; i < opakovania; ++i) { + vysledok = vysledok * pole[0] + pole[i]; + } - float vysledok = pole[1]; - for (int i = 2; i < opakovania; ++i) { - vysledok = vysledok * pole[0] + pole[i]; + printf("Vysledok je: %.2f\n", vysledok); } - printf("Vysledok je: %.2f\n", vysledok); - return 0; }