calculateinfix.java
来自「a calculator with some methods of java.m」· Java 代码 · 共 45 行
JAVA
45 行
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();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?