1 9 10 package org.nfunk.jep.function; 11 12 import java.util.*; 13 import org.nfunk.jep.*; 14 15 20 public class Sum extends PostfixMathCommand 21 { 22 private Add addFun = new Add(); 23 26 public Sum() { 27 numberOfParameters = -1; 29 } 30 31 35 public void run(Stack stack) throws ParseException { 36 37 if (null == stack) { 39 throw new ParseException("Stack argument null"); 40 } 41 42 Object param = stack.pop(); 43 Number result; 44 if (param instanceof Number ) 45 result = (Number ) param; 46 else 47 throw new ParseException("Invalid parameter type"); 48 49 for(int i=1;i < curNumberOfParameters;++i) 51 { 52 param = stack.pop(); 54 if (param instanceof Number ) { 55 result = addFun.add((Number ) param,result); 57 } else { 58 throw new ParseException("Invalid parameter type"); 59 } 60 61 i++; 62 } 63 stack.push(result); 65 } 66 } 67 | Popular Tags |