import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
import javax.swing.Box;
class chazhifa extends Frame implements ActionListener {
TextArea area, result, result2;
Button butt, butt2, butt3;
Label lab, lab3;
Box box1, box2, box3, box4, box5, box6, box7;
TextField field, field2;
chazhifa() {
super("插值法:");
area = new TextArea(5, 34);
result = new TextArea(4, 8);
result.setEditable(false);
result2 = new TextArea(4, 8);
result2.setEditable(false);
butt = new Button("拉格朗日插值:");
butt3 = new Button("牛顿插值:");
butt2 = new Button("清除");
field = new TextField(5);
field2 = new TextField(5);
lab = new Label("依次输入xi和yi的值:");
lab3 = new Label("输入n和x:");
box2 = Box.createHorizontalBox();
box1 = Box.createVerticalBox();
box3 = Box.createHorizontalBox();
box4 = Box.createHorizontalBox();
box5 = Box.createHorizontalBox();
box6 = Box.createHorizontalBox();
box7 = Box.createVerticalBox();
setLayout(new FlowLayout());
box2.add(lab3);
box2.add(field);
box2.add(field2);
box2.add(butt2);
box3.add(lab);
box4.add(area);
box5.add(butt);
box5.add(butt3);
box6.add(result);
box6.add(result2);
box7.add(Box.createVerticalStrut(6));
box7.add(box2);
box7.add(Box.createVerticalStrut(5));
box7.add(box3);
box7.add(Box.createVerticalStrut(5));
box7.add(box4);
box7.add(Box.createVerticalStrut(5));
box7.add(box5);
box7.add(Box.createVerticalStrut(5));
box7.add(box6);
box7.add(Box.createVerticalStrut(5));
add(box7);
butt.addActionListener(this);
butt2.addActionListener(this);
butt3.addActionListener(this);
validate();
setBounds(200, 200, 300, 330);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
String sa = area.getText().trim();
String sn = field.getText().trim();
String sx = field2.getText().trim();
int n = Integer.parseInt(sn);
float xx = Float.parseFloat(sx);
StringTokenizer fenxi = new StringTokenizer(sa, "#\n");
float x[] = new float[n + 1];
float y[] = new float[n + 1];
int i = 0, j = 0, flag = 1, k;
float temp, lk = 1, sumln = 0, b = 1, nn = 0;
float t[] = new float[n];
float f[] = new float[n + 1];
while (fenxi.hasMoreTokens()) {
String str = fenxi.nextToken();
StringTokenizer fenxi2 = new StringTokenizer(str, " ");
while (fenxi2.hasMoreTokens()) {
String str2 = fenxi2.nextToken();
temp = Float.parseFloat(str2);
if (flag == 1) {
x[i] = temp;
i++;
}
if (i == (n + 1)) {
flag = 0;
i = 0;
continue;
}
if (flag == 0) {
y[j] = temp;
j++;
}
if (j == (n + 1)) {
flag = 1;
j = 0;
continue;
}
}
}
if (e.getSource() == butt) {
for (k = 0; k <= n; k++) {
for (i = 0; i <= n; i++) {
if (i != k)
lk *= ((xx - x[i]) / (x[k] - x[i]));
}
sumln += (lk * y[k]);
lk = 1;
}
result.setText("Ln(" + xx + ")=\n" + sumln);
}
if (e.getSource() == butt2) {
area.setText(null);
field.setText(null);
field2.setText(null);
result.setText(null);
result2.setText(null);
}
if (e.getSource() == butt3) {
f[0] = y[0];
for (k = 1; k <= n; k++) {
for (i = 0; i < n - k + 1; i++)
t[i] = (y[i + 1] - y[i]) / (x[i + k] - x[i]);
f[k] = t[0];
for (i = 0; i < n - k + 1; i++)
y[i] = t[i];
}
nn = f[0];
for (i = 1; i <= n; i++) {
b = f[i];
for (j = 0; j <= i - 1; j++)
b *= (xx - x[j]);
nn += b;
}
result2.setText("Nn(" + xx + ")=\n" + nn);
}
}// action
}
public class chazhi {
public static void main(String[] args) {
chazhifa cha = new chazhifa();
}
}
/* x: 0.5635
* 0.56160 0.56280 0.56401 0.56521#0.82741 0.82659 0.82577 0.82495
*/