1 9 10 package org.nfunk.jep.function; 11 12 import java.util.*; 13 import org.nfunk.jep.*; 14 import org.nfunk.jep.type.*; 15 16 public class Add extends PostfixMathCommand 17 { 18 19 public Add() 20 { 21 numberOfParameters = -1; 22 } 23 24 28 public void run(Stack stack) throws ParseException { 29 checkStack(stack); 31 Object sum = stack.pop(); 32 Object param; 33 int i = 1; 34 35 while (i < curNumberOfParameters) { 37 param = stack.pop(); 39 40 sum = add(param, sum); 42 43 i++; 44 } 45 46 stack.push(sum); 47 48 return; 49 } 50 51 public Object add(Object param1, Object param2) throws ParseException { 52 if (param1 instanceof Complex) 53 { 54 if (param2 instanceof Complex) 55 return add((Complex)param1, (Complex)param2); 56 else if (param2 instanceof Number ) 57 return add((Complex)param1, (Number )param2); 58 } 59 else if (param1 instanceof Number ) 60 { 61 if (param2 instanceof Complex) 62 return add((Complex)param2, (Number )param1); 63 else if (param2 instanceof Number ) 64 return add((Number )param1, (Number )param2); 65 } 66 else if ((param1 instanceof String ) && (param2 instanceof String )) { 67 return (String )param1 + (String )param2; 68 } 69 70 throw new ParseException("Invalid parameter type"); 71 } 72 73 public Double add(Number d1, Number d2) { 74 return new Double (d1.doubleValue() + d2.doubleValue()); 75 } 76 77 public Complex add(Complex c1, Complex c2) { 78 return new Complex(c1.re() + c2.re(), c1.im() + c2.im()); 79 } 80 81 public Complex add(Complex c, Number d) { 82 return new Complex(c.re() + d.doubleValue(), c.im()); 83 } 84 } 85 | Popular Tags |