?? calculateinfix.java
字號(hào):
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
public class CalculateInfix {
private static double DynamicMethod(String f,double op) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Method method = (Math.class.getMethod(f, Double.TYPE));
return (Double)method.invoke(null, op);
}
public static double calculate(String s) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Stack<Double> operandStack = new Stack<Double>();
double interans = 0.0;
for(String st: s.trim().split("\\s+")){
if (Comparator.isOperand(st) )
operandStack.push(Double.parseDouble(st));
else {
double b= operandStack.pop();
switch(st.charAt(0)){
case '+': interans = operandStack.pop()+b; break;
case '-': interans = operandStack.pop()-b; break;
case '*': interans = operandStack.pop()*b; break;
case '/': interans = operandStack.pop()/b; break;
default: interans = DynamicMethod(st,b);
}
operandStack.push(interans);
}
};
return operandStack.pop();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -