我的結對夥伴是:陳思明 http://www.cnblogs.com/chensiming/
package size; import java.util.Stack; public class opt { public int math(String str) { String[] operater =new String[20]; String[] number = new String[20]; Stack countStack1 = new Stack(); Stack countStack2 = new Stack(); int result =0; number = str.split("\\/|\\*|\\+|\\-"); operater= str.split("\\d+"); for(int i = 0; i<number.length;i++) { countStack1.push(number[i]); if(i!=number.length-1) { countStack1.push(operater[i+1]); } } while(!countStack1.isEmpty())countStack2.push(countStack1.pop()); String op; while(!countStack2.isEmpty()) { result=0; op = countStack2.pop().toString(); if(op.equals("*")) { result=Integer.parseInt(countStack1.pop().toString())*Integer.parseInt(countStack2.pop().toString()); countStack1.push(result); continue; } if(op.equals("/")) { result=Integer.parseInt(countStack1.pop().toString())/Integer.parseInt(countStack2.pop().toString()); countStack1.push(result); continue; } countStack1.push(op); } while(!countStack1.isEmpty())countStack2.push(countStack1.pop()); while(!countStack2.isEmpty()) { result=0; op = countStack2.pop().toString(); if(op.equals("+")) { result=Integer.parseInt(countStack1.pop().toString())+Integer.parseInt(countStack2.pop().toString()); countStack1.push(result); continue; } if(op.equals("-")) { result=Integer.parseInt(countStack1.pop().toString())-Integer.parseInt(countStack2.pop().toString()); countStack1.push(result); continue; } countStack1.push(op); } return result; } }
上一次我已經將計算的模組獨立出來了,這次將他丟到另一個CLASS裡封裝了起來,方便測試以及其他用途