回 帖 发 新 帖 刷新版面

主题:计算器的几种实现

/////////////////////////文件夹3


/**
 * This class implements a calculator throught a GUI.
 *
 * @author Tang chaoyong    200532580087
 * @version 1.1
 * @date 3,16,2007
 */
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class Calculator extends JPanel{
    private JButton button1,button2,button3,button4,button5,button6,button7,button8,button9,button0,buttonPlus,
            buttonMinus,buttonMuti,buttonDiv,buttonPoint,buttonLeftB,buttonRightB,buttonResult,buttonClear,
            buttonBack;
    private JTextField inputField;
    private JTextArea errorArea;
    private JTextField expressionArea;
    private String result;
    private String expression;
    private Stack<Double> sOperand;
    private Stack<Character> sOperator;
    
    public Calculator(){
        result = new String();
        expression = new String();
        sOperand = new Stack<Double>();
        sOperator = new Stack<Character>();
        
        button1 = new JButton("1");
        button2 = new JButton("2");
        button3 = new JButton("3");
        button4 = new JButton("4");
        button5 = new JButton("5");
        button6 = new JButton("6");
        button7 = new JButton("7");
        button8 = new JButton("8");
        button9 = new JButton("9");
        button0 = new JButton("0");
        buttonPlus = new JButton("+");
        buttonMinus = new JButton("-");
        buttonMuti = new JButton("*");
        buttonDiv = new JButton("/");
        buttonPoint = new JButton(".");
        buttonLeftB = new JButton("(");
        buttonRightB = new JButton(")");
        buttonResult = new JButton("=");
        buttonClear = new JButton("C");
        buttonBack = new JButton("B");
        
        inputField = new JTextField(6);
        inputField.setHorizontalAlignment(JTextField.RIGHT);
        inputField.setText("");
        inputField.setEditable(false);
        
        expressionArea = new JTextField(20);
        expressionArea.setHorizontalAlignment(JTextField.RIGHT);
        expressionArea.setText("");
        expressionArea.setEditable(false);
        
        errorArea = new JTextArea();
        errorArea.setEditable(false);
        errorArea.setBackground(Color.GREEN);
        
        JPanel panel1 = new JPanel();
        panel1.setBorder(BorderFactory.createTitledBorder("Result"));
        panel1.add(expressionArea);panel1.add(inputField);
        
        JPanel panel2 = new JPanel();
        panel2.setBorder(BorderFactory.createTitledBorder("Number"));
        panel2.setLayout(new GridLayout(4, 3, 6, 6));
        panel2.add(button1);panel2.add(button2);panel2.add(button3);
        panel2.add(button4);panel2.add(button5);panel2.add(button6);
        panel2.add(button7);panel2.add(button8);panel2.add(button9);
        panel2.add(button0);
        
        JPanel panel3 =new JPanel();
        panel3.setBorder(BorderFactory.createTitledBorder("Operator"));
        panel3.setLayout(new GridLayout(4,3,6,6));
        panel3.add(buttonPlus);panel3.add(buttonMinus);panel3.add(buttonMuti);
        panel3.add(buttonDiv);panel3.add(buttonLeftB);panel3.add(buttonRightB);
        panel3.add(buttonClear);panel3.add(buttonBack);panel3.add(buttonResult);
        panel3.add(buttonPoint);
        
        JPanel panel4 = new JPanel();
        panel4.setBorder(BorderFactory.createTitledBorder("Mistakes"));
        panel4.add(errorArea);
        
        button1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                result = inputField.getText();
                result += 1;expression += 1;
                inputField.setText(result);expressionArea.setText(expression);
            }
        });
        button2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e2){
                result = inputField.getText();
                result += 2;expression += 2;
                inputField.setText(result);expressionArea.setText(expression);
            }
        });
        button3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e3){
                result = inputField.getText();
                result += 3;expression += 3;
                inputField.setText(result);expressionArea.setText(expression);
            }
        });
        button4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e4){
                result = inputField.getText();
                result += 4;expression += 4;
                inputField.setText(result);expressionArea.setText(expression);
            }
        });
        button5.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e5){
                result = inputField.getText();
                result += 5;expression += 5;
                inputField.setText(result);expressionArea.setText(expression);
            }
        });
        
        button6.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e6){
                result = inputField.getText();
                result += 6;expression += 6;
                inputField.setText(result);expressionArea.setText(expression);
            }
        });
        button7.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                result = inputField.getText();
                result += 7;expression += 7;
                inputField.setText(result);expressionArea.setText(expression);
            }
        });
        button8.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e8){
                result = inputField.getText();
                result += 8;expression += 8;
                inputField.setText(result);expressionArea.setText(expression);
            }
        });
        
        button9.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e9){
                result = inputField.getText();
                result += 9;expression += 9;
                inputField.setText(result);expressionArea.setText(expression);
            }
        });
        button0.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e0){
                result = inputField.getText();
                result += 0;expression += 0;
                inputField.setText(result);expressionArea.setText(expression);
            }
        });
        buttonPlus.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                inputField.setText("");
                expression += " + ";expressionArea.setText(expression);
            }
        });
        buttonMinus.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                inputField.setText("");
                expression += " - ";expressionArea.setText(expression);
            }
        });
        buttonMuti.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                inputField.setText("");
                expression += " * ";expressionArea.setText(expression);
            }
        });
        buttonDiv.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                inputField.setText("");
                expression += " / ";expressionArea.setText(expression);
            }
        });
        buttonPoint.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                result += ".";expression += ".";
                inputField.setText(result);expressionArea.setText(expression);
            }
        });
        buttonLeftB.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                if(!result.equals(""))inputField.setText("");
                expression += " ( ";expressionArea.setText(expression);
            }
        });
        buttonRightB.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                if(!result.equals(""))inputField.setText("");
                expression += " ) ";expressionArea.setText(expression);
            }
        });
        buttonBack.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                result = inputField.getText();
                if(result.length() == 0){
                    expression = expression.substring(0, expression.length()-1);
                    expressionArea.setText(expression);
                }
                else if(result.length() == 1){
                    result = "";inputField.setText("");
                    expression = expression.substring(0, expression.length()-1);
                    expressionArea.setText(expression);
                }else{
                    result = result.substring(0, result.length()-1);
                    inputField.setText(result);
                    expression = expression.substring(0, expression.length()-1);
                    expressionArea.setText(expression);
                }
                
            }
        });
        buttonClear.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                result = "";expression = "";
                sOperand.clear();sOperator.clear();sOperator.push('$');
                inputField.setText("");expressionArea.setText("");
                errorArea.setText("");
            }
        });
        buttonResult.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e1){
                String temp;sOperator.push(new Character('#'));
                StringTokenizer tokenizer = new StringTokenizer(expression, " ");
                while(tokenizer.hasMoreTokens()){
                    temp = tokenizer.nextToken();
                    if(temp.equals("+")||temp.equals("-")||temp.equals("*")||temp.equals("/")||temp.equals("(")||temp.equals(")")){
                        doOperator(temp.charAt(0));
                    }else{
                        sOperand.push(Double.parseDouble(temp));
                    }
                }
                while((sOperator.peek()).charValue()!='#'){
                    doOperand(sOperator.pop());
                }
                inputField.setText("");
                double d = sOperand.pop();int i = (new Double(d)).intValue();
                if(d == i)temp = (new Integer(i)).toString();
                else temp = (new Double(d)).toString();
                expressionArea.setText(expression+"="+temp);
                expression = temp;
                errorArea.setText("");
            }
        });
        
        this.setLayout(new BorderLayout(6,6));
        this.add(BorderLayout.NORTH, panel1);
        this.add(BorderLayout.WEST, panel2);
        this.add(BorderLayout.CENTER, panel3);
        this.add(BorderLayout.SOUTH, panel4);
    }
    

未完!!!!!!   见下面的回复

回复列表 (共1个回复)

沙发

/////继上面

private void doOperator(Character ch){
        if(ch.equals(')')){
            Character y = sOperator.pop();
            while(!y.equals('(')){
                doOperand(y);
                y = sOperator.pop();
            }
        }else{
            while(icp(ch)<isp(sOperator.peek())){
                doOperand(sOperator.pop());
            }
            sOperator.push(ch);
        }
        
    }
    private void doOperand(Character ch){
        double oper1,oper2;
        if(sOperand.isEmpty())  {
            errorArea.setText("The expression is wrong");return;
        }
        else oper1 = sOperand.pop();
        if(sOperand.isEmpty())  {
            errorArea.setText("The expression is wrong");return;
        }
        else oper2 = sOperand.pop();
        switch(ch){case '+':sOperand.push(oper2+oper1);break;
                   case '-':sOperand.push(oper2-oper1);break;
                   case '*':sOperand.push(oper2*oper1);break;
                   case '/':if(oper1==0.0)errorArea.setText("The dividend is zero");
                            else sOperand.push(oper2/oper1);break;
                            
        }
    }
    private int icp(Character ch){
        switch(ch){case '+':case '-':return 2;
                   case '*':case '/':return 4;
                   case '(':return 7;
                   case '$':return 0;
        }
        return -1;
    }
    private int isp(Character ch){
        switch(ch){case '+':case '-':return 3;
                   case '*':case '/':return 5;
                   case '(':return 1;
                   case '$':return 0;
        }
        return -1;
    }
        
        public static void main(String [] args){
            JFrame frame = new JFrame("Calculator");
            frame.setContentPane(new Calculator());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setResizable(false);
            frame.setVisible(true);
        }
    }


我来回复

您尚未登录,请登录后再回复。点此登录或注册