资源描述
package Computer;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.util.LinkedList;import java.text.NumberFormat;public class Cacultor extends Frame implements ActionListener/* * param args */NumberButton numberButton;OperationButton operationButton;Button 小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮;Panel panel;JTextField resultShow;String 运算符号=+,-,*,/;LinkedList 链表;boolean 是否按下等号=false;public Cacultor()super(计算器);链表=new LinkedList();numberButton=new NumberButton10;for(int i=0;i=9;i+)numberButtoni=new NumberButton(i);numberButtoni.addActionListener(this);operationButton=new OperationButton4;for(int i=0;i=1)num=num.substring(0,num.length()-1);链表.set(0, num);resultShow.setText(num);else链表.removeLast();resultShow.setText(0);else if(链表.size()=3)String num=(String)链表.getLast();if(num.length()=1)num=num.substring(0,num.length()-1);链表.set(2, num);resultShow.setText(num);else链表.removeLast();resultShow.setText(0);else if(e.getSource()=正负号按钮)if(链表.size()=1)String number1=(String)链表.getFirst();trydouble d=Double.parseDouble(number1);d=-1*d;String str=String.valueOf(d);链表.set(0, str);resultShow.setText(str);catch(Exception ee)else if(链表.size()=3)String number2=(String)链表.getLast();trydouble d=Double.parseDouble(number2);d=-1*d;String str=String.valueOf(d);链表.set(2, str);resultShow.setText(str);catch(Exception ee)else if(e.getSource()=求倒数按钮)if(链表.size()=1|链表.size()=2)String number1=(String)链表.getFirst();trydouble d=Double.parseDouble(number1);d=1.0/d;String str=String.valueOf(d);链表.set(0, str);resultShow.setText(str);catch(Exception ee)else if(链表.size()=3)String number2=(String)链表.getLast();trydouble d=Double.parseDouble(number2);d=1.0/d;String str=String.valueOf(d);链表.set(0, str);resultShow.setText(str);catch(Exception ee)else if(e.getSource()=清零按钮)是否按下等号=false;resultShow.setText(0);链表.clear();public static void main(String args) / TODO Auto-generated method stubnew Cacultor();
展开阅读全文