Created
October 15, 2012 19:07
-
-
Save LifeMoroz/3894463 to your computer and use it in GitHub Desktop.
Full lab_7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <math.h> | |
#include <iomanip> | |
using namespace std; | |
int g; | |
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+1))/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 f(double x){ //Подынтегральная функция | |
switch (g) | |
{ | |
case 1: | |
return x; | |
break; | |
case 2: | |
return sin( 22 * x); | |
break; | |
case 3: | |
return x*x*x*x; | |
break; | |
case 4: | |
return atan(x); | |
break; | |
}; | |
} | |
double IntRect(double a, double b, double n, double (*f)(double)) | |
{ | |
double result, h; | |
result = 0.0; | |
h=abs((a-b))/n; | |
for(int i=1; i <= n; i++) | |
{ | |
result += f( a + h * (i - 0.5) ); //Вычисляем в средней точке и добавляем в сумму | |
} | |
result *= h; | |
return result; | |
} | |
double toch(double a, double b) | |
{ | |
switch (g) | |
{ | |
case 1: | |
return (b*b - a*a)/2.0; | |
break; | |
case 2: | |
return (cos(a*22.0) - cos(b*22.0))/22.0; | |
break; | |
case 3: | |
return (b*b*b*b*b - a*a*a*a*a)/5.0; | |
break; | |
case 4: | |
return b*atan(b) - a*atan(a) - (log(b*b+1) - log(a*a+1))/2.0; | |
break; | |
}; | |
} | |
double IntTrap(double a, double b, double n, double (*f)(double)) | |
{ | |
double h, result = 0.0; | |
h=(a-b)/n; | |
result += f(a) / 2.; | |
result += f(b) / 2.; | |
for (int i = 1; i < (n-1); ++i) | |
result += f(a+i*h); | |
return h*result; | |
} | |
int main(void){ | |
setlocale(0,"rus"); | |
I_print i_prn[4]; | |
double a, b, eps; | |
int i=10; | |
cout << "Введите границы интегрирования" << endl << "a="; | |
cin >> a; | |
cout << "b="; | |
cin >> b; | |
cout << "Введите точность eps="; | |
cin >> eps; | |
i_prn[0].name="y=x"; | |
i_prn[1].name="sin(22*x)"; | |
i_prn[2].name="x^4"; | |
i_prn[3].name="arctg(x)"; | |
for (g=1;g<=4;g++) | |
{ | |
i=10; | |
while ((abs(IntRect(a,b,i,f)-IntRect(a,b,(i+static_cast<int>(i/2)),f))) >= eps) | |
{i+=2;} | |
i_prn[g-1].i_sum=IntRect(a,b,static_cast<int>(i+i/2),f); | |
i_prn[g-1].i_toch=toch(a,b); | |
i_prn[g-1].n=i; | |
} | |
setlocale(LC_ALL,"C"); | |
PrintTabl(i_prn, 4); | |
system("pause"); | |
setlocale(0,"rus"); | |
cout << "Метод трапеций"<< endl; | |
setlocale(LC_ALL,"C"); | |
for (g=1;g<=4;g++) | |
{ | |
i=5; | |
while ((abs(IntRect(a,b,i,f)-IntRect(a,b,(i+static_cast<int>(i/2)),f))) >= eps) | |
{i+=2;} | |
i_prn[g-1].i_sum=IntRect(a,b,i+2,f); | |
i_prn[g-1].i_toch=toch(a,b); | |
i_prn[g-1].n=i; | |
} | |
PrintTabl(i_prn, 4); | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment