将某一表达式放入一个String中,其中会带有"&&"和"||"的判断,然后判断这个表达式是否成立
如下:
String a = "(50-20>10)&&(2+3>0)||(10-1<10)"if(a){
//正确......
}else{
//错误......
}请教各位高手谁有思路实现这种程序,谢谢了
如下:
String a = "(50-20>10)&&(2+3>0)||(10-1<10)"if(a){
//正确......
}else{
//错误......
}请教各位高手谁有思路实现这种程序,谢谢了
解决方案 »
- 有人做过Jpcapde抓包么?
- 如果是按钮button,鼠标移上去变成手指图标。代码怎么写...谢谢...
- 关于java中Jlist如何连接到access数据库
- 请教各位 一个定义变量的问题,
- Applet中的一个绘图问题
- 重写!
- 我编的聊天程序,为什么程序不能正常退出
- 送100分!Lotus 与java有什么联系没有?Lotus的发展潜力大么,从基于j2ee的web开发,转到lotus划得来不
- java对机器配置是最低要求是什么?在win98可不可以写JAVA程序?
- java 字符串截取
- TWaverUtil这个类在TWaver.jar包里怎么找不到额
- 下面的bat脚本改成shell脚本怎么写
然后再解析X,Y,Z...算术表达式。
表达式的解析在任何一本数据结构的书上都能找到关键词:“栈”
//正确......
}else{
//错误......
}这样修改之后就可以实现相同的功能!如果纯粹是为了学习,可以试着做一下!beanutils
// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager(); // create a JavaScript engine
ScriptEngine engine = factory.getEngineByName("JavaScript"); // evaluate JavaScript code from String
final boolean b = (Boolean)engine.eval("(50-20>10)&&(2+3>0)||(10-1<10))");
System.out.println(b);
}
private static string falseCase = "FALSE";
private static string andCase = "&&";
private static string orCase = "||";
private static char spaceCase = ' ';
private static char leftParenthesesCase = '(';
private static char rightParenthesesCase = ')'; static void Main()
{
string expression = "A &&((B || C) && D OR E) && F && (G || H)";
Dictionary<string, bool> dict = new Dictionary<string, bool>();
dict.Add(trueCase, true);
dict.Add(falseCase, false);
dict.Add("A", true);
dict.Add("B", false);
dict.Add("C", false);
dict.Add("D", true);
dict.Add("E", true);
dict.Add("F", true);
dict.Add("G", false);
dict.Add("H", false); string result = ProcessLogicExpression(expression,dict);
Console.WriteLine("Result is : " + result);
Console.Read();
} static string ProcessLogicExpression(string expression, Dictionary<string, bool> dict)
{
while (expression.Contains(leftParenthesesCase))
{
int left = 0, right = 0;
bool matchRightParentheses = false;
for (int i = 0; i < expression.Length; i++)
{
if (expression[i].Equals(leftParenthesesCase))
{
left = i;
for (int j = i + 1; j < expression.Length; j++)
{
if (expression[j].Equals(leftParenthesesCase))
{
left = j;
i = j;
} if (expression[j].Equals(rightParenthesesCase))
{
right = j;
matchRightParentheses = true;
break;
}
} if (matchRightParentheses) break;
}
} int beginIndex = left + 1, length = right - left - 1;
string subExpression = expression.Substring(beginIndex, length);
string oriStr = leftParenthesesCase + subExpression + rightParenthesesCase;
string curStr = ProcessLogicExpression(subExpression, dict);
expression = expression.Replace(oriStr, curStr);
} expression = ProcessByLogicCase(expression, dict, andCase);
expression = ProcessByLogicCase(expression, dict, orCase); return expression;
} static string ProcessByLogicCase(string expression, Dictionary<string, bool> dict, string logicCase)
{
while (expression.Contains(logicCase))
{
string[] strArray = expression.Split(spaceCase);
for (int i = 0; i < strArray.Length; i++)
{
if (strArray[i].Equals(logicCase))
{
bool bResult;
if (logicCase.Equals(andCase))
{
bResult = dict[strArray[i - 1]] && dict[strArray[i + 1]];
}
else
{
bResult = dict[strArray[i - 1]] || dict[strArray[i + 1]];
} string oriStr = strArray[i - 1] + spaceCase + logicCase + spaceCase + strArray[i + 1];
string curStr = bResult.ToString().ToUpper();
expression = expression.Replace(oriStr, curStr);
break;
}
}
} return expression;
}
+1
jdk1.6以上,可以使用这个
或者可以自己写个语法分析器并执行结果