Advertisement
CherMi

Lab 12 version 0.9

Nov 7th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <locale.h>
  4. #include <math.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. double factorial(int n);
  10. double next_series_member(double x, int i);
  11. double enter_double(double *result);
  12.  
  13. int main()
  14. {
  15.  
  16.     setlocale(LC_ALL, "RUS"); //Подключение русского языка
  17.     char *err_msg = "An error has occured";
  18.     double step;
  19.     int cnt, n, i, N = 0;
  20.     double x, delta, sh = 0, precision = 0.000001;
  21.  
  22.     //Часть 1: вычисление N
  23.     for (i = 0; i < 2;)
  24.     {
  25.         n = 0;
  26.         sh = 0;
  27.         printf("Enter x%d: ", i + 1);
  28.         if (enter_double(&x) != 1) //Если возникла ошибка
  29.         {
  30.             printf("Please, enter correct value.\n");
  31.             continue;
  32.         }
  33.  
  34.         delta = fabs(sinh(x) - sh);
  35.         while (delta > precision)
  36.         {
  37.             sh = sh + next_series_member(x, n);
  38.             n++;
  39.             delta = fabs(sinh(x) - sh);
  40.         }
  41.         if (errno == EDOM || errno == ERANGE) //Проверка на ошибки
  42.         {
  43.             perror(err_msg);
  44.             printf("Please, enter correct value.\n");
  45.             errno = 0;
  46.             continue;
  47.         }
  48.         i++;
  49.         if (n > N) N = n;
  50.     }
  51.  
  52.  
  53.     //Часть 2: вывод таблицы
  54.     step = 0.1;
  55.     cnt = 0;
  56.     for (x = 0; x < 3.1; x += step) //От 0 до 3 с шагом 0,1
  57.     {
  58.         sh = 0;
  59.         for (n = 0; n <= N; n++)
  60.         {
  61.             sh = sh + next_series_member(x, n);
  62.             if (errno == EDOM || errno == ERANGE) //Проверка на ошибки
  63.             {
  64.                 perror(err_msg);
  65.                 return -1;
  66.             }
  67.         }
  68.         printf("%f    ", sh);
  69.         cnt++;
  70.         if (cnt == 9) { //По 9 значений в строке
  71.             printf("\n");
  72.             cnt = 0;
  73.         }
  74.     }
  75.     return 0;
  76. }
  77.  
  78. double enter_double(double *result)
  79. /*Возвращает -1, если ошибка считывания, -2, если неверный формат ввода,
  80. выводит на экран сообщение об этих ошибках; в остальных случаях возвращает 1
  81. и записывает по адресу result введённое значение типа double*/
  82. {
  83.     char str[1024];
  84.     char *ptr;
  85.     double value = 0;
  86.  
  87.     memset(str, '\0', 1024);
  88.     if (gets(str) == NULL) //Считываем строку с клавиатуры
  89.     {
  90.         printf("An error has occured.\n");
  91.         return -1; //Если не считалось
  92.     }
  93.     if (strlen(str) == 0) //Если введена пустая строка
  94.     {
  95.         printf("The string is empty.\n");
  96.         return -2;
  97.     }
  98.  
  99.     while ( (ptr = strchr(str, '.')) != NULL) //Замена всех точек на запятые
  100.     {
  101.         *ptr = ',';
  102.     }
  103.  
  104.     errno = 0;
  105.     ptr = NULL;
  106.     value = strtod(str, &ptr); //Перевод в число с плавающей запятой
  107.  
  108.     if (errno != 0 || (ptr != NULL && *ptr != '\0')) //Если некорректный формат ввода
  109.     {
  110.         printf("Invalid input format.\n");
  111.         return -2;
  112.     }
  113.     *result = value;
  114.     return 1;
  115. }
  116.  
  117. double factorial(int n) //Возвращает факториал числа n
  118. {
  119.     if (n <= 1) return 1;
  120.     else return n * factorial(n - 1);
  121. }
  122.  
  123. double next_series_member(double x, int i) //Возвращает i-ый член ряда разложения sh(x)
  124. {
  125.     return (pow(x, (2 * i + 1)) / factorial(2 * i + 1));
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement