公式表达式的java实现(包含加减乘除)

公式表达式的java实现(包含加减乘除)

public class EvaluateExpressFram extends JFrame implements ActionListener {
JButton jb_ok=null;
JLabel jl1=null;
JLabel jl2=null;
JTextField jtf=null;
JTextField jtf1=null;

    public EvaluateExpressFram() throws HeadlessException {
    super("表达式求值窗口");
    jb_ok=new JButton("确定");
    this.jl1=new JLabel("请输入表达式:");
    jl2=new JLabel("运算结果为:");
    jtf=new JTextField(20);
    jtf1=new JTextField(20);
    jtf1.setEditable(false);
    this.setBounds(600, 400,350, 350);
    this.setVisible(true);
    this.setLayout(new FlowLayout());
    this.add(jl1);
    this.add(this.jtf);
    this.add(jl2);
    this.add(this.jtf1);
    this.add(jb_ok);
    this.jb_ok.addActionListener(this);
    this.validate();
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    
}


    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String jtf_text=this.jtf.getText().trim();
        this.jtf1.setText(String.valueOf(EvaluateExpress(jtf_text)));
        
        

    }
    public static int EvaluateExpress(String expression) {
        Stack<Integer> operandStack=new Stack<>();
        Stack<Character> operatorStack=new Stack<>();
        expression = insertBlanks(expression);
        String[] tokens=expression.split(" ");
        for(String token:tokens) {
            if(token.length()==0)
                continue;
            else if(token.charAt(0)=='+'||token.charAt(0)=='-') {
                while(!operatorStack.isEmpty() &&
                        (operatorStack.peek()=='+'||
                        operatorStack.peek()=='-'||
                        operatorStack.peek()=='*'||
                        operatorStack.peek()=='/')) {
                    processAnOperator(operandStack,operatorStack);
                    
                }
                operatorStack.push(token.charAt(0));
            }
            else if(token.charAt(0)=='*'||token.charAt(0)=='/') {
                while(!operatorStack.isEmpty() &&
                        (operatorStack.peek()=='*'||
                        operatorStack.peek()=='/')) {
                    processAnOperator(operandStack,operatorStack);
                    
                }
                operatorStack.push(token.charAt(0));
            }
            else if(token.trim().charAt(0)=='(')
                operatorStack.push('(');
            else if(token.trim().charAt(0)==')') {
                while(operatorStack.peek()!='(') {
                    processAnOperator(operandStack,operatorStack);
                }
                operatorStack.pop();
            }
            else {
                operandStack.push(new Integer(token));
            }
                
            
        }
        while(!operatorStack.isEmpty()) {
            processAnOperator(operandStack,operatorStack);
        }
        
        return operandStack.pop();
    }


    private static void processAnOperator(Stack<Integer> operandStack, Stack<Character> operatorStack) {
        // TODO Auto-generated method stub
        char op=operatorStack.pop();
        int op1=operandStack.pop();
        int op2=operandStack.pop();
        if(op=='+')
            operandStack.push(op2+op1);
        else if(op=='-')
            operandStack.push(op2-op1);
        else if(op=='*')
            operandStack.push(op2*op1);
        else if(op=='/')
            operandStack.push(op2/op1);
    }


    private static String insertBlanks(String s) {
        // TODO Auto-generated method stub
        String result="";
        for(int i=0;i<s.length();i++) {
            if(s.charAt(i)=='('||s.charAt(i)==')'||
                    s.charAt(i)=='+'||s.charAt(i)=='-'
                    ||s.charAt(i)=='*'||s.charAt(i)=='/')
        result+=" "+s.charAt(i)+" ";
        else result+=s.charAt(i);
            }
        System.out.println(result);
        return result;
        
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pjhlth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值