Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include "math.h"
  3. #include <limits>
  4. #include <string>
  5. using namespace std;
  6. double f(double x);
  7. double countIntegral(double a, double b, int n);
  8. template <typename T>
  9. void input(T &a, string b);
  10. void checkAandB(double a, double b);
  11. int main() {
  12.     double y, a, b;
  13.     int n;
  14.     input(a, "a");
  15.     input(b, "b");
  16.     input(n, "n");
  17.     checkAandB(a, b);
  18.     y = countIntegral(a, b, n);
  19.     cout << y << endl;
  20.     system("pause");
  21.     return 0;
  22. }
  23. double f(double x) {
  24.     return pow(sin(x), 2) - 3 * cos(x);
  25. }
  26. double countIntegral(double a, double b, int n) {
  27.     double h, s = 0, x;
  28.     int i;
  29.     h = (b - a) / n;
  30.     for (i = 0; i < n - 1; i++) {
  31.         s += f(a + h*(i + 0.5));
  32.     }
  33.     s *= h;
  34.     return s;
  35. }
  36. template <typename T>
  37. void input(T &a, string b) {
  38.     while (true) {
  39.         cout << "Enter " << b << endl;
  40.         cin >> a;
  41.         if (cin.get() == '\n' && !cin.fail()) {
  42.             break;
  43.         }
  44.         else if (cin.fail()) {
  45.             cout << "Number is too big/small" << endl;
  46.         }
  47.         else {
  48.             cout << "Wrong input" << endl;
  49.         }
  50.         cin.clear();
  51.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  52.     }
  53. }
  54. void checkAandB(double a, double b) {
  55.     while (a >= b) {
  56.         cout << "A should be less than B" << endl;
  57.         input(a, "a");
  58.         input(b, "b");
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement