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

📄 leelview.cpp

📁 可以实现 拟合 龙各库塔 等计算方法的多种算法
💻 CPP
字号:
// leelView.cpp : implementation of the CLeelView class
//

#include "stdafx.h"
#include "leel.h"

#include "DlgData.h"
#include "math.h" 

#include "leelDoc.h"
#include "leelView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLeelView

IMPLEMENT_DYNCREATE(CLeelView, CView)

BEGIN_MESSAGE_MAP(CLeelView, CView)
	//{{AFX_MSG_MAP(CLeelView)
	ON_COMMAND(ID_DataInput, OnDataInput)
	ON_COMMAND(ID_Newton, OnNewton)
	ON_COMMAND(ID_REAE, OnReae)
	ON_COMMAND(ID_Romberg_Edit, OnRombergEdit)
	ON_COMMAND(ID_NiHeFk, OnNiHeFk)
	ON_COMMAND(ID_DataEdit_Read, OnDataEditRead)
	ON_COMMAND(ID_dragon, Ondragon)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLeelView construction/destruction

CLeelView::CLeelView()
{
	// TODO: add construction code here

}

CLeelView::~CLeelView()
{
}

BOOL CLeelView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CLeelView drawing

void CLeelView::OnDraw(CDC* pDC)
{
	CLeelDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CLeelView printing

BOOL CLeelView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CLeelView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CLeelView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CLeelView diagnostics

#ifdef _DEBUG
void CLeelView::AssertValid() const
{
	CView::AssertValid();
}

void CLeelView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CLeelDoc* CLeelView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLeelDoc)));
	return (CLeelDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLeelView message handlers

void CLeelView::OnDataInput() 
{
	// TODO: Add your command handler code here
	CDlgData dlg;    //定义一个对话框对象
   int i,j;
   CString str;
   //首先将数清空,赋初始值0
   for(i=0;i<3;i++)
	  for(j=0;j<5;j++)
		  arrRef[i][j]=0.0;
	  //刷新窗口
	  Invalidate();
	  UpdateWindow();
	  //循环三次,按行输入数据
	  for(i=0;i<3;i++)
	  {
		  if(IDOK==dlg.DoModal())
		  {
			  str=dlg.m_strData;
			  SepString(i,arrRef[i],5,str);
		  }
	  }
	
}

void CLeelView::OnNewton() 
{
	// TODO: Add your command handler code here 
	 AfxMessageBox("你确定现在使用牛顿插值法计算。");
       Invalidate();
	UpdateWindow();
	newtondd(arrX,arrY,doubleX);// TODO: Add your command handler code her
	
}

 

void CLeelView::SepString(int intPos, double *arr, int n, CString str)
{
   int j,k,l;
	CString strValue;
	int flag;//标识位。标识当前是负数还是正数

	j=0;
	flag=0;
	strValue="";
	for(l=0;l<str.GetLength();l++)
	{
		if(j<n&&(str.GetAt(l)==' '||str.GetAt(l)=='-'))
		{
			arr[j]=StrToValue(strValue);
			if (flag==-1)
				arr[j]=(-1)*arr[j];
			flag=0;
			if(str.GetAt(l)=='-')
				flag=-1;
			else
				j++;
			strValue="";
		}
		else
			if(isdigit(str.GetAt(l))||str.GetAt(l)=='.')
				strValue=strValue+str.GetAt(l);
			else 
				strValue="Err";
	}
	if(j<n)
		arr[j]=StrToValue(strValue);

	CClientDC dc(this);
	for(k=0;k<n;k++)
	{
		CString strTemp;
		strTemp.Format("%5.3f ",arr[k]);
		dc.TextOut(k*100,intPos*15,strTemp);
	}
}

double CLeelView::StrToValue(CString strNum)
{
    int i,j;
	int flag;
	char c;
	double dblValue;

	if(strNum=="Err")//如果字符串参数为"Err",则返回值设为0
		return(0.0);

	flag=0;
	dblValue=0.0;
	for(i=0;i<strNum.GetLength();i++)
	{
		c=strNum.GetAt(i);
		if(c=='.')
			flag=-1;

		switch(flag)
		{
		case 0:
			dblValue=dblValue+(c-48);
			flag++;
			break;
		case 1:
			dblValue=dblValue*10+(c-48);
			break;
		case -1:
			flag--;
			break;
		default:
			double p=0.1;
			for(j=-1;j>=flag+2;j--)
				p=p*0.1;
			dblValue=dblValue+(c-48)*p;
			flag--;
			break;
		}
	}
	return (dblValue);
}

void CLeelView::newtondd(double*rrX,double*rrY,double doubX )
{
    double x[5],y[5];
	double s,p,x0,q;
	int i,j,m=0;
       i=0;
	   for(j=0;j<5;j++)
		{
			 
	       x[j]=rrX[j];
		    y[j]=rrY[j];
		}
	   x0=doubX;
	   s=y[0];
	   p=1;
   for(i=0;i<4;i++)
   {
	  for(j=0;j<4-i;j++)
	  {  
	    CClientDC dc(this);
		 CString strResult;
		y[j]=(y[j+1]-y[j])/(x[j+i+1]-x[j]);
	
            strResult.Format("%10.4f ",y[j]);
	        dc.TextOut(20+i*70,115+15*j+7*i,strResult);
	
	  }

		   q=x0-x[i];
		   p=p*q;
		   s=s+y[i]*p;
	   }
	   
	   CClientDC dc(this);
	   CString strResult;
	 //  int m;
	 //  m=GetLength(*rrX);//获得数组的长度
	   

	   strResult.Format("牛顿插值的原始数据为:");
	    dc.TextOut(10,10,strResult);
		for(i=0;i<5;i++)
		{
		strResult.Format("%4.4f ",x[i]);
	    dc.TextOut(i*60+50,30,strResult); 

		strResult.Format("%4.4f  ",y[i]);
	    dc.TextOut(i*60+25,50,strResult);
		}

   		strResult.Format("插值点x0为:0.596");
	    dc.TextOut(10,70,strResult); 

		strResult.Format("差商表为为: ");
	    dc.TextOut(10,90,strResult); 


		strResult.Format("插值节点的值为: ");
	    dc.TextOut(10,200,strResult); 

        strResult.Format("%10.4f ",s);
	    dc.TextOut(130,(i+5)*20,strResult); 
}

void CLeelView::OnReae() 
{
	// TODO: Add your command handler code here
	CStdioFile f;
   CString strValue;
   //open file.
  f.Open("qxnh.txt",CFile::modeRead | CFile::typeText);
    //刷新windows
    Invalidate();
     UpdateWindow();
   //read noe row data save to arrX  ,xi
   f.ReadString(strValue);
   SepString(0,arrX,5,strValue);
    //read noe row data save to arrY ,yi
     f.ReadString(strValue);
     SepString(1,arrY,5,strValue);
    //read one row data save to intN ,number
     f.ReadString(strValue);
     SepString(2,&doubleX,1,strValue);
     //close file
     f.Close();	
}

void CLeelView::OnRombergEdit() 
{
	// TODO: Add your command handler code here
       
	AfxMessageBox("现在使用Romberg法计算积分函数f(x)=sinx/x。\n它的积分上限是0,积分下限是1,\n 精确到0.0000001\n");
        Invalidate();
	UpdateWindow();
	Romberg(0,1,0.000001);
	
}

void CLeelView::Romberg(double m, double n, double ep)
{
	long double a,b,h,s;
    long double S1,S2,T1,T2,R1,R2,C1,C2;
   int i,k,NM=1;
 //  int j;
  // int a0,b0,c0,d0,a1,b1,c1,d1;
   a=m;
   b=n;
   h=(b-a)/2;
   k=0;
   T2=h*(Rfunction(a)+Rfunction(b));
   S2=0.0;C2=0.0;R2=0.0;
   do 
   {
	R1=R2;
	T1=T2;
	S1=S2;
	C1=C2;
  	s=0;
   for (i=1;i<=NM;i++)
      s=s+Rfunction(a+(2*i-1)*h);
   T2=T1/2+s*h;
   S2=(4*T2-T1)/3;
   C2=(16*S2-S1)/15;
    R2=(64*C2-C1)/63;
//	CClientDC dc(this);
//	   CString strResult;
//      for(i=0;i<4;i++)
//	  {
//		 strResult.Format("%10.6f ",R2);
//	    dc.TextOut(10,10,strResult); 
//		strResult.Format("%10.6f ",R2);
//	    dc.TextOut(10,10,strResult); 
//	  }
    NM=NM*2;
	k=k+1;
	h=h/2.0;
   }
   while(fabs(R2-R1)>ep);

     CClientDC dc(this);
	   CString strResult;
        strResult.Format("积分上限为:%9.2f ",b);
	    dc.TextOut(10,15,strResult); 
        strResult.Format("积分下限为:%10.2f ",a);
	    dc.TextOut(10,40,strResult); 

        strResult.Format("函数sin(x)/x的值是 ");
	    dc.TextOut(10,60,strResult); 

        strResult.Format("%10.7f ",R2);
	    dc.TextOut(10,80,strResult); 

}

long double CLeelView::Rfunction(double x)
{
	long double y;
	y=x;
	if(y==0)
		return 1;
	else
		return sin(y)/y;


}

 

void CLeelView::OnNiHeFk() 
{
	// TODO: Add your command handler code here
	AfxMessageBox("现在使用拟合法计算。");
       Invalidate();
	UpdateWindow();
	NiHe(arrRef1[0],arrRef1[1],8,(int)intN);
	
}

void CLeelView::OnDataEditRead() 
{
	// TODO: Add your command handler code here
CStdioFile f;
CString strValue;
//open file.
f.Open("Rungion.txt",CFile::modeRead | CFile::typeText);
//刷新windows
Invalidate();
UpdateWindow();
//read noe row data save to arrX  ,xi
f.ReadString(strValue);
SepString(0,arrRef1[0],8,strValue);
//read noe row data save to arrY ,yi
f.ReadString(strValue);
SepString(1,arrRef1[1],8,strValue);
//read one row data save to intN ,number
f.ReadString(strValue);
SepString(2,&intN,1,strValue);
//close file

f.Close();
	
}

void CLeelView::NiHe(double x[], double y[], int n, int m)
{
    int i,j,k;
    double ff[10];
	double c[3];
    double a[3][4];

     for (k=0;k<=2*m;k++)
	{ 
		ff[k]=0;
		for(i=0;i<n;i++)
			ff[k]=ff[k]+pow(x[i],(double)k);
	}
	for(k=0;k<=m;k++)    
	{ 
		c[k]=0;
		for(i=0;i<n;i++)
			c[k]=c[k]+y[i]*pow(x[i],(double)k);
	}
	for(k=0;k<=m;k++)
		for(j=0;j<=m;j++)
			a[k][j]=ff[k+j];
	for(i=0;i<=m;i++)
			a[i][3]=c[i];
	for(i=0;i<3;i++)
	{
		
		CClientDC dc(this);
		CString strResult;
		   for(j=0;j<4;j++)
		   {
			strResult.Format("%3.2f ",a[i][j]);
		    dc.TextOut(j*50+30,i*30+40,strResult);
		
		   }
//		     			
	}
	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
			{
			CClientDC dc(this);
		CString strResult;
			 strResult.Format("----------------------------------");
	          dc.TextOut(j*50+20,i*30+25,strResult);
			}
	}


	gaussj(a,3,0.001);
	

	CStdioFile f;
	CString strResult;
	f.Open("run.txt",CFile::modeWrite | CFile::typeText);
	strResult="";
	for(i=0;i<=m;i++)
	{  strResult.Format("x[%1d]=%4.4f\n",i+1,a[i][m+1]);
	   f.WriteString(strResult);
	}
	f.Close();


}

