Advertisement
Kentoo

A#1

Dec 18th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #define _USE_MATH_DEFINES
  5. #include "math.h"
  6. #include <windows.h>
  7.  
  8. using namespace std;
  9.  
  10. int fact(int n) {
  11.     switch (n) {
  12.     case 0: return 1;
  13.     case 1: return 1;
  14.     default: {
  15.         int k = 1;
  16.         for (int i = 1; i <= n; i++)
  17.             k *= i;
  18.         return k;
  19.     }
  20.     }
  21. }
  22.  
  23. double receq(double t, double f, double x, double e, double k, int i) {
  24.     double t1 = t * pow(x, 2) * fact(2 * (i - 1)) / fact(2 * i);
  25.     f += t1;
  26.     if (abs(f - k) < e) {
  27.         return f;
  28.     }
  29.     else {
  30.         return receq(t1, f, x, e, k, i + 1);
  31.     }
  32. }
  33.  
  34. void main()
  35. {
  36.     SetConsoleCP(1251);
  37.     SetConsoleOutputCP(1251);
  38.     double e;
  39.     cout << "Введите точность вычисления" << endl;
  40.     cin >> e;
  41.     double x;
  42.     cout << "Введите x" << endl;
  43.     cin >> x;
  44.     double k, t;
  45.     k = (exp(x) + exp(-x)) / 2.0;
  46.     double r;
  47.     r = receq(1, 1, x, e, k, 1);
  48.     cout.precision(15);
  49.     cout << "k = " << fixed << k << endl;
  50.     cout << "r = " << fixed << r << endl;
  51.     cout << "Абсолютная погрешность = " << fixed  << k - r << endl;
  52.     cout << "Относительная погрешность = " << fixed  << ((k - r) / k) * 100 << "%" << endl;
  53.     system("pause");
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement