JAVA 计算器基本实现

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Stack;

public class calc {
    public static void main(String[] args) {
        new myframe();
    }
}

class myframe extends Frame {
    Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, add, min, mul, div, equal, clear, delete, dot, bfh, ce;
    TextField tx1, tx2, tx3;

    public myframe() {
        //组件
        bfh = new Button("%");
        ce = new Button("ce");
        clear = new Button("c");
        delete = new Button("<-");
        btn1 = new Button("1");
        btn2 = new Button("2");
        btn3 = new Button("3");
        div = new Button("/");
        btn4 = new Button("4");
        btn5 = new Button("5");
        btn6 = new Button("6");
        mul = new Button("*");
        btn7 = new Button("7");
        btn8 = new Button("8");
        btn9 = new Button("9");
        min = new Button("-");
        dot = new Button(".");
        btn0 = new Button("0");
        equal = new Button("=");
        add = new Button("+");

        tx1 = new TextField("");
        tx2 = new TextField("");
        tx3 = new TextField("");

        Panel pannel1 = new Panel();
        Panel pannel2 = new Panel();
        Panel pannel3 = new Panel();
        Panel pannel4 = new Panel();

        pannel1.setLayout(new BorderLayout());
        pannel1.add(tx1);
        pannel2.setLayout(new BorderLayout());
        pannel2.add(tx2);
        pannel3.setLayout(new BorderLayout());
        pannel3.add(tx3);
        pannel4.setLayout((new GridLayout(5, 4)));
        pannel4.add(bfh);
        pannel4.add(ce);
        pannel4.add(clear);
        pannel4.add(delete);
        pannel4.add(btn1);
        pannel4.add(btn2);
        pannel4.add(btn3);
        pannel4.add(div);
        pannel4.add(btn4);
        pannel4.add(btn5);
        pannel4.add(btn6);
        pannel4.add(mul);
        pannel4.add(btn7);
        pannel4.add(btn8);
        pannel4.add(btn9);
        pannel4.add(min);
        pannel4.add(dot);
        pannel4.add(btn0);
        pannel4.add(equal);
        pannel4.add(add);

        add(pannel1);
        add(pannel2);
        add(pannel3);
        add(pannel4);
        setLayout(new GridLayout(4, 1));
        setVisible(true);
        pack();
        setBounds(100, 100, 100, 100);

        //监听
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        MyActionListen myActionListen = new MyActionListen();
        bfh.addActionListener(myActionListen);
        ce.addActionListener(myActionListen);
        dot.addActionListener(myActionListen);
        add.addActionListener(myActionListen);
        min.addActionListener(myActionListen);
        mul.addActionListener(myActionListen);
        div.addActionListener(myActionListen);
        clear.addActionListener(myActionListen);
        delete.addActionListener(myActionListen);
        equal.addActionListener(myActionListen);
        btn0.addActionListener(myActionListen);
        btn1.addActionListener(myActionListen);
        btn2.addActionListener(myActionListen);
        btn3.addActionListener(myActionListen);
        btn4.addActionListener(myActionListen);
        btn5.addActionListener(myActionListen);
        btn6.addActionListener(myActionListen);
        btn7.addActionListener(myActionListen);
        btn8.addActionListener(myActionListen);
        btn9.addActionListener(myActionListen);
    }

    private class MyActionListen implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String text = tx1.getText();
            if (e.getSource() == btn0) {
                text += btn0.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == btn1) {
                text += btn1.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == btn2) {
                text += btn2.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == btn3) {
                text += btn3.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == btn4) {
                text += btn4.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == btn5) {
                text += btn5.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == btn6) {
                text += btn6.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == btn7) {
                text += btn7.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == btn8) {
                text += btn8.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == btn9) {
                text += btn9.getLabel();
                tx1.setText(text);
            }

            if (e.getSource() == add) {
                text += add.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == min) {
                text += min.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == mul) {
                text += mul.getLabel();
                tx1.setText(text);
            }
            if (e.getSource() == div) {
                text += div.getLabel();
                tx1.setText(text);
            }
            //计算表达式的结果
            if (e.getSource() == equal) {
                if (tx1.getText() != null) {
                    text += equal.getLabel();
                    tx1.setText(text);
                    double calculate = calculate(tx1);
                    text += calculate;
                    tx2.setText(text);
                }

            }
            //清零按钮
            if (e.getSource() == clear) {
                tx1.setText("");
            }
            //删除按钮
            if (e.getSource() == delete) {
                String s = tx1.getText();
                if (s.length() > 0) {
                    tx1.setText(s.substring(0, s.length() - 1));
                }
            }
        }

        private double calculate(TextField tx) {
            //定义两个栈 用来计算数据
            Stack<Double> numStack = new Stack<>();
            Stack<Character> charStack = new Stack<>();
            //进行计算的标志
            boolean flag = false;

            //获取文本框的内容
            String str = tx1.getText();
            int begin = 0;
            int end = 0;
            //遍历s
            for (int i = 0; i < str.length(); i++) {
                char ch = str.charAt(i);

                if (!Character.isDigit(ch)) {
                    //如果不是数字 ,首先判断字符栈是否为空 将end置为当前位置
                    end = i;
                    //将begin 和 end之间的的数字入数字栈
                    numStack.push(Double.parseDouble(str.substring(begin, end)));
                    begin = end + 1;

                    if (flag == true) {
                        double second = numStack.pop();
                        double first = numStack.pop();
                        char opt = charStack.pop();
                        double result = operation(first, second, opt);
                        //将计算出的结果进行入栈
                        numStack.push(result);
                    }

                    if (!charStack.empty()) {
                        //非空 判断优先级
                        if (level(ch, charStack.get(charStack.size() - 1))) {
                            //优先级高 将flag设置为true 下一步放入数字的时候进行运算
                            flag = true;
                        }
                    }
                    //进行入栈
                    if (str.charAt(i) != '=') {
                        charStack.push(ch);
                    }
                }

            }


            //全部入栈完成 进行计算
            while (!numStack.empty() && !charStack.empty()) {
                //从底往上计算
                double first = numStack.remove(0);
                double second = numStack.remove(0);
                char opt = charStack.remove(0);
                double result = operation(first, second, opt);
                //运算完进行入栈
                numStack.add(0, result);
            }
            return numStack.pop();
        }


        /**
         * 进行计算
         *
         * @param first
         * @param second
         * @param opt
         * @return
         */
        private double operation(double first, double second, char opt) {
            switch (opt) {
                case '+':
                    return first + second;
                case '-':
                    return first - second;
                case '*':
                    return first * second;
                case '/':
                    return first / second;
            }
            return -1;
        }

        /**
         * 判断ch 和 topCh的优先级
         *
         * @param ch
         * @param topCh
         * @return
         */
        private boolean level(char ch, Character topCh) {
            switch (ch) {
                case '+':
                    return false;
                case '-':
                    return false;
                case '*':
                    if (topCh == '+' || topCh == '-') {
                        return true;
                    }
                case '/':
                    if (topCh == '+' || topCh == '-') {
                        return true;
                    }
            }
            return false;
        }
    }
}




运行:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值