阅读 3362 次
发表时间:2010-08-19
感觉自己的方法有点烂,代码冗余太多,很不优雅。希望大家来分享下自己的思路,让我借鉴学习下 ![]() import java.util.*; public class ComputeString { public static void main(String[] args) { System.out.println(computeString("321+432*43/7+1")); } public static Double computeString(String s) { ArrayList<Double> numList = new <Double>ArrayList(); ArrayList<String> operatorList = new <String>ArrayList(); String[] nL = s.split("[+-/*]"); String[] oL = s.split("[\\d]+"); for (String str : nL) { numList.add(Double.parseDouble(str)); } for (String str : oL) { operatorList.add(str); } operatorList.remove(0); for (int i = 0; i < operatorList.size(); i++) { switch (operatorList.get(i).charAt(0)) { case '*': { numList.set(i, numList.get(i) * numList.get(i + 1)); numList.remove(i + 1); operatorList.remove(i); i--; break; } case '/': { numList.set(i, numList.get(i) / numList.get(i + 1)); numList.remove(i + 1); operatorList.remove(i); i--; break; } } } for (int i = 0; i < operatorList.size(); i++) { switch (operatorList.get(i).charAt(0)) { case '+': { numList.set(i, numList.get(i) + numList.get(i + 1)); numList.remove(i + 1); operatorList.remove(i); i--; break; } case '-': { numList.set(i, numList.get(i) - numList.get(i + 1)); numList.remove(i + 1); operatorList.remove(i); i--; break; } } } return numList.get(0); } } |
|
发表时间:2010-10-17
不知道有什么更好的方法可以实现这个?我也来试试
|
|
发表时间:2010-10-17
呵呵,腾讯一面面试官有问这个~~~~~
|
|
发表时间:2010-10-18
经典的二叉树问题
|