⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1.cpp

📁 数值积分
💻 CPP
字号:
#include "Int.h"

Integrate::Integrate()
{
     ifstream infile("input.dat");
     if(!infile){
             cout<<"sorry!The input data can not be loaded!"<<endl;
             exit(-1);
	 }

     else{int i,j;
          for(i=0;i<n;i++)
             for(j=0;j<=i;j++)
                 infile >> Xknot[i][j];//输入求积节点

          for (i=0;i<n;i++)
             for(j=0;j<=i;j++)
                 infile >> Aknot[i][j];//输入求积系数
                 infile >> a >>b;    //输入区间端点值
	 }
     infile.close();
}


void Integrate::Inte()
      {int i;
       double temp,result=0.0;//置result初值为零
       
      cout<<"Please input m(1<=m<=5)"<<endl<<"m=";
      cin>>m;
      m=m-1;//数组是从零开始

      if(a==-1&&b==1)
	     {for(i=0;i<=m;i++)
	       {temp=Function(Xknot[m][i]);//若a=-1且b=1,直接使用式(2.2)

             result+=Aknot[m][i]*temp;
	       }
	      }

      else{
           for(i=0;i<=m;i++)
		      {
               temp=Function(0.5*(b-a)*Xknot[m][i]+0.5*(b+a));//否则,使用(2.3)式计算
               result+=Aknot[m][i]*temp;
		       }
               result*=0.5*(b-a);//结果再乘上(a+b)/2
	       }
       cout<<"The result is"<<result<<endl;

}

double Integrate::Function(double x)//要计算的函数sin(x^2)
        { double y;
           y=sin(x*x);
           return y;
        }


int main()
    { char cmd;
      cmd='n';

       while(cmd!='y')//当从键盘读入command为yes时,跳出。
	   {Integrate inter;
             inter.Inte();

             cout<<"Do you want to end?(y/n) "<<endl;
             cin >>cmd;
	   }

        return 1;
     }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -