求大神,用JAVA编计算器,可以优先级混合运算 求源代码!!想回家好好过年 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 List<Object> toSuffix(String s) { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("+", 0); map.put("-", 0); map.put("*", 1); map.put("/", 1); map.put("(", -1); s = s.trim(); List<Object> list = new ArrayList<Object>(); String numberRegex = "\\d+"; String operatorRegex = "\\+|\\-|\\*|/|(|)"; String regex = numberRegex + "|[" + operatorRegex + "]"; Matcher matcher = Pattern.compile(regex).matcher(s); Stack<String> stack = new Stack<String>(); while (matcher.find()) { String t = matcher.group(); if (t.equals("(")) { stack.push(t); } else if (t.equals(")")) { while (!stack.peek().equals("(")) { list.add(stack.pop()); } stack.pop(); } else if (map.containsKey(t)) { while (!stack.isEmpty() && map.get(t) <= map.get(stack.peek())) { list.add(stack.pop()); } stack.push(t); } else { list.add(Double.parseDouble(t)); } } while (!stack.isEmpty()) { list.add(stack.pop()); } return list; } double calculate(List<Object> list) { Stack<Double> stack = new Stack<Double>(); for (Object obj : list) { if (obj instanceof Double) { stack.push((Double) obj); } else { double b = stack.pop(); double a = stack.pop(); if (obj.equals("+")) stack.push(a + b); if (obj.equals("-")) stack.push(a - b); if (obj.equals("*")) stack.push(a * b); if (obj.equals("/")) stack.push(a / b); } } return stack.pop(); } java打包成jar可执行文件 安装properties插件时,新建一个link文件,却不可用是怎么回事? 应该怎么样学习JAVA? 为何阻塞了? java内存管理 求教!!! swing中的一个不大理解的问题 如何能够得到一个Button再一个Panel中的位置 学习java的走向 请教:jvm物理表现形式是不是jre? 求大神,用JAVA绘制正弦函数波形,向x轴右侧不断移动 转载数据库驱动器 sun.jdbc.odbc.JdbcOdbcDriver 失败
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("+", 0);
map.put("-", 0);
map.put("*", 1);
map.put("/", 1);
map.put("(", -1);
s = s.trim();
List<Object> list = new ArrayList<Object>();
String numberRegex = "\\d+";
String operatorRegex = "\\+|\\-|\\*|/|(|)";
String regex = numberRegex + "|[" + operatorRegex + "]";
Matcher matcher = Pattern.compile(regex).matcher(s);
Stack<String> stack = new Stack<String>();
while (matcher.find()) {
String t = matcher.group();
if (t.equals("(")) {
stack.push(t);
} else if (t.equals(")")) {
while (!stack.peek().equals("(")) {
list.add(stack.pop());
}
stack.pop();
} else if (map.containsKey(t)) {
while (!stack.isEmpty() && map.get(t) <= map.get(stack.peek())) {
list.add(stack.pop());
}
stack.push(t);
} else {
list.add(Double.parseDouble(t));
}
}
while (!stack.isEmpty()) {
list.add(stack.pop());
}
return list;
}
double calculate(List<Object> list) {
Stack<Double> stack = new Stack<Double>();
for (Object obj : list) {
if (obj instanceof Double) {
stack.push((Double) obj);
} else {
double b = stack.pop();
double a = stack.pop();
if (obj.equals("+"))
stack.push(a + b);
if (obj.equals("-"))
stack.push(a - b);
if (obj.equals("*"))
stack.push(a * b);
if (obj.equals("/"))
stack.push(a / b);
}
}
return stack.pop();
}