Xom9ik

Lab_1/15var (IV semester) OS&SP

Apr 1st, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <windows.h>
  5. #include <iomanip>
  6. #include <iostream>
  7. #include <string>
  8. using namespace std;
  9.  
  10. void main()
  11. {
  12.     float a = 0, b = 0, step = 0, x = 0;
  13.     cout << "Enter A: "; cin >> a;
  14.     cout << "Enter B: "; cin >> b;
  15.     step = (b - a) / 10000;
  16.     cout << left << setw(4) << "Step" << " | " << setw(10) << "x" << " | " << setw(10) << "sqrt(1-x)" << " | " << setw(10) << "Taylor" << endl;
  17.  
  18.     clock_t start = clock();
  19.     int count = 1;
  20.     for (float stepX = a; stepX < b; stepX += step, count++) {
  21.         x = stepX;
  22.         float summ = 1;
  23.         summ -= x / 2;
  24.         for (int i = 1; i < 25; i++) {
  25.             float numerator = 1, denominator = 1;
  26.             string strnumerator = "1*";
  27.             string strdenominator = "";
  28.             int tmp = 1;
  29.             for (int j = 0; j < i; j++) {
  30.                 if (j < i - 1)
  31.                     strnumerator += to_string(tmp) + "*";
  32.                 else
  33.                     strnumerator += to_string(tmp);
  34.                 numerator *= tmp;
  35.                 tmp += 2;
  36.             }
  37.             tmp = 0;
  38.             for (int j = 0; j <= i; j++) {
  39.                 tmp += 2;
  40.                 if (j < i)
  41.                     strdenominator += to_string(tmp) + "*";
  42.                 else
  43.                     strdenominator += to_string(tmp);
  44.                 denominator *= tmp;
  45.             }
  46.             summ -= (numerator / denominator)*pow(x, (i + 1));
  47.         }
  48.         float sqr = sqrt(1 - x);
  49.         cout << left << setw(4) << count << " | " << setw(10) << x << " | " << setw(10) << sqr << " | " << setw(10) << summ << endl;
  50.     }
  51.     clock_t end = clock();
  52.     double seconds = (double)(end - start) / CLOCKS_PER_SEC;
  53.     cout << "Execution time: " << seconds << " seconds" << endl;
  54.     system("pause");
  55. }
Advertisement
Add Comment
Please, Sign In to add comment