Aktualizovat du2/program.c
This commit is contained in:
parent
00196da435
commit
d78d23d8aa
@ -3,14 +3,12 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
// Проверка, является ли строка числом
|
|
||||||
int je_cislo(const char *str) {
|
int je_cislo(const char *str) {
|
||||||
char *endptr;
|
char *endptr;
|
||||||
strtod(str, &endptr);
|
strtod(str, &endptr);
|
||||||
return *endptr == '\0';
|
return *endptr == '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Чтение одного числа с обработкой ошибок
|
|
||||||
double citaj_cislo(int poradie, int *chyba) {
|
double citaj_cislo(int poradie, int *chyba) {
|
||||||
char buffer[100];
|
char buffer[100];
|
||||||
|
|
||||||
@ -20,15 +18,15 @@ double citaj_cislo(int poradie, int *chyba) {
|
|||||||
} else {
|
} else {
|
||||||
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
||||||
}
|
}
|
||||||
*chyba = 1; // Сообщаем об ошибке
|
*chyba = 1;
|
||||||
return NAN;
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *newline = strchr(buffer, '\n');
|
char *newline = strchr(buffer, '\n');
|
||||||
if (newline) *newline = '\0'; // Убираем символ новой строки
|
if (newline) *newline = '\0';
|
||||||
|
|
||||||
if (buffer[0] == '\0') {
|
if (buffer[0] == '\0') {
|
||||||
return NAN; // Пустая строка — конец ввода
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!je_cislo(buffer)) {
|
if (!je_cislo(buffer)) {
|
||||||
@ -37,14 +35,13 @@ double citaj_cislo(int poradie, int *chyba) {
|
|||||||
} else {
|
} else {
|
||||||
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", poradie);
|
||||||
}
|
}
|
||||||
*chyba = 1; // Сообщаем об ошибке
|
*chyba = 1;
|
||||||
return NAN;
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
return atof(buffer);
|
return atof(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Функция для вычисления значения полинома по схеме Горнера
|
|
||||||
double hornerova_schema(double *koef, int n, double x) {
|
double hornerova_schema(double *koef, int n, double x) {
|
||||||
double vysledok = 0.0;
|
double vysledok = 0.0;
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
@ -53,23 +50,20 @@ double hornerova_schema(double *koef, int n, double x) {
|
|||||||
return vysledok;
|
return vysledok;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Главная функция
|
|
||||||
int main() {
|
int main() {
|
||||||
double x;
|
double x;
|
||||||
double koef[100]; // Массив коэффициентов
|
double koef[100];
|
||||||
int n = 0; // Количество коэффициентов
|
int n = 0;
|
||||||
int chyba = 0; // Флаг ошибки
|
int chyba = 0;
|
||||||
|
|
||||||
// Считываем x
|
|
||||||
x = citaj_cislo(0, &chyba);
|
x = citaj_cislo(0, &chyba);
|
||||||
if (chyba) return 0; // Если ошибка при чтении x, завершаем программу
|
if (chyba) return 0;
|
||||||
|
|
||||||
// Считываем коэффициенты
|
|
||||||
while (1) {
|
while (1) {
|
||||||
double koeficient = citaj_cislo(n + 1, &chyba);
|
double koeficient = citaj_cislo(n + 1, &chyba);
|
||||||
if (chyba) return 0; // Если ошибка при чтении коэф., завершаем программу
|
if (chyba) return 0;
|
||||||
|
|
||||||
if (isnan(koeficient)) break; // Пустая строка — конец ввода
|
if (isnan(koeficient)) break;
|
||||||
|
|
||||||
koef[n++] = koeficient;
|
koef[n++] = koeficient;
|
||||||
|
|
||||||
@ -84,7 +78,6 @@ int main() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Вычисляем результат по схеме Горнера
|
|
||||||
double vysledok = hornerova_schema(koef, n, x);
|
double vysledok = hornerova_schema(koef, n, x);
|
||||||
printf("Vysledok je: %.2f\n", vysledok);
|
printf("Vysledok je: %.2f\n", vysledok);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user