pvjc24/cv3/program.c

40 lines
886 B
C
Raw Normal View History

2024-02-27 09:04:02 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_LINE_LENGTH 256
bool IsValidNumber(const char *line) {
char *endFtr;
strtod(line, &endFtr);
2024-02-28 14:46:06 +00:00
return endFtr != line;
2024-02-27 09:04:02 +00:00
}
int main() {
char line[MAX_LINE_LENGTH];
2024-02-28 14:53:40 +00:00
double result = 0;
2024-03-07 21:27:00 +00:00
double x;
2024-03-07 21:38:58 +00:00
int lineNumber = 1;
2024-02-28 14:51:46 +00:00
2024-02-27 09:04:02 +00:00
if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) {
2024-03-07 21:47:33 +00:00
printf("Nepodarilo sa nacitat zaklad x\n");
2024-03-07 21:46:27 +00:00
return 0;
2024-02-27 09:04:02 +00:00
}
2024-03-07 21:27:00 +00:00
x = strtod(line, NULL);
2024-02-28 14:53:40 +00:00
2024-03-07 21:27:00 +00:00
while (fgets(line, MAX_LINE_LENGTH, stdin) != NULL && IsValidNumber(line)) {
double coefficient = strtod(line, NULL);
result = result * x + coefficient;
2024-03-07 21:38:58 +00:00
lineNumber++;
2024-03-07 21:27:00 +00:00
}
2024-02-27 09:04:02 +00:00
2024-03-07 21:27:00 +00:00
if (line[0] != '\n') {
2024-03-07 21:42:35 +00:00
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", lineNumber);
2024-03-07 21:44:25 +00:00
return 0;
2024-02-27 09:04:02 +00:00
}
2024-02-27 09:13:05 +00:00
printf("Vysledok je: %.2f\n", result);
2024-02-27 09:04:02 +00:00
return 0;
}