Advertisement
Guest User

lab 4

a guest
Nov 18th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cmath>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7. typedef double(*func) (double, double);
  8.  
  9. double func_y(double a, double n) {
  10.     return exp(a * 2);
  11. }
  12.  
  13. double func_s(double a, double n) {
  14.     double k = 1, summ = 1, r = 1;
  15.     do
  16.     {
  17.         r = r * 2 * a / k;
  18.         summ += r;
  19.         k++;
  20.     } while (k <= n);
  21.     return summ;
  22. }
  23.  
  24. double func_fabs( double a, double n) {
  25.     return fabs(func_y(a, n) - func_s(a, n));
  26. }
  27.  
  28. void rez(func f, double a, double b, double h, double n) {
  29.     for (a; a < b; a += h) cout << fixed << "x = " << a << "\tFunction = " << setprecision(7) << f(a, n) << endl;
  30. }
  31.  
  32. int main()
  33. {
  34.     double a, b, h, n;
  35.     char k;
  36.     cout << "Enter a, b, h, n" << endl;
  37.     cin >> a >> b >> h >> n;
  38.     while (true) {
  39.         cout << "What to do?" << endl << "1. Display the function Y(x)" << endl << "2. Display the function S(x)" << endl << "3. Display |Y(x)-S(X)|" << endl << "4. Press 'e' to exit the program" << endl;
  40.         cin >> k;
  41.         switch (k) {
  42.         case '1': default: {
  43.             cout << "Function Y(x)" << endl;
  44.             rez(func_y, a, b, h, n);
  45.             cout << endl;
  46.             break;
  47.         }
  48.         case '2': {
  49.             cout << "Function S(x)" << endl;
  50.             rez(func_s, a, b, h, n);
  51.             cout << endl;
  52.             break;
  53.         }
  54.         case '3': {
  55.             cout << "Function |Y(x)-S(X)|" << endl;
  56.             rez(func_fabs, a, b, h, n);
  57.             cout << endl;
  58.             break;
  59.         }
  60.         case 'e': case 'E': return 11;
  61.         }
  62.     }
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement