2025-03-06 10:21:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2025-03-06 10:40:23 +00:00
|
|
|
#include <ctype.h>
|
2025-03-06 10:21:11 +00:00
|
|
|
|
|
|
|
#define LINE_SIZE 100
|
|
|
|
|
2025-03-06 11:07:55 +00:00
|
|
|
// Function to safely read a double value
|
2025-03-06 10:43:15 +00:00
|
|
|
int read_double(double *value, int coef_index) {
|
2025-03-06 10:40:23 +00:00
|
|
|
char line[LINE_SIZE];
|
2025-03-06 11:07:55 +00:00
|
|
|
|
2025-03-06 10:40:23 +00:00
|
|
|
if (fgets(line, LINE_SIZE, stdin) == NULL) {
|
2025-03-06 11:07:55 +00:00
|
|
|
return 0; // Error while reading
|
2025-03-06 10:49:06 +00:00
|
|
|
}
|
2025-03-06 11:07:55 +00:00
|
|
|
|
|
|
|
// Remove the newline character
|
2025-03-06 10:46:00 +00:00
|
|
|
line[strcspn(line, "\r\n")] = 0;
|
|
|
|
|
2025-03-06 11:07:55 +00:00
|
|
|
// Check if the line is empty
|
2025-03-06 10:46:00 +00:00
|
|
|
if (strlen(line) == 0) {
|
|
|
|
return 0;
|
2025-03-06 10:40:23 +00:00
|
|
|
}
|
2025-03-06 10:46:00 +00:00
|
|
|
|
2025-03-06 10:40:23 +00:00
|
|
|
char *endptr;
|
|
|
|
*value = strtod(line, &endptr);
|
2025-03-06 10:46:00 +00:00
|
|
|
|
2025-03-06 11:07:55 +00:00
|
|
|
// If no number was read or there are invalid characters
|
2025-03-06 10:46:00 +00:00
|
|
|
if (endptr == line || *endptr != '\0') {
|
2025-03-06 11:17:00 +00:00
|
|
|
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", coef_index); // Corrected error message
|
2025-03-06 11:07:55 +00:00
|
|
|
return -1; // Indicate error
|
2025-03-06 10:40:23 +00:00
|
|
|
}
|
2025-03-06 11:07:55 +00:00
|
|
|
|
2025-03-06 10:40:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2025-03-06 10:21:11 +00:00
|
|
|
int main() {
|
2025-03-06 10:40:23 +00:00
|
|
|
double x;
|
2025-03-06 11:07:55 +00:00
|
|
|
|
2025-03-06 11:17:00 +00:00
|
|
|
// Read the value of x
|
2025-03-06 11:11:29 +00:00
|
|
|
int status = read_double(&x, 1);
|
|
|
|
if (status == -1) {
|
2025-03-06 11:17:00 +00:00
|
|
|
// If x is invalid, print a specific error and exit
|
2025-03-06 11:11:29 +00:00
|
|
|
printf("Nepodarilo sa nacitat zaklad x\n");
|
2025-03-06 10:40:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2025-03-06 11:11:29 +00:00
|
|
|
if (status == 0) {
|
|
|
|
return 1; // Exit if no input is provided
|
|
|
|
}
|
2025-03-06 10:40:23 +00:00
|
|
|
|
|
|
|
double coef;
|
|
|
|
double result = 0;
|
2025-03-06 11:17:00 +00:00
|
|
|
int coef_count = 0; // We will count coefficients starting from 0, so x^0 coefficient will be at index 0
|
2025-03-06 10:21:11 +00:00
|
|
|
|
2025-03-06 11:07:55 +00:00
|
|
|
// Loop to read coefficients
|
2025-03-06 10:40:23 +00:00
|
|
|
while (1) {
|
2025-03-06 11:17:00 +00:00
|
|
|
coef_count++; // Increase coefficient index for each input
|
2025-03-06 11:14:11 +00:00
|
|
|
status = read_double(&coef, coef_count + 1); // coef_count + 1 for correct indexing
|
2025-03-06 11:07:55 +00:00
|
|
|
|
2025-03-06 10:56:22 +00:00
|
|
|
if (status == -1) {
|
2025-03-06 11:07:55 +00:00
|
|
|
return 0; // Exit program if there's invalid input
|
2025-03-06 10:56:22 +00:00
|
|
|
}
|
2025-03-06 11:07:55 +00:00
|
|
|
|
2025-03-06 10:56:22 +00:00
|
|
|
if (status == 0) {
|
2025-03-06 11:07:55 +00:00
|
|
|
if (coef_count == 1) { // If no coefficient is provided for x^0
|
2025-03-06 10:40:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2025-03-06 10:53:15 +00:00
|
|
|
break;
|
2025-03-06 10:36:49 +00:00
|
|
|
}
|
2025-03-06 11:07:55 +00:00
|
|
|
|
2025-03-06 11:18:16 +00:00
|
|
|
result = result * x + coef; // Apply Horner's method for polynial evaluation
|
2025-03-06 10:21:11 +00:00
|
|
|
}
|
2025-03-06 10:40:23 +00:00
|
|
|
|
2025-03-06 11:07:55 +00:00
|
|
|
// Print the result
|
2025-03-06 10:40:23 +00:00
|
|
|
printf("Vysledok je: %.2f\n", result);
|
2025-03-06 10:21:11 +00:00
|
|
|
return 0;
|
2025-03-06 10:40:23 +00:00
|
|
|
}
|