nvnnaidenov

Zad69

Oct 28th, 2021
670
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 next1(double);
  7. double next2(double);
  8. double plus(double, double);
  9. double mult(double, double);
  10. double accumulate(double (*)(double, double), double, double, double,  double (*)(double), double (*)(double));
  11.  
  12. int main()
  13. {
  14.     double a;
  15.     double b;
  16.  
  17.     cout << "a = ";
  18.     cin >> a;
  19.     if(!cin)
  20.     {
  21.         cout << "Error, bad input!" << endl;
  22.         return 1;
  23.     }
  24.     cout << "b = ";
  25.     cin >> b;
  26.     if(!cin)
  27.     {
  28.         cout << "Error, bad input!" << endl;
  29.         return 1;
  30.     }
  31.  
  32.     cout << accumulate(plus, 0, a, b, cos, next1) << endl;
  33.     cout << accumulate(mult, 1, a, b, sin, next2) << endl;
  34.  
  35.     return 0;
  36. }
  37.  
  38. double next1(double x)
  39. {
  40.     return x + 1;
  41. }
  42.  
  43. double next2(double x)
  44. {
  45.     return x + 2;
  46. }
  47.  
  48. double plus(double x, double y)
  49. {
  50.     return x + y;
  51. }
  52.  
  53. double mult(double x, double y)
  54. {
  55.     return x * y;
  56. }
  57.  
  58. double accumulate(double (*op)(double, double), double null_value, double a, double b, double (*f)(double), double (*next)(double))
  59. {
  60.     double s = null_value;
  61.  
  62.     for(double i = a; i <= b + 1e-14; i = next(i))
  63.     {
  64.         s = op(s, f(i));
  65.     }
  66.  
  67.     return s;
  68. }
RAW Paste Data