Flash版的拉格朗日插值程序
依次输入n,x,x[i],y[i],输一个打个回车。
下面是另一种风格
源代码:
请用NeoSwiff C#编译器编译,编译的结果是生成flash
/*
|
| ZhangLi CopyRight 2005
|
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace eval
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox CommandBox=null;
private System.Windows.Forms.TextBox InputBox=null;
double[] y;
double[] x;
int n;
int i=0;
double xx,yy;
int state=0;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.CommandBox=new System.Windows.Forms.TextBox();
this.InputBox=new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.CommandBox.Multiline=true;
this.CommandBox.ReadOnly=true;
this.CommandBox.ScrollBars=System.Windows.Forms.ScrollBars.Vertical;
this.CommandBox.Text="Lagrange interpolation Demo Program\nZhangLi CopyRight 2005\nhttp://www.cnblogs.com/jonlee\n\nn=";
this.CommandBox.BackColor=System.Drawing.Color.Black;//是否黑底
this.CommandBox.ForeColor=System.Drawing.Color.White;//是否白字
this.CommandBox.Location=new System.Drawing.Point(0,0);
this.CommandBox.Width=400;
this.CommandBox.Height=275;
this.InputBox.Multiline=false;
this.InputBox.Location=new System.Drawing.Point(0,275);
this.InputBox.Width=400;
this.InputBox.Height=25;
this.InputBox.KeyPress += new KeyPressEventHandler(getCommand);
this.InputBox.MouseDown += new System.Windows.Forms.MouseEventHandler(TMouseDown);
this.InputBox.Text="Enter the number here";
this.Controls.Add(this.CommandBox);
this.Controls.Add(this.InputBox);
//设置“平面”样式
//System.Windows.Forms.Skins.Flat.FlatSkin fsk=null;
//fsk=new System.Windows.Forms.Skins.Flat.FlatSkin();
//this.Skin=fsk;
this.Text = "Form1";
this.BackColor=System.Drawing.Color.White;
this.ResumeLayout(false);
y=new double[100];
x=new double[100];
}
private void TMouseDown( Object sender, MouseEventArgs e )
{
if(this.InputBox.Text=="Enter the number here")
{
this.InputBox.Text="";
}
}
private void getCommand( object sender, KeyPressEventArgs e )
{
if(e.KeyChar=='\r')
{
this.CommandBox.Text+=this.InputBox.Text;
if(state==0)
{
n=System.Int32.Parse(this.InputBox.Text);
this.CommandBox.Text+="\nx=";
}
else if(state==1)
{
xx=System.Double.Parse(this.InputBox.Text);
this.CommandBox.Text+="\nxi=";
}
else if(state%2==0 && i<n)
{
x[i]=System.Double.Parse(this.InputBox.Text);
this.CommandBox.Text+="\nyi=";
}
else if(i<n)
{
y[i]=System.Double.Parse(this.InputBox.Text);
if(i<n-1)
{
this.CommandBox.Text+="\nxi=";
++i;
}
else
{
yy=Lagrange(x,y,xx,n);
this.CommandBox.Text+="\nResult: x="+ xx.ToString() +" y="+yy.ToString()+"\n";
i=0;
state=-1;
this.CommandBox.Text+="\nn=";
}
}
++state;
this.InputBox.Text="";
this.CommandBox.ScrollToBottom();
}
}
private double Lagrange(double[] x,double[] y,double xx,int n)
{
double p;
double yx=0;
for(int k=0;k<n;++k)
{
p=1;
for(int j=0;j<n;++j)
{
if(j!=k)
{
p = p * ( xx - x[j] ) / ( x[k] - x[j] );
}
}
yx = yx + p * y[k];
}
return yx;
}
static void Main()
{
System.Environment.FSCommand("Allowscale","false");
System.Environment.FSCommand("showmenu","false");
Application.Run( new Form1() );
}
}
}
|
| ZhangLi CopyRight 2005
|
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace eval
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox CommandBox=null;
private System.Windows.Forms.TextBox InputBox=null;
double[] y;
double[] x;
int n;
int i=0;
double xx,yy;
int state=0;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.CommandBox=new System.Windows.Forms.TextBox();
this.InputBox=new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.CommandBox.Multiline=true;
this.CommandBox.ReadOnly=true;
this.CommandBox.ScrollBars=System.Windows.Forms.ScrollBars.Vertical;
this.CommandBox.Text="Lagrange interpolation Demo Program\nZhangLi CopyRight 2005\nhttp://www.cnblogs.com/jonlee\n\nn=";
this.CommandBox.BackColor=System.Drawing.Color.Black;//是否黑底
this.CommandBox.ForeColor=System.Drawing.Color.White;//是否白字
this.CommandBox.Location=new System.Drawing.Point(0,0);
this.CommandBox.Width=400;
this.CommandBox.Height=275;
this.InputBox.Multiline=false;
this.InputBox.Location=new System.Drawing.Point(0,275);
this.InputBox.Width=400;
this.InputBox.Height=25;
this.InputBox.KeyPress += new KeyPressEventHandler(getCommand);
this.InputBox.MouseDown += new System.Windows.Forms.MouseEventHandler(TMouseDown);
this.InputBox.Text="Enter the number here";
this.Controls.Add(this.CommandBox);
this.Controls.Add(this.InputBox);
//设置“平面”样式
//System.Windows.Forms.Skins.Flat.FlatSkin fsk=null;
//fsk=new System.Windows.Forms.Skins.Flat.FlatSkin();
//this.Skin=fsk;
this.Text = "Form1";
this.BackColor=System.Drawing.Color.White;
this.ResumeLayout(false);
y=new double[100];
x=new double[100];
}
private void TMouseDown( Object sender, MouseEventArgs e )
{
if(this.InputBox.Text=="Enter the number here")
{
this.InputBox.Text="";
}
}
private void getCommand( object sender, KeyPressEventArgs e )
{
if(e.KeyChar=='\r')
{
this.CommandBox.Text+=this.InputBox.Text;
if(state==0)
{
n=System.Int32.Parse(this.InputBox.Text);
this.CommandBox.Text+="\nx=";
}
else if(state==1)
{
xx=System.Double.Parse(this.InputBox.Text);
this.CommandBox.Text+="\nxi=";
}
else if(state%2==0 && i<n)
{
x[i]=System.Double.Parse(this.InputBox.Text);
this.CommandBox.Text+="\nyi=";
}
else if(i<n)
{
y[i]=System.Double.Parse(this.InputBox.Text);
if(i<n-1)
{
this.CommandBox.Text+="\nxi=";
++i;
}
else
{
yy=Lagrange(x,y,xx,n);
this.CommandBox.Text+="\nResult: x="+ xx.ToString() +" y="+yy.ToString()+"\n";
i=0;
state=-1;
this.CommandBox.Text+="\nn=";
}
}
++state;
this.InputBox.Text="";
this.CommandBox.ScrollToBottom();
}
}
private double Lagrange(double[] x,double[] y,double xx,int n)
{
double p;
double yx=0;
for(int k=0;k<n;++k)
{
p=1;
for(int j=0;j<n;++j)
{
if(j!=k)
{
p = p * ( xx - x[j] ) / ( x[k] - x[j] );
}
}
yx = yx + p * y[k];
}
return yx;
}
static void Main()
{
System.Environment.FSCommand("Allowscale","false");
System.Environment.FSCommand("showmenu","false");
Application.Run( new Form1() );
}
}
}
工程文件下载(Visual Studio 2003版本)
因为这个算法实现比较简单,所以我想到用这种特别方法来做实验,其实也说明了很多算法是可以独立于具体语言的。