Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #include "ConsolCyr.h"
- #include <math.h>
- #include <iomanip>
- double dx;
- struct I_print{ //данные для печати результатов интегрирования
- char* name;//название функции
- double i_sum; //значение интегральной суммы
- double i_toch; //точное значение интеграла
- int n; //число разбиений области интегрирования
- //при котором достигнута требуемая точность
- };
- void PrintTabl(I_print i_prn[],int k)
- {
- const int m=4;//число столбцов таблицы
- int wn[m]={12,18,18,10};//ширина столбцов таблицы
- char *title[m]={"Function","Integral","IntSum","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;
- }
- typedef double (*TPF)(double);
- double IntRect(TPF f,double a,double b,double eps)
- {
- dx=fabs(b-a)/2; double st1, st2, r;
- st2=f(a);
- for (double x=a+dx/2; x<b; x+=dx)
- st2=st2+f(x);
- st1=st2*dx;
- do
- {
- st2=st1;
- dx=dx/2;
- st1=f(a);
- for (double x=a+dx/2; x<b; x+=dx)
- st1=st1+f(x);
- st1=st1*dx;
- r=fabs(st1-st2)/3;
- }
- while (r-eps>0);
- return st1;
- }
- double f1(double x)
- {
- return x;
- };
- double f2(double x)
- {
- return sin(22*x);
- };
- double f3(double x)
- {
- return x*x*x*x;
- };
- double f4(double x)
- {
- return atan(x);
- };
- double TOCH (int i, double a, double b)
- { double t;
- switch(i)
- {
- case 0 : t=(b*b - a*a)/2.0; break;
- case 1 : t=(cos(a*22.0) - cos(b*22.0))/22.0; break;
- case 2 : t=(b*b*b*b*b - a*a*a*a*a)/5.0; break;
- case 3 : t=b*atan(b) - a*atan(a) - (log(b*b+1) - log(a*a+1))/2.0; break;
- }
- return t;}
- double IntTrap(TPF f,double a,double b,double eps)
- {
- dx=fabs(b-a)/2; double st1, st2, r;
- st2=0;
- for (double x=a; x<b; x=x+dx)
- st2=st2+f(x);
- st1=dx*st2/2;
- do
- {
- st2=st1;
- dx=dx/2;
- st1=0;
- for (double x=a; x<b; x=dx+x)
- st1=st1+f(x);
- st1=st1*dx/2;
- r=fabs(st1-st2)/3;
- }
- while (r-eps>0);
- return st1;
- }
- int main ()
- { double a=-1, b=3;
- TPF f [4] = {f1,f2 ,f3 ,f4 };
- char* fs [4] = {"x ", "sin(22*x) ", "x*x*x*x ", "atan(x) "};
- double eps [5] = {0.01, 0.001, 0.0001, 0.00001, 0.000001};
- I_print mass[4];
- for(int j=0;j<5;j++)
- {
- for (int i=0; i<4; i++)
- {
- mass[i].name=fs[i];
- mass[i].i_sum=IntRect(f[i],a,b,eps[j]);
- mass[i].i_toch=TOCH(i, a, b);
- mass[i].n=(b-a)/dx;
- };
- PrintTabl(mass,4);
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement