Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- #define Dash cout << '|' << setw(3) << "NUM" << '|' << setw(4) << 'x' << '|' << setw(10) << "f(x)" << '|' << " cnt|" << endl
- #define Line cout << "|---|----|----------|----|" << endl
- using namespace std;
- int tw = 2, svn = 7;
- double eps;
- double abs(double a){return a > 0 ? a : -a;}
- double next(double x, double cur_x, int i)
- {
- return (x * cur_x)/(tw * i * svn * i);
- }
- int cnt_sum;
- double F(double x)
- {
- double result = 1;
- double cur_x = 1;
- cnt_sum = 0;//просуммированно всего.
- for (int i = 1; i < 1000; i++)
- {
- double add = next(x, cur_x, i);
- if (abs(add) <= eps) break;
- cur_x *= -x;
- cnt_sum++;
- result += add;
- }
- return result;
- }
- void Print(double x, double f_x, int cnt, int cnt_sum)
- {
- cout << '|' << setw(3) << cnt
- << '|' << setw(4) << x
- << '|' << setw(10) << fixed << setprecision(6) << f_x << setprecision(1)
- << '|' << setw(4) << cnt_sum
- << '|' << endl;
- Line;
- }
- int main()
- {
- cout << "Enter a : ";
- double a;
- cin >> a;
- cout << "Enter b : ";
- double b;
- cin >> b;
- cout << "Enter 'eps' : ";
- cin >> eps;
- double answer = 1;
- int cnt = 1;
- Line;
- Dash;
- Line;
- for (double i = a; i <= b + 0.0001 ; i += 0.1)// 0.0001 нужен т.к. сравнение вещественных чисел работает через жопу. можно убрать, не думаю, что преподы в курсе таких тонкостей
- {
- double cur = F(i);
- Print(i, cur, cnt, cnt_sum);
- cnt++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement