#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <fstream.h>

double Fun(double x)
{
 return (exp(-x*x/2));
}
//-------------------------TRAP
double Trap(double a,double b, int n)
{
  double I,X=a;
  double h=(b-a)/n;
  I=Fun(X);
  for(int i=0;i<n-1;i++)
  {
	X+=h;
	I+=2*(Fun(X));
  }
  I+=Fun(b);
  h=h/2;
  return I*h;
}
//-------------------------------------SIM
double Sim(double a,double b, int n)
{
  double I,X=a;
  double h=(b-a)/n;

  I=Fun(a);
  for(int i=1;i<n;i++)
  {
	 X+=h;
	 if(i%2==0)I+=2*Fun(X);
	 else I+=4*Fun(X);
  }
  I+=Fun(b);
  h=h/3;
  return I*h;
}
//---------------------------GAUSS
double F(double M,double a,double b)
{
 return ((b-a)/2*Fun(M*(b-a)/2+(b+a)/2));
}
double Gauss(double a,double b, int n)
{
  double I=0;
  double M=0,A=0;

  ifstream f("U:\\Chis\\data.txt");
  /*if (in==NULL)
	{fclose(in);
	 return 0;
	}*/
  for(int i=1;i<=n/2;i++)
  {
	f>>M;
	f>>A;
	I+=(A*F(M,a,b)+A*F(-1.0*M,a,b));
  }
  return I;
}

void main()
{
 clrscr();
 cout<<"N = 0  Znachenie I      = 2.3925"<<endl;
 cout<<"N = 28 Znachenie Trap   = "<<Trap(-2,2,28)<<endl;
 cout<<"N = 14 Znachenie Sim    = "<<Sim(-2,2,14)<<endl;
 cout<<"N = 7  Znachenie Gauss  = "<<Gauss(-2,2,6)<<endl;
 getch();
}