📄 thdeeq.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package ThDeEq;import java.awt.*;import java.awt.event.*;/** * * @author liuwei */public class ThDeEq extends WindowAdapter implements ActionListener{ Frame f=new Frame("一元三次方程解法器"); Label lb1=new Label("X^3+");Label lb2=new Label("X^2+"); Label lb3=new Label("X+");Label lb4=new Label("=0"); TextField tf[]=new TextField[7]; Button btn1=new Button("计算");Button btn2=new Button("清除"); Label Rootlb[]=new Label[3]; Panel p[]=new Panel[3]; public ThDeEq(){ int i; f.setLocation(400, 300); f.setSize(400,200); f.setLayout(new BorderLayout()); f.addWindowListener(this); for(i=0;i<7;i++){ tf[i]=new TextField(3); } //************************************************************** p[0]=new Panel(); p[0].setSize(150,30); p[0].setLayout(new FlowLayout()); p[0].add(tf[0]);p[0].add(lb1);p[0].add(tf[1]);p[0].add(lb2);p[0].add(tf[2]); p[0].add(lb3);p[0].add(tf[3]);p[0].add(lb4); p[0].setBackground(new Color(100,200,150)); f.add(p[0],BorderLayout.NORTH); //************************************************************** p[1]=new Panel(); p[1].setBackground(new Color(200,130,100)); //p[1].setLocation(300, 300); p[1].setLayout(new GridLayout(3,2)); for(i=0;i<3;i++){ Rootlb[i]=new Label("X"+(i+1)+"="); p[1].add(Rootlb[i]);p[1].add(tf[i+4]);tf[i+4].setEditable(false); } f.add(p[1],BorderLayout.SOUTH); //************************************************************** p[2]=new Panel(); p[2].setLayout(new FlowLayout()); p[2].setBackground(new Color(250,200,240)); p[2].add(btn1);p[2].add(btn2); f.add(p[2],BorderLayout.CENTER); btn1.addActionListener(new btn1ActionAdapter()); btn2.addActionListener(new btn2ActionAdapter()); //************************************************************** f.setVisible(true); } public static void main(String[] args){ new ThDeEq(); } public void windowClosing(WindowEvent we){ System.exit(1); } class btn1ActionAdapter implements ActionListener{ public void actionPerformed(ActionEvent ae) { double a,b,c,d; double A,B; double p,q,m;//(x+m)^3+p(x+m)+q=0 double f,g;//a*(x-x1)(x^2+f*x+g)=0 double x1,x2,x3; x1=0;x2=0;x3=0; int flag; a=Double.parseDouble(tf[0].getText()); b=Double.parseDouble(tf[1].getText()); c=Double.parseDouble(tf[2].getText()); d=Double.parseDouble(tf[3].getText()); if(a!=0) { m=b/3/a; p=b*b/3/a/a-c/a; q=m*m*m-p*m-d/a; A=-1*q/2-Math.pow((Math.pow(q/2,2)+Math.pow(p/3,3)),0.5); B=-1*q/2+Math.pow((Math.pow(q/2,2)+Math.pow(p/3,3)),0.5); double A1,B1; if (A<=0) A1=-1*Math.pow(-1*A,1.0/3); else A1=Math.pow(A,1.0/3); if (B<=0) B1=-1*Math.pow(-1*B,1.0/3); else B1=Math.pow(B,1.0/3); x1=A1+B1-b/3/a; f=b/a+x1; g=c/a+f*x1; double delta=f*f-4*g; if(delta>=0) { x2=(-1*f+Math.sqrt(delta))/2; x3=(-1*f-Math.sqrt(delta))/2; flag=1; } else { flag=0; } } else { double delta1; delta1=c*c-4*b*d; if(b==0) { flag=-1; x1=-1*d/c; } if(delta1>=0) { x1=(-b+(double)Math.sqrt(delta1))/(2*b); x2=(-b-(double)Math.sqrt(delta1))/(2*b); flag=2; } else flag=-2; } //************************************************************** if(flag==-2) { tf[4].setText("NoRealRoot");tf[5].setText("NoRealRoot"); tf[6].setText("NoRealRoot"); } if(flag==-1) { tf[4].setText(""+x1);tf[5].setText("NoRealRoot"); tf[6].setText("NoRealRoot"); } if(flag==0) { tf[4].setText(""+x1);tf[5].setText("NoRealRoot"); tf[6].setText("NoRealRoot"); } if(flag==1) { tf[4].setText(""+x1);tf[5].setText(""+x2); tf[6].setText(""+x3); } if(flag==2) { tf[4].setText(""+x1);tf[5].setText(""+x2); tf[6].setText(""+x3); } } } class btn2ActionAdapter implements ActionListener{ public void actionPerformed(ActionEvent ae){ int j; for(j=0;j<7;j++){ tf[j].setText(""); } } } public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -