half.java

来自「这个是一个计算表达式的java包」· Java 代码 · 共 44 行

JAVA
44
字号
import java.util.*;import com.singularsys.jep.*;import com.singularsys.jep.functions.*;/** * An example custom function class for JEP. */class Half extends PostfixMathCommand {	/**	 * Constructor. The half function only accepts 1 parameter so the	 * numberOfParameters value is set accordingly.	 */	public Half() {		numberOfParameters = 1;	}		/**	 * Divides the value of the top on the inStack by two. The parameter is 	 * popped off the <code>inStack</code>, and divided by two. The final value 	 * is pushed back to the top of <code>inStack</code>.	 * <p>	 * This function only accepts Double types.	 */	public void run(Stack<Object> inStack) throws EvaluationException {		// check the stack		checkStack(inStack);		// get the parameter from the stack		Object param = inStack.pop();		// check whether the argument is of the right type		if (param instanceof Double) {			// calculate the result			double r = ((Double)param).doubleValue() / 2;			// push the result on the inStack			inStack.push(new Double(r));		} else {			throw new EvaluationException("Invalid parameter type");		}	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?