1 8 9 package org.lsmp.djepExamples; 10 import org.nfunk.jep.*; 11 import org.lsmp.djep.rpe.*; 12 15 public class RpExample { 16 static JEP j; 17 18 public static void main(String args[]) { 19 j = new JEP(); 20 j.addStandardConstants(); 21 j.addStandardFunctions(); 22 j.addComplex(); 23 j.setAllowUndeclared(true); 24 j.setImplicitMul(true); 25 j.setAllowAssignment(true); 26 27 29 doStuff("1*2*3+4*5*6+7*8*9"); 30 doAll(new String []{"x1=1","x2=2","x3=3","x4=4","x5=5","x6=6","x7=7","x8=8","x9=9", 31 "x1*x2*x3+x4*x5*x6+x7*x8*x9"}); 32 doAll(new String []{"x=0.7","cos(x)^2+sin(x)^2"}); 33 } 50 51 public static void doStuff(String str) { 52 try { 53 Node node = j.parse(str); 54 55 RpEval rpe = new RpEval(j); 56 RpCommandList list = rpe.compile(node); 57 double res = rpe.evaluate(list); 58 59 System.out.println(str+"\nres " + res); 61 62 System.out.println("Commands"); 64 System.out.println(list.toString()); 65 } 66 catch(ParseException e) { System.out.println("Parse error "+e.getMessage()); } 67 catch(Exception e) { System.out.println("evaluation error "+e.getMessage()); e.printStackTrace(); } 68 } 69 70 public static void doAll(String str[]) { 71 try { 72 RpEval rpe = new RpEval(j); 73 74 for(int i=0;i<str.length;++i) 75 { 76 Node node = j.parse(str[i]); 77 RpCommandList list = rpe.compile(node); 78 double res = rpe.evaluate(list); 79 80 System.out.println(str[i]+"\nres " + res); 82 83 System.out.println("Commands"); 85 System.out.println(list.toString()); 86 } 87 88 } 89 catch(ParseException e) { System.out.println("Parse error "+e.getMessage()); } 90 catch(Exception e) { System.out.println("evaluation error "+e.getMessage()); e.printStackTrace(); } 91 } 92 } 93 | Popular Tags |