我需要将字符串转换为
class freud IF : 0.010<=cst0.1<=0.02 ^ 0.012<=cst0.2<=0.014 ^ 0.01<=cst0.3<=0.011 ^ 0.009<=cst0.4<=0.01 ^ cst0.5=0.009 ^ 0.008<=cst0.6<=0.009 ^ 0.008<=cst0.7<=0.009 ^ 0.007<=cst0.8<=0.009 ^ 0.007<=cst0.9<=0.009 ^ 0.008<=cst1.0<=0.012转换为实际的Java if语句:即
if(0.010 <= cst0.1 && cst0.1 <= 0.02 .....)其中cst0.1成为一个浮点数的名称,等等。
有什么想法吗?我已经尝试将字符串拆分成它的组成部分,但我需要的所有字符串都不同!
谢谢
发布于 2012-08-17 17:59:53
您可以尝试创建evaluate method boolean evaluate(String stringToEvaluate),它会将字符串拆分成段并计算每一位
public class MyClass {
public boolean evaluate(String stringToEvaluate)
{
boolean exprvalue = true;
for (String string : stringToEvaluate.split("^"))
{
exprvalue = exprvalue && evaluateLE(string);
}
return exprvalue;
}
private boolean evaluateLE(String string) {
String[] values = string.split("<=");
if (values.length==2)
{
return evaluateVal(values[0]) <= evaluateVal(values[1]);
}
return evaluateOtherLogicalExpr(string);
}
private float evaluateVal(String string) {
if (isExpresion(string)) return evaluateArthmeticalExpr(string);
if (isVariable(string)) return evaluateVariable(string);
return Float.parseFloat(string);
}
}发布于 2012-08-17 17:14:32
从您所展示的内容可以看出,在cstX.Y形式的字符串中,用一个有效的标识符字符替换点号就足够了,例如_,其中X和Y是数字,并将^替换为&&。
要使生成的if语句有效,还应该声明引入的所有cstX_Y变量。
发布于 2012-08-17 18:08:51
我想我有答案了。下面是我的步骤和代码:
>H111>基于3,5的create JAVA if语句,以及如果所有等式在整个行中都成立,则将其与要比较的数据进行比较的数据。
<>H111>如果一个失败了,就离开,换一条新的线。If all holds函数返回初始值,即Artist string。
不管怎样,下面是代码:
while(line != null ){
boolean equality = false;
String[] split_line;
String[] first_part;
String[] statements;
String[] components;
if(line.contains("IF")){
split_line = line.split(":");
statements = split_line[1].split(" ^ ");
int attrib_pos = 0;
for(int pos = 0; pos < statements.length; pos++){
if(statements[pos].contains("<=")){
components = statements[pos].split("<=");
float component_1 = new Float(components[0]);
// middle component is attribute
float component_2 = new Float(components[2]);
if(!(( component_1 <= data[attrib_pos]) && (data[attrib_pos] <= component_1))){
break;
}
}
else if(statements[pos].contains("=") ){
components = statements[pos].split("=");
float component_1 = new Float(components[1]);
if(!(component_1 == data[attrib_pos])){
break;
}
// if we have not found a false statement, increase no of attributes which hold
attrib_pos++;
}
}
// if we have run through all attributes as holding then return, as we have found a true rule
if(attrib_pos-1 == statements.length){
_return = (split_line[0].split("\\s+"))[2];
return _return;
}
}
// once we have read the line, read another
line = read.readLine();
}
}https://stackoverflow.com/questions/12002906
复制相似问题