fds
This commit is contained in:
parent
381896727e
commit
39fdd51cc9
39
du2/program.c
Normal file
39
du2/program.c
Normal file
@ -0,0 +1,39 @@
|
||||
#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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user