JAVA编程从一个文件中读取公式,计算每一个公式的结果并输出。
例如:要计算的表达式为:2+2+(8-2)/3
测试代码时,总是提示错误。
- Java code
Exception in thread "main" java.util.NoSuchElementException at java.util.LinkedList.remove(LinkedList.java:788) at java.util.LinkedList.removeFirst(LinkedList.java:134) at java.util.LinkedList.pop(LinkedList.java:601) at Temp.OPText.main(OPText.java:64)
以下为测试源代码,请教大家:
- Java code
//主类 import java.util.Deque;import java.util.LinkedList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;public class OPText { public static void main(String[] args) { String expression = "2+2+(8-2)/3";// 要计算的表达式 List list = new LinkedList(); // 正则式 Pattern entryOfExpression = Pattern.compile("[0-9]+(\\.[0-9]+)? |\\( |\\) |\\+ |- |\\* |/"); Deque stack = new LinkedList();// 栈 Matcher m = entryOfExpression.matcher(expression); while (m.find()) { // 提取语素 String nodeString = expression.substring(m.start(), m.end()); if (nodeString.matches("[0-9].*")) { list.add(Double.valueOf(nodeString));// 如果是数字直接送入列表 } else { OPNode opn = new OPNode(nodeString);// 如果是运算符 int peekLevel = (stack.peek() == null) ? 0 : ((OPNode)stack.peek()).level; if (opn.level >= peekLevel) { stack.push(opn);// 新的运算符比旧的优先级别高则入栈 } else { if (opn.level == -1) { OPNode temp = (OPNode) stack.pop(); while (temp.level != -3) {// 如果为"("则一直出栈一直到")" list.add(temp); System.out.println(nodeString); temp = (OPNode)stack.pop(); } } else if (opn.level == -3) { stack.push(opn); } else {// 如果新运算符比栈顶运算符底则一直出栈 OPNode temp = (OPNode) stack.pop(); while (temp.level > opn.level) { list.add(temp); if (stack.isEmpty()) { break; } temp = (OPNode)stack.pop(); } stack.push(opn); } } } } OPNode temp = null; while (!stack.isEmpty()) { temp = (OPNode)stack.pop(); list.add(temp); }// 后续表达式计算 stack.clear(); for (Object o : list) { if (o instanceof Double) { stack.push(o);// 为数字入栈 } else { double op2 = ((Double)stack.pop()).doubleValue(); double op1 = ((Double)stack.pop()).doubleValue(); switch (((OPNode) o).op) { case '+': stack.push(op1 + op2); break; case '-': stack.push(op1 - op2); break; case '*': stack.push(op1 * op2); break; case '/': stack.push(op1 / op2); break; } } } System.out.println("结果为:" + stack.pop()); }}
- Java code
public class OPNode { char op;// 运算符号 int level;// 优先级 //设置优先级 public OPNode(String op) { this.op = op.charAt(0); if (op.equals("+") || op.equals("-")) { this.level = 1; }else if (op.equals("*") || op.equals("/")) { this.level = 2; }else if (op.equals("(")) { this.level = -3; }else { this.level = -1; } } }