void CLeelView::gaussj(double b[][4], int n, double ep)
{
	 double c[3][4];
	int i,j,k;
	double m;
	int ip=1;
	  //复制方程组的增广矩阵,保证元增广矩阵的数据不被破坏。
	for (i=0;i<3;i++)
		for (j=0;j<4;j++)
			c[i][j]=b[i][j];
	//消元。
    for(k=0;k<=n-2;k++)
	{
	                  //找主元
       if(fabs(c[k][k])<ep)
            ip=-1;
    
          else
		   for (i=k+1;i<=n-1;i++)  //消元
			 {
			  m=c[i][k]/c[k][k];
		    	for(j=k+1;j<=n;j++)
        		c[i][j]=c[i][j]-m*c[k][j];
			 }   
		}		 
		//回代
		if(ip==-1)
		{
			CClientDC dc(this);
			CString strErr;
			strErr="Error";
			dc.TextOut(0,0,strErr);
		}
		else
			for(i=n-1;i>=0;i--)
			{
				for(j=i+1;j<n;j++)
					c[i][n]=c[i][n]-c[i][j]*c[j][n];
				c[i][n]=c[i][n]/c[i][i];
			}

    
	//打印结果
  			CClientDC dc(this);
		CString strResult;
		strResult.Format("拟合的系数增广矩阵为:");
		    dc.TextOut(15,10,strResult); 

		   strResult.Format("拟合函数是:");
		    dc.TextOut(15,140,strResult); 

		 strResult.Format("y=%3.4f+%3.4fx%3.4fx^2",c[0][n],c[1][n],c[2][n]);
		    dc.TextOut(15,160,strResult);
		 
}

