Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- typedef double (*ftype)(double, int);
- struct I_print { //данные для печати результатов интегрирования
- const char* name;//название функции
- double i_sum; //значение интегральной суммы
- double i_toch; //точное значение интеграла
- int n; //число разбиений области интегрирования
- //при котором достигнута требуемая точность
- };
- void PrintTabl(I_print i_prn[], int k, char* gg);
- double IntRect(ftype f, double a, double b, double eps, int& n, int mode);
- double IntTrap(ftype f, double a, double b, double eps, int& n, int mode);
- double i_toch(int mode);
- double funcs(double x, int mode);
- int main()
- {
- double a = -1, b = 3;
- int n = 0;
- double eps = 0.1;
- I_print i_prn[4];
- const char* func_name[] = { "y=x ", "y=sin(22x)", "y=x^4 ", "y=arctg(x)" };
- //IntRect
- while (eps >= 0.000001) {
- cout << fixed << setw(30) << "eps = " << eps << endl;
- for (int i = 0; i < 4; i++) {
- I_print I_str = { func_name[i], IntRect(funcs, a, b, eps, n, i + 1), i_toch(i + 1), n };
- i_prn[i] = I_str;
- }
- PrintTabl(i_prn, 4, "IntRect ");
- eps /= 10;
- }
- cout << endl << endl;
- eps = 0.1;
- //IntTrap
- while (eps >= 0.000001) {
- cout << fixed << setw(30) << "eps = " << eps << endl;
- for (int i = 0; i < 4; i++) {
- I_print I_str = { func_name[i], IntTrap(funcs, a, b, eps, n, i + 1), i_toch(i + 1), n };
- i_prn[i] = I_str;
- }
- PrintTabl(i_prn, 4, "IntTrap ");
- eps /= 10;
- }
- system("pause");
- return 0;
- }
- void PrintTabl(I_print i_prn[], int k, char* gg)
- {
- const int m = 4;//число столбцов таблицы
- int wn[m] = { 12,18,18,10 };//ширина столбцов таблицы
- const char* title[m] = { "Function","Integral", gg ,"N " };
- int size[m];
- for (int i = 0; i < m; i++)
- size[i] = strlen(title[i]);
- //шапка таблицы
- cout << char(218) << setfill(char(196));
- for (int j = 0; j < m - 1; j++)
- cout << setw(wn[j]) << char(194);
- cout << setw(wn[m - 1]) << char(191) << endl;
- cout << char(179);
- for (int j = 0; j < m; j++)
- cout << setw((wn[j] - size[j]) / 2) << setfill(' ') << ' ' << title[j]
- << setw((wn[j] - size[j]) / 2) << char(179);
- cout << endl;
- for (int i = 0; i < k; i++)
- {//заполнение таблицы
- cout << char(195) << fixed;
- for (int j = 0; j < m - 1; j++)
- cout << setfill(char(196)) << setw(wn[j]) << char(197);
- cout << setw(wn[m - 1]) << char(180) << setfill(' ') << endl;
- cout << char(179) << setw((wn[0] - strlen(i_prn[i].name)) / 2) << ' ' << i_prn[i].name
- << setw((wn[0] - strlen(i_prn[i].name)) / 2) << char(179);
- cout << setw(wn[1] - 1) << setprecision(10) << i_prn[i].i_toch << char(179)
- << setw(wn[2] - 1) << i_prn[i].i_sum << setprecision(6) << char(179)
- << setw(wn[3] - 1) << i_prn[i].n << char(179) << endl;
- }
- //низ таблицы
- cout << char(192) << setfill(char(196));
- for (int j = 0; j < m - 1; j++)
- cout << setw(wn[j]) << char(193);
- cout << setw(wn[m - 1]) << char(217) << setfill(' ') << endl;
- }
- double IntRect(ftype f, double a, double b, double eps, int& n, int mode)
- {
- n = 1;
- double y = 0;
- double Iold = 0, Inew = 0;
- double left = a;
- do {
- n *= 2;
- Iold = Inew;
- double dx = (b - a) / n;
- while (a < b) {
- y += f(a + dx / 2, mode);
- a += dx;
- }
- a = left;
- Inew = y * dx;
- y = 0;
- } while (abs(Iold - Inew) > 3 * eps);
- return Inew;
- }
- double IntTrap(ftype f, double a, double b, double eps, int& n, int mode) {
- n = 1;
- double y = 0;
- double Iold = 0, Inew = 0;
- double left = a;
- do {
- n *= 2;
- Iold = Inew;
- double dx = (b - a) / n;
- while (a < b) {
- y += (f(a, mode) + f(a + dx, mode)) / 2;
- a += dx;
- }
- a = left;
- Inew = y * dx;
- y = 0;
- } while (abs(Inew - Iold) > 3 * eps);
- return Inew;
- }
- double funcs(double x, int mode) {
- switch (mode) {
- case 1: return x;
- case 2: return sin(22 * x);
- case 3: return x * x * x * x;
- case 4: return atan(x);
- }
- }
- double i_toch(int mode) {
- double a = -1, b = 3;
- switch (mode) {
- case 1: return (b * b - a * a) / 2.0;
- case 2: return (cos(a * 22.0) - cos(b * 22.0)) / 22.0;
- case 3: return (b * b * b * b * b - a * a * a * a * a) / 5.0;
- case 4: return b * atan(b) - a * atan(a) - (log(b * b + 1) - log(a * a + 1)) / 2.0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement