?? leelview.cpp
字號(hào):
// 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; //定義一個(gè)對(duì)話框?qū)ο? int i,j;
CString str;
//首先將數(shù)清空,賦初始值0
for(i=0;i<3;i++)
for(j=0;j<5;j++)
arrRef[i][j]=0.0;
//刷新窗口
Invalidate();
UpdateWindow();
//循環(huán)三次,按行輸入數(shù)據(jù)
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("你確定現(xiàn)在使用牛頓插值法計(jì)算。");
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;//標(biāo)識(shí)位。標(biāo)識(shí)當(dāng)前是負(fù)數(shù)還是正數(shù)
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")//如果字符串參數(shù)為"Err",則返回值設(shè)為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);//獲得數(shù)組的長(zhǎng)度
strResult.Format("牛頓插值的原始數(shù)據(jù)為:");
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("插值點(diǎn)x0為:0.596");
dc.TextOut(10,70,strResult);
strResult.Format("差商表為為: ");
dc.TextOut(10,90,strResult);
strResult.Format("插值節(jié)點(diǎn)的值為: ");
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("現(xiàn)在使用Romberg法計(jì)算積分函數(shù)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("函數(shù)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("現(xiàn)在使用擬合法計(jì)算。");
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;
//復(fù)制方程組的增廣矩陣,保證元增廣矩陣的數(shù)據(jù)不被破壞。
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];
}
//打印結(jié)果
CClientDC dc(this);
CString strResult;
strResult.Format("擬合的系數(shù)增廣矩陣為:");
dc.TextOut(15,10,strResult);
strResult.Format("擬合函數(shù)是:");
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計(jì)算。");
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(" 次數(shù) 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;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -