Advertisement
DasShelmer

5.4.19

Oct 15th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. double calc(int& n, double &xm1) {
  7.     return (n % 2 == 0) ? -xm1 / n : xm1 / n;
  8. }
  9. int main() {
  10.     setlocale(LC_ALL, "Russian");
  11.     int n = 1, precision = 0;
  12.     const double h = 0.1, a = 0.5, b = 1.5;
  13.     double x = a, e = 0, fx, s = 0, xm1;
  14.     while (e <= 0) {
  15.         cout << "Введите значение e (e > 0): ";
  16.         cin >> e;
  17.     }
  18.     precision = -((int)log10(e) - 1); // Установка точности для вывода
  19.     e = pow(10, -precision) / 2.000000000000001; // Упрощение E (0.008 -> 0.001 -> 0.000499..9)
  20.  
  21.     cout << "  №          X           Fx   n\n";
  22.     while (x <= b + h) {
  23.         xm1 = x - 1;
  24.         s = 0;
  25.         int ns = 10;
  26.         fx = e + 1;
  27.         do{
  28.             xm1 *= x - 1;
  29.             fx = calc(ns, xm1);
  30.             s += fx;
  31.             ns++;
  32.         }while (abs(fx) > e);
  33.  
  34.         cout << setw(3) << n
  35.             << setw(11) << right << fixed << setprecision(1) << x
  36.             << setw(13) << right << fixed << setprecision(precision) << s
  37.             << setw(4) << ns << endl;
  38.         x += h;
  39.         n++;
  40.     }
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement