简易四则运算,利用中缀转后缀表达…

package com.caculate;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Stack;
public class Caculate {
    
    public static void main(String[] args) {
        String str = "9+(3-1)*3+3/2";
        LastExpression le = new Lastexpression_r();
        le.toLastexpression_r(str);
        le.pop();//调用栈中剩下的符号元素
        String ss = le.getResult();
System.out.println(ss);
        LastCaculate lc = new LastCaculate();
        lc.caculate(ss);
System.out.println("result:"+lc.getResult());
    }
}
class LastExpression {
    Stack stack = new Stack();
    int level = 0;
    StringBuilder sb = new StringBuilder();
    public void toLastexpression_r(String str) {
        for(int i = 0;i
            if(str.charAt(i)>='0'&&str.charAt(i)<='9') {
//                System.out.print(str.charAt(i));//数字直接输出
                sb.append(str.charAt(i));
            } else {
                operate(str.charAt(i));//处理各个符号
            }
        }
    }
    private void operate(char ch) {
        switch(ch) {
        case '+':
        case '-':
            push(ch,1);break;
        case '*':
        case '/':
            push(ch,2);break;
        case '(':
            push(ch,0);break;
        case ')':
            push(ch,3);break;
        default:break;
        }
    }
    
    private void push(char ch,int priority) {
        if(priority == 0) {
            stack.push(ch);
        } else if(priority==2) {
            stack.push(ch);
        } else if(priority == 3) {
            do {
                
//                System.out.print(stack.pop());
                sb.append(stack.pop());
            } while(stack.pop()!='(');
            priority = 0;
        } else if(priority
            while(!stack.empty()) {
//                System.out.print(stack.pop());
                sb.append(stack.pop());
            }
            stack.push(ch);
        } else {
            stack.push(ch);
        }
        level = priority;//最后更新一下level便于对后面的优先级符号进行比较
    }
    public void pop() {
        while(!stack.empty()) {
//            System.out.print(stack.pop());
            sb.append(stack.pop());
        }
    }
    public String getResult() {
        return sb.toString();
    }
}

class LastCaculate {
    Stack stack = new Stack();
    public void caculate(String str) {
        for(int i = 0;i
            if(str.charAt(i)>='0'&&str.charAt(i)<='9') {
                stack.push(str.charAt(i)-'0');
            } else {
                operate(str.charAt(i));//处理各个符号
            }
        }
    }
    private void operate(char ch) {
        switch(ch) {
        case '+':
            add();break;
        case '-':
            sub();break;
        case '*':
            multiply();break;
        case '/':
            divide();break;
        default:break;
        }
    }
    private void add() {
        double num1 = 0,num2 = 0;
        if(stack.peek()!=null) {
            num1 = Double.parseDouble(""+stack.pop());
        } if(stack.peek()!=null) {
            num2 = Double.parseDouble(""+stack.pop());
        }
        double result = num2 + num1;
        stack.push(result);
    }
    
    private void sub() {
        double num1 = 0,num2 = 0;
        if(stack.peek()!=null) {
            num1 = Double.parseDouble(""+stack.pop());
        } if(stack.peek()!=null) {
            num2 = Double.parseDouble(""+stack.pop());
        }
        double result = num2 - num1;
        stack.push(result);
    }
    private void multiply() {
        double num1 = 0,num2 = 0;
        if(stack.peek()!=null) {
            num1 = Double.parseDouble(""+stack.pop());
        } if(stack.peek()!=null) {
            num2 = Double.parseDouble(""+stack.pop());
        }
        double result = num2 * num1;
        stack.push(result);
    }
    private void divide() {
        double num1 = 0,num2 = 0;
        if(stack.peek()!=null) {
            num1 = Double.parseDouble(""+stack.pop());
        } if(stack.peek()!=null) {
            num2 = Double.parseDouble(""+stack.pop());
        }
        double result = 0;
        if(num1!=0) {
            result = num2 / num1;
            stack.push(result);
        } else {
            System.out.println("除数不能为0!");
            return;
        }
    }
    public double getResult() {
        double num = Double.parseDouble(""+stack.pop());
        BigDecimal bg = new BigDecimal(num);
        double result = bg.setScale(1, 4).doubleValue();//保留1位小数,四舍五入
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值