package com.kingdz.algorithm.time201702;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;/** * 算数表达式求值<br/> * 求解思路:按照计算表达式的思考模式,从左到右一次计算,判断每个运算符前后两个临近运算符的优先级来判断计算的方式<br/> * 缺陷:目前在处理中括号和大括号时会有困难 * * @author kingdz * */public class Algo08 { // 优先级map public static final Map<String, Integer> PRiorMap; static { priorMap = new HashMap<String, Integer>(); priorMap.put("+", 0); priorMap.put("-", 0); // priorMap.put("*", 1); priorMap.put("/", 1); // priorMap.put("(", 2); priorMap.put(")", 2); // priorMap.put("[", 3); priorMap.put("]", 3); // priorMap.put("{", 4); priorMap.put("}", 4); } public static void main(String[] args) { String question = "1+2*(3+4)*5-6"; // 将字符串转化为list List<String> list = Algo07.dismantle(question); System.out.print(" "); // 首先打印需要计算的结果 printList(list, false); int result = cal(list); // 输出最终计算的结果 System.out.println(result); } /** * 打印list,类似于算术 * * @param list * @param isPrintEquals * 是否在前面打印“=”符号 */ private static void printList(List<String> list, boolean isPrintEquals) { StringBuilder strb = new StringBuilder(); for (String str : list) { strb.append(str); } if (isPrintEquals) { System.out.println("=" + strb); } else { System.out.println(strb); } } /** * 打印list,类似于算术 * * @param list */ private static void printList(List<String> list) { printList(list, true); } /** * 计算list表达式 * * @param list * @return */ private static int cal(List<String> list) { // 将所有运算符存入set,方便判断是否是运算符 Set<String> set = new HashSet<String>(); for (String str : Algo07.separator) { set.add(str); } // 用于标记当前处理的位置 int index = 0; // 当list的大小为1时表示计算完成 while (list.size() > 1) { if (index >= list.size()) { // 如果已经处理到结尾,但是依然没有计算出结果则重新计算 index = 0; } // 获得当前处理的字符串 String now = list.get(index); { String before2 = index - 2 >= 0 ? list.get(index - 2) : null; String before1 = index - 1 >= 0 ? list.get(index - 1) : null; String after1 = index + 1 < list.size() ? list.get(index + 1) : null; String after2 = index + 2 < list.size() ? list.get(index + 2) : null; if (before1 != null && after1 != null) { { // 用于去括号 boolean accord = false; if (before1.equals("(") && after1.equals(")")) { accord = true; } else if (before1.equals("[") && after1.equals("]")) { accord = true; } else if (before1.equals("{") && after1.equals("}")) { accord = true; } if (accord) { list.remove(index + 1); list.remove(index - 1); // index--; index = 0; // System.out.println(list); printList(list); continue; } } // 是运算符 if (set.contains(now)) { // 计算前后的优先级 int beforePrior2 = getPriority(before2); // int beforePrior1 = getPriority(before1); int nowPrior = getPriority(now); // int afterPrior1 = getPriority(after1); int afterPrior2 = getPriority(after2); if (nowPrior == 1 || (nowPrior == 0 && beforePrior2 != 1 && afterPrior2 != 1)) { int beforeInt; int afterInt; try { beforeInt = Integer.parseInt(list.get(index - 1)); afterInt = Integer.parseInt(list.get(index + 1)); } catch (NumberFormatException e) { index++; continue; } // 实际进行计算处理 boolean accord = false; if ("+".equals(now)) { list.set(index, "" + (beforeInt + afterInt)); accord = true; } else if ("-".equals(now)) { list.set(index, "" + (beforeInt - afterInt)); accord = true; } else if ("*".equals(now)) { list.set(index, "" + (beforeInt * afterInt)); accord = true; } else if ("/".equals(now)) { list.set(index, "" + (beforeInt / afterInt)); accord = true; } if (accord) { list.remove(index + 1); list.remove(index - 1); // index--; index = 0; // System.out.println(list); printList(list); continue; } } } } } index++; } return Integer.parseInt(list.get(0)); } /** * 根据运算符判断优先级数字 * * @param str * @return 如果是空则返回-1,如果没有则返回-2 * */ private static int getPriority(String str) { if (str == null) { return -1; } if (priorMap.containsKey(str)) { return priorMap.get(str); } else { return -2; } }}
新闻热点
疑难解答