1 10 package org.mmbase.util; 11 12 import java.util.*; 13 import org.mmbase.util.logging.*; 14 15 16 30 public class ExprCalc { 31 private static final int MC_SYMB=1; 32 private static final int MC_NUM =2; 33 private static final int MC_NONE=0; 34 private static final int MC_EOT =-1; 35 36 private static final Logger log = Logging.getLoggerInstance(ExprCalc.class); 37 38 42 private StringTokenizer tokenizer; 43 private String input; 44 45 private int mCode; 46 private char mSymb; 47 private double mNum; 48 49 private double result; 50 51 55 public ExprCalc(String input) { 56 this.input = input; 57 tokenizer = new StringTokenizer(input, "+-*/()% \t", true); 58 mCode = MC_NONE; 59 result = expr(); 60 if (mCode != MC_EOT) { 61 log.error("Could not evaluate expression: '" + input + "'"); 62 } 63 } 64 65 68 public double getResult() { 69 return result; 70 } 71 72 75 private boolean lex() { 76 String token; 77 if (mCode==MC_NONE) { 78 mCode=MC_EOT;mSymb='\0';mNum=0.0; 79 try { 80 do { 81 token = tokenizer.nextToken(); 82 } while (token.equals(" ")||token.equals("\t")); 83 } catch(NoSuchElementException e) { 84 return false; 85 } 86 if (Character.isDigit(token.charAt(0))) { 88 int i; 89 for(i=0;i<token.length() && 90 (Character.isDigit(token.charAt(i)) || 91 token.charAt(i)=='.');i++) { }; 92 if (i!=token.length()) { 93 log.error("Could not evaluate expression '" + token + "' of '" + input + "'"); 94 } 95 try { 96 mNum=(Double.valueOf(token)).doubleValue(); 97 } catch (NumberFormatException e) { 98 log.error("Could not evaluate expression ('" + token + "' not a number) of '" + input + "'"); 99 } 100 mCode=MC_NUM; 101 } else { mSymb=token.charAt(0); 103 mCode=MC_SYMB; 104 } 105 } 106 return true; 107 } 108 109 113 private double expr() { 114 double tmp = term(); 115 while (lex() && mCode == MC_SYMB && (mSymb == '+' || mSymb == '-')) { 116 mCode=MC_NONE; 117 if (mSymb=='+') { 118 tmp += term(); 119 } else { 120 tmp -= term(); 121 } 122 } 123 if (mCode==MC_SYMB && mSymb=='(' 124 || mCode==MC_SYMB && mSymb==')' 125 || mCode==MC_EOT) { 126 127 } else { 128 log.error("expr: Could not evaluate expression '" + input + "'"); 129 } 130 return tmp; 131 } 132 133 137 private double term() { 138 double tmp=fac(); 139 while (lex() && mCode==MC_SYMB && (mSymb=='*' || mSymb=='/' || mSymb=='%')) { 140 mCode=MC_NONE; 141 if (mSymb=='*') { 142 tmp *= fac(); 143 } else if (mSymb=='/') { 144 tmp /= fac(); 145 } else { 146 tmp %= fac(); 147 } 148 } 149 return tmp; 150 } 151 152 156 private double fac() { 157 double tmp =- 1; 158 boolean minus=false; 159 160 if(lex()&& mCode==MC_SYMB && mSymb=='-') { 161 mCode = MC_NONE; 162 minus = true; 163 } 164 if(lex() && mCode==MC_SYMB && mSymb=='(') { 165 mCode = MC_NONE; 166 tmp = expr(); 167 if(lex() && mCode!=MC_SYMB || mSymb!=')') { 168 log.error("fac1: Could not evaluate expression '" + input + "'"); 169 } 170 mCode=MC_NONE; 171 } else if (mCode==MC_NUM) { 172 mCode=MC_NONE; 173 tmp=mNum; 174 } else { 175 log.error("fac2: Could not evaluate expression '" + input + "'"); 176 } 177 if (minus) tmp = -tmp; 178 return tmp; 179 } 180 } 181 | Popular Tags |