package com.ruoyi.project.business.dto.JEPCustomFunction;

import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.PostfixMathCommand;

import java.util.Stack;

public class MinFunction extends PostfixMathCommand {
    public MinFunction() {
        super();
        // 使用参数的数量
        numberOfParameters = -1;
    }

    @Override
    public void run(Stack inStack) throws ParseException {
        //检查栈
        checkStack(inStack);
        if (this.curNumberOfParameters < 1) {
            throw new ParseException("No arguments for Sum");
        } else {
            Object max = inStack.pop();
            for (int i = 0; i < this.curNumberOfParameters - 1; i++) {
                Object maxCompareParam = inStack.pop();
                max = Math.min((Double) max, (Double) maxCompareParam);
            }
            inStack.push(max);

        }

    }
}
