dcsimg
Evaluate the value of a String expression, example: "5*4"
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, August 13, 2003 10:22 PM

Hi All, Is there any Arthemetic Engine available which evaluates a String expression and gives back a int/float/dobule value. Example: int a1= 10; int a2 = 12; String myExpr = "a1*a2"; When I send 'myExpr' as an argument to the engine, I have to get 120 (int/.. or any other data..). This engine should have the following: operators: +. -, *, / functions: max, min Hope you all got what I am trying to ask.. I would really appreciate your help.. Thanks, Satish    More>>

Hi All,
Is there any Arthemetic Engine available which evaluates a String expression and gives back a int/float/dobule value.

Example:


int a1= 10;

int a2 = 12;

String myExpr = "a1*a2";



When I send 'myExpr' as an argument to the engine, I have to get 120 (int/.. or any other data..).

This engine should have the following:

operators: +. -, *, /

functions: max, min


Hope you all got what I am trying to ask.. I would really appreciate your help..

Thanks,

Satish

   <<Less

Re: Evaluate the value of a String expression, example: "5*4"

Posted By:   joise_joise  
Posted On:   Wednesday, August 13, 2003 11:28 PM

Hi I donĀ“t know if exists any engine for evaluate expresions...
But I coded this class,I hope help you :

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
* @author jmramon
*@
*/
public class ExpresionsEvaluator {

private static final String OPERADOR_SUMA = "+";
private static final String OPERADOR_RESTA = "-";
private static final String OPERADOR_MULTIPLICADOR = "*";
private static final String OPERADOR_DIVISION = "/";
private static final String OPERADORES = "+-*/";


public static void main(String[] args) {


String expresion = "4*6+(5-29)+(30*${mivariableA}*${mivariableB})";
Map cachedObjects = new HashMap();
cachedObjects.put("mivariableA","10");
cachedObjects.put("mivariableB","10");
calcularExpresion(expresion,cachedObjects);

}
private static double calcularExpresion(String expresion,Map cachedObjects){


while (expresion.indexOf("${")!=-1) {
int inicioVariable = expresion.indexOf("${");
int finVariable = expresion.indexOf("}");
String nombreVariable = expresion.substring(inicioVariable+2,finVariable);
String nombreVariableR = "${"+nombreVariable+"}";
String valorVariable = (String)cachedObjects.get(nombreVariable);
expresion = StringUtils.replace(expresion,nombreVariableR,valorVariable);
}
return calcularExpresionParentesis(expresion);

}
private static double calcularExpresionParentesis(String expresion){


double calculo = 0;

int posicionAbreParentesis = expresion.indexOf("(");
int posicionCierraParentesis = expresion.indexOf(")");

boolean noParentesis = (posicionAbreParentesis==-1 || posicionCierraParentesis==-1);

if(noParentesis){

calculo = calcularExpresionSimple(expresion);

}else{

String subExpresion = expresion.substring(posicionAbreParentesis+1,posicionCierraParentesis);
System.out.println("subExpresion=" + subExpresion);
String calculoSubExpresionS = normalizaNumeroCalculo(calcularExpresionSimple(subExpresion));
String nuevaExpresion = StringUtils.replace(expresion,"("+subExpresion+")",calculoSubExpresionS);
System.out.println("nuevaExpresion=" + nuevaExpresion);
calcularExpresionParentesis(nuevaExpresion);

}
return calculo;

}
private static double calcularExpresionSimple(String expresion){


int size = expresion.length();


List listaOperandoOperador = new ArrayList();
int contador = 0;
for (int i = 0; i < size; i++) {

String caracter = expresion.substring(i, i + 1);



if (OPERADORES.indexOf(caracter) != -1) {

String operando = expresion.substring(contador, i);
String operador = caracter;
System.out.println("operando=" + operando);
System.out.println("operador=" + operador);
listaOperandoOperador.add(operando);
listaOperandoOperador.add(operador);
contador = i + 1;
}
if (i + 1 == size) {
listaOperandoOperador.add(expresion.substring(contador, size));
}

}
System.out.println("listaOperandoOperador=" + listaOperandoOperador);
double calculo = calcular(listaOperandoOperador);
System.out.println(calculo);
return calculo;
}
private static double calcular(List listaOperandoOperador) {

double calculo = 0;
int sizeLista = listaOperandoOperador.size();
System.out.println("sizeLista Principal=" + sizeLista);
if (sizeLista == 3) {

String numero1S = (String) listaOperandoOperador.get(0);
String operador = (String) listaOperandoOperador.get(1);
String numero2S = (String) listaOperandoOperador.get(2);

double numero1 = normalizaNumeroCalculo(numero1S);
double numero2 = normalizaNumeroCalculo(numero2S);
calculo = calcular(numero1, numero2, operador);

} else {



List nuevaLista = listaOperandoOperador.subList(0, 3);
System.out.println("sublista=" + nuevaLista);
double calculoParcial = calcular(nuevaLista);
System.out.println("calculoParcial=" + calculoParcial);
String calculoParcialS = normalizaNumeroCalculo(calculoParcial);
List listaResto = listaOperandoOperador.subList(3, sizeLista);
listaResto.add(0, calculoParcialS);
System.out.println("listaOperandoOperador Resto=" + listaResto);
System.out.println(
"listaOperandoOperador Resto=" + listaResto.size());
calculo = calcular(listaResto);


}
return calculo;
}
private static double multiplicar(double x, double y) {
return x * y;
}
private static double dividir(double x, double y) {
return x / y;
}
private static double sumar(double x, double y) {
return x + y;
}
private static double restar(double x, double y) {
return x - y;
}
private static double calcular(double x, double y, String operador) {

double calculo = 0;

if (OPERADOR_SUMA.equals(operador)) {

calculo = sumar(x, y);

}
if (OPERADOR_RESTA.equals(operador)) {

calculo = restar(x, y);

}
if (OPERADOR_MULTIPLICADOR.equals(operador)) {

calculo = multiplicar(x, y);

}
if (OPERADOR_DIVISION.equals(operador)) {

calculo = dividir(x, y);

}
return calculo;

}
private static double normalizaNumeroCalculo(String numero){

double numeroD = 0;

if(numero.startsWith("#")){

numero = numero.substring(1,numero.length()-1);
double num = Double.parseDouble(numero);
numeroD = (double)-1*num;
}else{
numeroD = Double.parseDouble(numero);
}
return numeroD;
}
private static String normalizaNumeroCalculo(double numero){

String numeroS = "";
if(numero<0){

numeroS = "#"+String.valueOf(Math.abs(numero));

}else{
numeroS = String.valueOf(numero);
}
return numeroS;

}
}
About | Sitemap | Contact