nvnnaidenov

Zad70

Oct 28th, 2021
589
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. double x;
  7.  
  8. typedef double (*type1)(double, double);
  9. typedef double (*type2)(int);
  10. typedef int (*type3)(int);
  11.  
  12. double accumulate(type1, double, int, int, type2, type3);
  13. int next(int);
  14. double f(int);
  15. double sum(double, double);
  16.  
  17. int main()
  18. {
  19.     int n;
  20.  
  21.     cout << "n = ";
  22.     cin >> n;
  23.     if(!cin)
  24.     {
  25.         cout << "Error, bad input!" << endl;
  26.         return 1;
  27.     }
  28.     cout << "x = ";
  29.     cin >> x;
  30.     if(!cin)
  31.     {
  32.         cout << "Error, bad input!" << endl;
  33.         return 1;
  34.     }
  35.  
  36.     cout << accumulate(sum, 0, 0, n, f, next);
  37.  
  38.     return 0;
  39. }
  40.  
  41. int next(int x)
  42. {
  43.     return x + 1;
  44. }
  45.  
  46. double f(int i)
  47. {
  48.     int p = 1;
  49.  
  50.     for(int j = 1; j <= i; j++)
  51.     {
  52.         p *= j;
  53.     }
  54.  
  55.     return pow(x, i) / p;
  56. }
  57.  
  58. double sum(double x, double y)
  59. {
  60.     return x + y;
  61. }
  62.  
  63. double accumulate(type1 op, double null_value, int a, int b, type2 f, type3 next)
  64. {
  65.     double s = null_value;
  66.  
  67.     for(int i = a; i <= b; i = next(i))
  68.     {
  69.         s = op(s, f(i));
  70.     }
  71.  
  72.     return s;
  73. }
RAW Paste Data