利用栈实现字符串的四则运算

package javatest.stack;


import java.math.BigDecimal;
import java.util.Stack;


public class MyOperate {
private static class Formula{
private String formula;  //计算公式字符串
private int offset;
public Formula(String formula,int offset){
this.formula=formula;
this.offset=offset;
}
}
private static class Operator{
private String operator;
public int primary;
public BigDecimal value;
public Operator(String operator){
this.operator=operator;
if (operator.equals("+")) {
primary = 10;
} else if (operator.equals("-")) {
primary = 10;
} else if (operator.equals("*")) {
primary = 20;
} else if (operator.equals("/")) {
primary = 20;
} else if (operator.equals("&")) {
primary = 0;
} else {
throw new RuntimeException("不支持操作符'" + operator + "'.");
}
}
public BigDecimal exec(BigDecimal value, BigDecimal value1) {
if(operator.equals("+")){
return value.add(value1);
}else if(operator.equals("-")){
return value.subtract(value1);
}else if(operator.equals("*")){
return value.multiply(value1);
}else if(operator.equals("/")){
return value.divide(value1, 5, BigDecimal.ROUND_HALF_UP );
}else{
throw new RuntimeException("不支持的操作符");
}
}
}
public BigDecimal calculate(String str){
Formula formula=new Formula(str.replaceAll("\\s", ""), 0);
BigDecimal value=parseExp(formula);
if(formula.offset<str.length()){
throw new RuntimeException("公式没有计算完全");
}
return value;
}
private BigDecimal parseExp(Formula formula) {
Stack stack=new Stack();
stack.push(new Operator("&"));
BigDecimal value=parseVal(formula);
for(;;){
Operator operator=parseOp(formula);
if (operator == null) {
operator = new Operator("&");
}
Operator stackOperator=(Operator) stack.peek();
while(operator.primary<=stackOperator.primary){
Operator top=(Operator) stack.pop();
if(top.operator.equals("&")){
return value;
}
value=top.exec(top.value,value);
stackOperator=(Operator) stack.peek();
}
Operator newOp=new Operator(operator.operator);
newOp.value=value;
stack.push(newOp);
value=parseVal(formula);
}
}
private Operator parseOp(Formula r) {
if (r.offset >= r.formula.length()) {
return null;
}
if ("+-*/".indexOf(r.formula.substring(r.offset, r.offset + 1)) >= 0) {
String token = r.formula.substring(r.offset, r.offset + 1);
Operator op = new Operator(token);
r.offset++;
return op;
}
return null;
}
private BigDecimal parseVal(Formula r) {
int startSet=r.offset;
while(r.offset<r.formula.length()&&"0123456789".indexOf(r.formula.substring(r.offset,r.offset+1))>=0){
r.offset++;
}
if(r.offset<r.formula.length()&&r.formula.substring(r.offset,r.offset+1).equals(".")){
r.offset++;
while(r.offset<r.formula.length()&&"0123456789".indexOf(r.formula.substring(r.offset,r.offset+1))>=0){
r.offset++;
}
}
if(r.offset>startSet){
return new BigDecimal(r.formula.substring(startSet,r.offset).trim());
}else if(r.offset<r.formula.length()&&r.formula.substring(r.offset,r.offset+1).equals("+")){
r.offset++;
return parseVal(r);
}else if(r.offset<r.formula.length()&&r.formula.substring(r.offset,r.offset+1).equals("-")){
r.offset++;
return parseVal(r).negate();
}else if(r.offset<r.formula.length()&&r.formula.substring(r.offset,r.offset+1).equals("(")){
r.offset++;
BigDecimal value=parseExp(r);
if (r.offset < r.formula.length() && r.formula.substring(r.offset, r.offset + 1).equals(")")) {
r.offset++;
return value;
}
throw new RuntimeException("缺少')'.");
}else{
if(r.offset==r.formula.length()){
throw new RuntimeException("结尾期望为数值");
}else{
throw new RuntimeException("不识别的取值");
}
}
}
public static void main(String[] args) {
MyOperate o=new MyOperate();
System.out.println(o.calculate("-2*(3+4*2)"));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值