Back to index

wims  3.65+svn20090927
Evaluator.java
Go to the documentation of this file.
00001 /*
00002 $Id: Evaluator.java,v 1.3 2003/02/18 11:48:47 sander Exp $
00003 */
00004 
00005 
00006 /*
00007 Copyright (C) 2001-2002 Mainline Project (I3S - ESSI - CNRS -UNSA)
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU Lesser General Public
00011 License as published by the Free Software Foundation; either
00012 version 2.1 of the License, or (at your option) any later version.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public
00020 License along with this library; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023 For further information on the GNU Lesser General Public License,
00024 see: http://www.gnu.org/copyleft/lesser.html
00025 For further information on this library, contact: mainline@essi.fr
00026 */
00027 
00028 
00029 package fr.ove.openmath.jome.model.evaluation;
00030 
00031 public final class Evaluator {
00032     public static String evaluate(String operand1, String operand2, String anOperator) {
00033         String oper1 = removeBrackets(operand1);
00034         String oper2 = removeBrackets(operand2);
00035         
00036         double result;
00037         
00038         if (anOperator.equals("+")) {
00039             if (isNumber(oper1) && isNumber(oper2))
00040                 return cutDecimal((Double.valueOf(oper1)).doubleValue() + (Double.valueOf(oper2)).doubleValue());
00041             else
00042                 // Si la seconde opérande est un -, alors il ne faudra pas insérer le + entre les 2
00043                 anOperator = (oper2.charAt(0) == '-') ? "" : anOperator;
00044         }
00045         else if (anOperator.equals("-")) {
00046             if (isNumber(oper1) && isNumber(oper2))
00047                 return cutDecimal((Double.valueOf(oper1)).doubleValue() - (Double.valueOf(oper2)).doubleValue());
00048         }
00049         else if (anOperator.equals("*")) {
00050             if (isNumber(oper1) && isNumber(oper2))
00051                 return cutDecimal((Double.valueOf(oper1)).doubleValue() * (Double.valueOf(oper2)).doubleValue());
00052         }
00053         else if (anOperator.equals("/")) {
00054             if (isNumber(oper1) && isNumber(oper2))
00055                 return cutDecimal((Double.valueOf(oper1)).doubleValue() / (Double.valueOf(oper2)).doubleValue());
00056         }
00057         else if (anOperator.equals("^")) {
00058             if (isNumber(oper1) && isNumber(oper2))
00059                 return cutDecimal(Math.pow((Double.valueOf(oper1)).doubleValue(), (Double.valueOf(oper2)).doubleValue()));
00060         }
00061             
00062         return operand1 + anOperator + operand2;
00063     }
00064     
00065     public static String evaluate(String function, String argument) {
00066         function = function.toLowerCase();
00067         
00068         argument = removeBrackets(argument);
00069         
00070         if (function.equals("sin")) {
00071             if (isNumber(argument))
00072                 return cutDecimal(Math.sin((Double.valueOf(argument)).doubleValue()));
00073         }
00074         if (function.equals("arcsin")) {
00075             if (isNumber(argument))
00076                 return cutDecimal(Math.asin((Double.valueOf(argument)).doubleValue()));
00077         }
00078         if (function.equals("cos")) {
00079             if (isNumber(argument))
00080                 return cutDecimal(Math.cos((Double.valueOf(argument)).doubleValue()));
00081         }
00082         if (function.equals("arccos")) {
00083             if (isNumber(argument))
00084                 return cutDecimal(Math.acos((Double.valueOf(argument)).doubleValue()));
00085         }
00086         if (function.equals("tan")) {
00087             if (isNumber(argument))
00088                 return cutDecimal(Math.tan((Double.valueOf(argument)).doubleValue()));
00089         }
00090         if (function.equals("arctan")) {
00091             if (isNumber(argument))
00092                 return cutDecimal(Math.atan((Double.valueOf(argument)).doubleValue()));
00093         }
00094         if (function.equals("sqrt")) {
00095             if (isNumber(argument))
00096                 return cutDecimal(Math.sqrt((Double.valueOf(argument)).doubleValue()));
00097         }
00098             
00099         return function + "(" + argument + ")";
00100     }
00101     
00102     public static int type(String argument) {
00103 
00104         argument = removeBrackets(argument);
00105         
00106         if (isNumber(argument))
00107             return 0;
00108         else
00109             return 1;
00110     }
00111     
00118     private static boolean isNumber(String token) {
00119         char oneChar;
00120         
00121         token = removeBrackets(token);
00122         
00123         for (int i = 0; i < token.length(); i++) {
00124             oneChar = token.charAt(i);
00125             if (!Character.isDigit(oneChar) && (oneChar != '.'))
00126                 if (!((i == 0) && (oneChar == '-')))
00127                     return false;
00128         }
00129         return true;
00130     }
00131     
00135     private static String removeBrackets(String token) {
00136         if ((token.charAt(0) == '(') && (token.charAt(token.length() - 1) == ')'))
00137             return token.substring(1, token.length() - 1);
00138         else
00139             return token;
00140     }
00141     
00145     private static String cutDecimal(double value) {
00146         if ((value - Math.floor(value)) < 10e-5)
00147             return "" + (int) Math.floor(value);
00148         else
00149             return "" + value;
00150     }
00151 }