40 lines
830 B
C
40 lines
830 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
|
|
#define LINE_SIZE 100
|
|
|
|
int horner(int coeffs[], int n, int x) {
|
|
int result = coeffs[0];
|
|
for (int i = 1; i < n; i++) {
|
|
result = result * x + coeffs[i];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int main() {
|
|
char line[LINE_SIZE];
|
|
memset(line, 0, LINE_SIZE);
|
|
|
|
char* r = fgets(line, LINE_SIZE, stdin);
|
|
assert(r != NULL);
|
|
|
|
char* endptr = NULL;
|
|
int x = strtol(line, &endptr, 10);
|
|
|
|
if (endptr == line) {
|
|
fprintf(stderr, "Error: Invalid input.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
int coeffs[] = {2, 3, 4};
|
|
int n = sizeof(coeffs) / sizeof(coeffs[0]);
|
|
|
|
int result = horner(coeffs, n, x);
|
|
|
|
printf("The result of the polynomial evaluation is: %d\n", result);
|
|
|
|
return 0;
|
|
}
|