This commit is contained in:
Džubara 2024-02-28 15:53:40 +01:00
parent d5d8deff25
commit 0bff677e4f

View File

@ -13,8 +13,8 @@ bool IsValidNumber(const char *line) {
int main() { int main() {
char line[MAX_LINE_LENGTH]; char line[MAX_LINE_LENGTH];
double result = 1; double result = 0;
int coefficientIndex = 0; int exponent = 2;
if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) { if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) {
@ -22,24 +22,22 @@ int main() {
return 1; return 1;
} }
double x = strtod(line, NULL);
result += x;
while (1) { while (1) {
if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) { if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) {
if (line[0] == '\n') { if (line[0] == '\n') {
break; break;
} }
printf("Nepodarilo sa nacitat polynom na %d mieste. \n", coefficientIndex); printf("Nepodarilo sa nacitat polynom\n");
return 1; return 1;
} }
char *endPtr; x = strtod(line, NULL);
double x = strtod(line, &endPtr); result += x * exponent;
if (*endPtr != '\n' && *endPtr != '\0') {
printf("Nespravny format cisla\n");
return 1;
}
result *= x; exponent--;
coefficientIndex++;
} }
printf("Vysledok je: %.2f\n", result); printf("Vysledok je: %.2f\n", result);