打开字符串如果语句一个Java IF语句
问题描述:
我需要把一个字符串,如打开字符串如果语句一个Java IF语句
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成为名浮动等
任何想法?我已经尝试将字符串拆分为其组件,但是我需要的所有字符串都不相同!
感谢
答
你可以尝试创建评估方法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);
}
}
+0
哇,你的比我的更漂亮!谢谢! – redrubia 2012-08-17 10:11:20
答
从大家展示一下它应该是足够的,以取代一个有效的标识字符,如点_
,其格式为cstX.Y
,其中X和Y是数字,并用&&
代替^
。
要使生成的if语句有效,还应该声明所有您引入的cstX_Y
变量。
答
我想我有一个答案。这里是我的步骤,然后代码:
- 确定如果行有IF在它
- 如果确实如此,那么分割线在平等的部件采用分体式(“^”) - 这将在结构上有所不同。
- 现在检查每个组件,检查是否存在相等“< =”,“=”
- 每个独立处理根据结构。
- 将每个“0.025”(例如)转换为浮点数
- 创建JAVA if语句基于3,5和您想要比较的数据。
- 如果全部平等贯穿始终如一的记录。如果一个人失败了,可以脱身,换一个新的线。如果所有保持功能返回初始值,即艺术家字符串。
反正这里是代码:
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();
}
}
你需要一个语法为您弗洛伊德IF语句。将其解析为AST,然后遍历该树以生成Java IF语句。 – duffymo 2012-08-17 09:10:50
字符串是否存在底层语法?你可能需要一个解析器。你可以看看JavaCC,但我听说它有一个陡峭的学习曲线。 – 2012-08-17 09:11:09
@SLBarth在使用' redrubia 2012-08-17 09:16:16