中缀表达式转换为后缀表达式及逆波兰式计算


import java.util.LinkedList; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 1.中缀表达式转换为后缀表达式 * 2.逆波兰式计算 * * @author Administrator * */ public class Calculater { public static void main(String[] args) { // String str = "5+((1+2)*4)-13"; String str = "12/4.1+(1+4*5)"; //处理结果: 5 1 2 + 4 * + 3 − //result = 14.0 Calculater cal = new Calculater(); String result = cal.calculate(str); System.out.println(result); } /** * 逆波兰式计算 * * @param expr * @return */ public String calculate(String expr) { LinkedList<String> rpn = RPExpression(expr); LinkedList<String> ops = new LinkedList<String>(); double result = 0; for (int i = 0; i < rpn.size(); i++) { String symbol = rpn.get(i); if (isDigit(symbol)) { ops.addLast(symbol); } if (isOperaton(symbol)) { double tail = Double.valueOf(ops.getLast()); ops.removeLast(); double head = Double.valueOf(ops.getLast()); ops.removeLast(); if (symbol.equals("+")) { result = head + tail; } else if (symbol.equals("-")) { result = head - tail; } else if (symbol.equals("*")) { result = head * tail; } else if (symbol.equals("/")) { if (tail == 0) { result = 0; } else { result = head / tail; } } ops.addLast(String.valueOf(result)); } } return ops.getLast().toString(); } /** * 中缀表达式转换为后缀表达式 * * @param expr * @return */ public LinkedList<String> RPExpression(String expr) { LinkedList<String> rpnStack = new LinkedList<String>(); LinkedList<String> opsStack = new LinkedList<String>(); opsStack.addLast("#"); char[] echars = expr.toCharArray(); int index = 0; for (;index < echars.length; index ++) { char sign = echars[index]; if (isDigit(sign)) { StringBuffer num = new StringBuffer(); while (index < echars.length && isDigit(sign = echars[index])) { num.append(sign); index ++; } rpnStack.addLast(num.toString()); } if (isOperaton(sign)) { char top = opsStack.getLast().toCharArray()[0]; if (top == '#' || top == '(') { opsStack.addLast(String.valueOf(sign)); } else { if (sign == ')') { while (top != '(') { rpnStack.addLast(opsStack.getLast()); opsStack.removeLast(); top = opsStack.getLast().toCharArray()[0]; } opsStack.removeLast(); } else if (sign == '-' || sign == '+') { while (top == '/' || top == '*') { rpnStack.addLast(opsStack.getLast()); opsStack.removeLast(); top = opsStack.getLast().toCharArray()[0]; } opsStack.addLast(String.valueOf(sign)); } else { opsStack.addLast(String.valueOf(sign)); } } } } while (!opsStack.isEmpty() && !opsStack.getLast().equals("#")) { String top = opsStack.getLast(); rpnStack.addLast(top); opsStack.removeLast(); } System.out.println(rpnStack.toString()); return rpnStack; } private boolean isDigit(char di) { if (di > '0' && di < '9' || di == '.') { return true; } return false; } private boolean isDigit(String di) { Pattern digitPattern = Pattern.compile("\\d*(\\.?\\d+)?"); Matcher digitPatcher = digitPattern.matcher(di); return digitPatcher.matches(); } private boolean isOperaton(char op) { return isOperaton(String.valueOf(op)); } private boolean isOperaton(String op) { // 主键盘与数字键盘符号ASNII不一致 Pattern opPattern = Pattern.compile("[+|-|/|*|(|)|-]"); Matcher opMatcher = opPattern.matcher(op); return opMatcher.matches(); } }
posted on 2012-10-30 14:36 dexterman 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/dexterman/archive/2012/10/30/2746294.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值