Обновить du2/program.c

This commit is contained in:
Bohdana Marchenko 2025-03-06 11:11:29 +00:00
parent 8d455bfe3a
commit b5ed7a6c61

View File

@ -26,7 +26,6 @@ int read_double(double *value, int coef_index) {
// If no number was read or there are invalid characters // If no number was read or there are invalid characters
if (endptr == line || *endptr != '\0') { if (endptr == line || *endptr != '\0') {
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", coef_index); // Corrected error message
return -1; // Indicate error return -1; // Indicate error
} }
@ -36,19 +35,24 @@ int read_double(double *value, int coef_index) {
int main() { int main() {
double x; double x;
// Read the value of x // Read the value of x with special error message for invalid input
if (!read_double(&x, 1)) { int status = read_double(&x, 1);
if (status == -1) {
printf("Nepodarilo sa nacitat zaklad x\n");
return 1; return 1;
} }
if (status == 0) {
return 1; // Exit if no input is provided
}
double coef; double coef;
double result = 0; double result = 0;
int coef_count = 0; // We will count coefficients starting from 0, so x^0 coefficient will be at index 0 int coef_count = 0; // Start counting coefficients from 0
// Loop to read coefficients // Loop to read coefficients
while (1) { while (1) {
coef_count++; // Increase coefficient index for each input coef_count++; // Increase coefficient index for each input
int status = read_double(&coef, coef_count); status = read_double(&coef, coef_count);
if (status == -1) { if (status == -1) {
return 0; // Exit program if there's invalid input return 0; // Exit program if there's invalid input