void CLeelView::Ondragon() 
{
	// TODO: Add your command handler code here
		AfxMessageBox("使用Dragon-tower计算。");
       Invalidate();
	UpdateWindow();
	DragonTower(1,1.4,1,1,10);
	
}

void CLeelView::DragonTower(double a0, double b0, double x0, double y0, int n0)
{
	int i,n=n0;
	double a,b,x=x0,yy=y0,h,k1,k2,k3,k4;
    double y[10];
	a=a0;
	b=b0;
	h=(b-a)/n;
	y[0]=yy;
	for(i=1;i<=n;i++)
	{
		k1=h*func(x,yy);
       k2=h*func(x+h/2,yy+k1/2);
	   k3=h*func(x+h/2,yy+k2/2);
       k4=h*func(x+h,yy+k3);
       y[i]=yy+(k1+2*k2+2*k3+k4)/6;
       x=x+h;
       yy=y[i];
	 //  CClientDC dc(this);
    // CString strResult;
    //  if(i==2)
	 // {
	//   strResult.Format("%4.4f %4.4f %4.4f %4.4f",k1,k2,k3,k4);
     //   dc.TextOut(50,200,strResult);
	//  }
	}
	  


     CClientDC dc(this);
     CString strResult;
	 strResult.Format(" 次数       x             y");
        dc.TextOut(20,10,strResult);
     for(i=0;i<=n;i++)
	 { strResult.Format("   %d       %2.2f         %5.6f",i,1+(h*i),y[i]);
        dc.TextOut(20,i*20+40,strResult);
	 }


}

double CLeelView::func(double m, double n)
{
	double x,y;
	x=m;
	y=n;
	return x*x+x*x*x*y;
}

⌨️ 快捷键说明

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