This commit is contained in:
Džubara 2024-03-07 22:27:00 +01:00
parent f7ad89efe0
commit cef61faf0c

View File

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <stdbool.h> #include <stdbool.h>
#define MAX_LINE_LENGTH 256 #define MAX_LINE_LENGTH 256
@ -14,32 +13,25 @@ bool IsValidNumber(const char *line) {
int main() { int main() {
char line[MAX_LINE_LENGTH]; char line[MAX_LINE_LENGTH];
double result = 0; double result = 0;
int exponent = 2; double x;
if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) { if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) {
printf("Nepodarilo sa nacitat zaklad x\n"); printf("Nepodarilo sa nacitat zaklad x\n");
return 1; return 1;
} }
double x = strtod(line, NULL); x = strtod(line, NULL);
result += 1;
while (1) { while (fgets(line, MAX_LINE_LENGTH, stdin) != NULL && IsValidNumber(line)) {
if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) { double coefficient = strtod(line, NULL);
if (line[0] == '\n') { result = result * x + coefficient;
break; }
}
printf("Nepodarilo sa nacitat polynom\n");
return 1;
}
x = strtod(line, NULL); if (line[0] != '\n') {
result = result * x + 1; printf("Nepodarilo sa nacitat koeficient polynomu\n");
return 1;
exponent--;
} }
printf("Vysledok je: %.2f\n", result); printf("Vysledok je: %.2f\n", result);
return 0; return 0;
} }