要求是前綴運算符將像AND(a,b)
,OR(a,b)
或NOT(a)
到中綴,這樣的:(a && b)
,(a || b)
,(!(a))
前綴中綴使用Java
我已經寫了下面的代碼,但它的作品,如果表達ISN」太複雜了。我能夠轉換: AND(OR(1<2, OR(3<4, 1<2, FUNC(5<6, 2<3))), 2<3)
到
(((1<2 || ((3<4 || (1<2 || FUNC(5<6, 2<3))))))&& 2<3))
除了那些額外的括號外,這個表達式是可以接受的。但是當我運行這個表達式的代碼有點複雜時,它裏面有太多的函數和括號,或者失敗或者返回表達式。例如這個表達式: AND(OR((NOT(A != null)), OR(FUNC(3<4, 1==1), 1<2, FUNC(5<6, 2<3))), 2<3)
它應該忽略除了And/Or/Not之外的其他函數。例如FUNC(5<6, 2<3)
應輸出爲FUNC(5<6, 2<3)
,正如我在上面的例子中提到的那樣。
代碼:
public String ConvertToJS(String sExpr, String Operator)
{
//String subExpr[] = sExpr.split(",");
sExpr = sExpr.trim();
String resolved = "";
String resolved2 = "";
if(sExpr.indexOf(",") != -1 || sExpr.indexOf("(") != -1)
{
if((sExpr.indexOf(",") != -1 && sExpr.indexOf("(") != -1 && sExpr.indexOf(",") < sExpr.indexOf("(")) || sExpr.indexOf("(") == -1)
{
if(sExpr.indexOf(",") > 0)
{
if("AND".equalsIgnoreCase(Operator))
return "(" + sExpr.substring(0, sExpr.indexOf(",")) + " && " + ConvertToJS(sExpr.substring(sExpr.indexOf(",")+1, sExpr.length()), Operator) + ")";
else if("OR".equalsIgnoreCase(Operator))
return "(" + sExpr.substring(0, sExpr.indexOf(",")) + " || " + ConvertToJS(sExpr.substring(sExpr.indexOf(",")+1, sExpr.length()), Operator) + ")";
else
return sExpr;
}
else
{
if("AND".equalsIgnoreCase(Operator))
return " && " + ConvertToJS(sExpr.substring(sExpr.indexOf(",")+1, sExpr.length()), Operator) + ")";
else if("OR".equalsIgnoreCase(Operator))
return " || " + ConvertToJS(sExpr.substring(sExpr.indexOf(",")+1, sExpr.length()), Operator) + ")";
else
return sExpr;
}
}
else
{
if(sExpr.indexOf("(") < 2)
{
resolved = sExpr.substring(0, sExpr.indexOf("(")) + "(" + ConvertToJS(sExpr.substring(sExpr.indexOf("(")+1, sExpr.lastIndexOf(")")), "") + ")";
if(sExpr.lastIndexOf(")")< sExpr.length()-1)
resolved += ConvertToJS(sExpr.substring(sExpr.lastIndexOf(")") + 1), Operator);
return resolved;
}
else if(sExpr.indexOf("(") == 2)
{
if(sExpr.substring(0, sExpr.indexOf("(")).equalsIgnoreCase("OR"))
{
resolved = "(" + ConvertToJS(sExpr.substring(sExpr.indexOf("(")+1, sExpr.lastIndexOf(")")), "OR") + ")";
if(sExpr.lastIndexOf(")")< sExpr.length()-1)
resolved += ConvertToJS(sExpr.substring(sExpr.lastIndexOf(")") + 1), Operator);
return resolved;
}
else
{
resolved = sExpr.substring(0, sExpr.indexOf("(")) + "(" + ConvertToJS(sExpr.substring(sExpr.indexOf("(")+1, sExpr.lastIndexOf(")")), "") + ")";
if(sExpr.lastIndexOf(")")< sExpr.length()-1)
resolved += ConvertToJS(sExpr.substring(sExpr.lastIndexOf(")") + 1), Operator);
return resolved;
}
}
else if(sExpr.indexOf("(") == 3)
{
if(sExpr.substring(0, sExpr.indexOf("(")).equalsIgnoreCase("AND"))
{
resolved = "(" + ConvertToJS(sExpr.substring(sExpr.indexOf("(")+1, sExpr.lastIndexOf(")")), "AND") + ")";
if(sExpr.lastIndexOf(")")< sExpr.length()-1)
resolved += ConvertToJS(sExpr.substring(sExpr.lastIndexOf(")") + 1), Operator);
return resolved;
}
else if(sExpr.substring(0, sExpr.indexOf("(")).equalsIgnoreCase("NOT"))
{
resolved = "(" + ConvertToJS(sExpr.substring(sExpr.indexOf("(")+1, sExpr.lastIndexOf(")")), "NOT") + ")";
if(sExpr.lastIndexOf(")")< sExpr.length()-1)
resolved += ConvertToJS(sExpr.substring(sExpr.lastIndexOf(")") + 1), Operator);
return resolved;
}
else
{
resolved = sExpr.substring(0, sExpr.indexOf("(")) + "(" + ConvertToJS(sExpr.substring(sExpr.indexOf("(")+1, sExpr.lastIndexOf(")")), "") + ")";
if(sExpr.lastIndexOf(")")< sExpr.length()-1)
resolved += ConvertToJS(sExpr.substring(sExpr.lastIndexOf(")") + 1), Operator);
return resolved;
}
}
else
{
resolved = sExpr.substring(0, sExpr.indexOf("(")) + "(" + ConvertToJS(sExpr.substring(sExpr.indexOf("(")+1, sExpr.lastIndexOf(")")), "") + ")";
if(sExpr.lastIndexOf(")")< sExpr.length()-1)
resolved += ConvertToJS(sExpr.substring(sExpr.lastIndexOf(")") + 1), Operator);
return resolved;
}
}
}
else
{
if("NOT".equalsIgnoreCase(Operator))
return " !(" + sExpr + ") ";
else
return sExpr;
}
}
[前綴中綴轉換算法與圖]可能重複(http://stackoverflow.com/questions/4374388/prefix-to-infix-conversion-algorithm-with-figure) –
@JulienLopez,謝謝,但我檢查發佈Q之前的網站。他正在使用Stack。我不是。 – Enthusiastic
@Enthusiastic儘管你的代碼沒有明確的引用棧,但你仍然通過遞歸來使用它。 – dasblinkenlight