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