📄 newton.java
字号:
/*@author 邱宇峰 ,software project031
*2004,9,20
*/
import java.awt.*;
import java.awt.event.*;
public class newton extends Frame implements MouseListener,ActionListener{
static newton frm=new newton();
static Button b1;
static int array[][]=new int[30][2];
static int X=0;
static int count=0;
static double p=0;
public static void main(String[] args){
frm.setTitle("牛顿插值法");
b1=new Button("click to start");
frm.add(b1,"South");
frm.setSize(500,500);
frm.setLocation(200,200);
frm.setVisible(true);
frm.addMouseListener(frm);
b1.addActionListener(frm);
frm.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}
});
}
public void mouseEntered(MouseEvent e){ }
public void mousePressed(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
public void mouseReleased(MouseEvent e){ }
/*this mouseevent is to get the basic spots
*/
public void mouseClicked(MouseEvent e){
Graphics g=getGraphics();
int x=e.getX();
int y=e.getY();
array[count][0]=x;array[count][1]=y;
g.drawString("。",x,y);
count++;
}
/*this actionevent is to draw the line
*/
public void actionPerformed(ActionEvent e){
double arrayD[]=new double[count];
Graphics g=getGraphics();
creatA cA=new creatA(count);
arrayD=cA.creatArrayD(array);
for(X=array[0][0];X<array[count-1][0];X++){
for(long h=0;h<5000000;h++);
double p=arrayD[count-1];
for(int i=count-2;i>=0;i--){
p=arrayD[i]+p*((double)X-(double)array[i][0]);
}
g.drawString(".",X,(int)p);
}
}
}
/*this class is to calculate the constant number of 'a'
*/
class creatA{
static double arrayD[];
static int count;
public creatA(int number){
arrayD=new double[number];
count=number;
}
double[] creatArrayD(int location[][]){
for(int i=0;i<count;i++) arrayD[i]=(double)location[i][1];
for(int i=1;i<count;i++){
for(int j=count-1;j>=i;j--){
arrayD[j]=((double)arrayD[j]-(double)arrayD[j-1])/((double)location[j][0]-(double)location[j-i][0]);
}
}
return (arrayD);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -