⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculateinfix.java

📁 a calculator with some methods of java.math
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -