Skip to content

Instantly share code, notes, and snippets.

@LifeMoroz
Created October 7, 2012 14:15
Show Gist options
  • Save LifeMoroz/3848506 to your computer and use it in GitHub Desktop.
Save LifeMoroz/3848506 to your computer and use it in GitHub Desktop.
Lab_7
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
void *uk;
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))/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 eps, double (*f)(double) )
{
double result, h;
I_print struct1;
double i;
int n=2;
bool f1;
if (exp <(abs( f( a + ((b-a)/n) * (i - 0.5)))- abs( f( a + (b-a)/(n-1) * (i - 0.5)))))
f1=true;
while (f1==true)
{
n+=1;
}
h = (b-a)/n; //Шаг сетки
result = 0.0;
for(i=1; i <= n; i++){
result += f( a + h * (i - 0.5) ); //Вычисляем в средней точке и добавляем в сумму
}
result *= h;
struct1.i_sum=result;
struct1.n=n;
switch (g)
{
case 1:
struct1.name='x';
break;
case 2:
struct1.name='sin( 22 * x)';
break;
case 3:
struct1.name='x^4';
break;
case 4:
struct1.name='arctg(x)';
break;
}
return 0;
}
int main(void){
double integral, a, b, n;
IntRec(0,0.001,100,f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment