Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "math.h"
- #include <limits>
- #include <string>
- using namespace std;
- double f(double x);
- double countIntegral(double a, double b, int n);
- template <typename T>
- void input(T &a, string b);
- void checkAandB(double a, double b);
- int main() {
- double y, a, b;
- int n;
- input(a, "a");
- input(b, "b");
- input(n, "n");
- checkAandB(a, b);
- y = countIntegral(a, b, n);
- cout << y << endl;
- system("pause");
- return 0;
- }
- double f(double x) {
- return pow(sin(x), 2) - 3 * cos(x);
- }
- double countIntegral(double a, double b, int n) {
- double h, s = 0, x;
- int i;
- h = (b - a) / n;
- for (i = 0; i < n - 1; i++) {
- s += f(a + h*(i + 0.5));
- }
- s *= h;
- return s;
- }
- template <typename T>
- void input(T &a, string b) {
- while (true) {
- cout << "Enter " << b << endl;
- cin >> a;
- if (cin.get() == '\n' && !cin.fail()) {
- break;
- }
- else if (cin.fail()) {
- cout << "Number is too big/small" << endl;
- }
- else {
- cout << "Wrong input" << endl;
- }
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- }
- void checkAandB(double a, double b) {
- while (a >= b) {
- cout << "A should be less than B" << endl;
- input(a, "a");
- input(b, "b");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement