import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Calculator {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Listhouzhuishi = houzhui("(0.006+(3-1))/(2+1)+3*2");
//Listhouzhuishi = houzhui("(600+(30000-1))/(2+1)+3*2");
//Double a = Calucate.recursiveCalStr("6+(3-1)/(2+1)+3*2");
System.out.println(calcResult(houzhuishi));
}
public static double calcResult(List houzhishi) throws Exception{
Stack charStack = new Stack();
for (String str : houzhishi) {
if (getHuHao().contains(str)) {
Double data1 = charStack.pop();
Double data2 = charStack.pop();
charStack.push(cal(str.charAt(0),data2,data1));
} else {
charStack.push(Double.valueOf(str));
}
}
return charStack.peek();
}
public static List getHuHao() {
List huhao = new ArrayList(0);
huhao.add("+");
huhao.add("-");
huhao.add("*");
huhao.add("/");
huhao.add("(");
huhao.add(")");
huhao.add("#");
return huhao;
}
public static Double cal(char c,double a,double b) throws Exception{
switch(c){
case '+':
return a+b;
case '-':
return a-b;
case '*':
return a*b;
case '/':
return a/b;
default:
throw new Exception("XX");
}
}
public static List houzhui(String val) {
List result = new ArrayList();
Stack charStack = new Stack();
charStack.push("#");
val = val + "#";
int[][] dir = {
{1,1,0,0,0,1,1},
{1,1,0,0,0,1,1},
{1,1,1,1,0,1,1},
{1,1,1,1,0,1,1},
{0,0,0,0,0,2,-1},
{1,1,1,1,-1,1,1},
{0,0,0,0,0,-1,2},
};
List test = new ArrayList();
StringBuilder temp = new StringBuilder();
for(int i = 0 ,length = val.length(); i < length ; i++) {
String chr = String.valueOf(val.charAt(i));
if (getHuHao().contains(chr) ) {
if (temp.length() > 0) {
test.add(temp.toString());
}
test.add(chr);
temp.delete(0, temp.length());
} else {
temp.append(chr);
}
}
for (int i = 0 ,size = test.size(); i < size ; i++) {
String str = test.get(i);
try {
Double.valueOf(str);
result.add(str);
} catch (NumberFormatException e) {
int x = getHuHao().indexOf(str);
int y = getHuHao().indexOf(charStack.peek());
if (dir[y][x] == 0) {
charStack.push(str);
} else if (dir[y][x] == 1) {
result.add(charStack.pop());
i --;
} else if (dir[y][x] == 2 && (x == 5 && y == 4 )) {
charStack.pop();
} else if (dir[y][x] == 2 && (x == 6 && y == 6 )) {
break;
} else {
break;
}
}
}
return result;
}
}
附件中有算法解释。