最近在写一个比较复杂的业务程序,其中有个这样一张表,
1、它即可以实现每条记录之间的或者是所有记录上的部分区域的链式结构
2、它也可以实现两条记录间的与非关系统,
3、它还可以实现几条记录之间的优先级关系。括号语法。
4、它可以进行几条数据之间的并行操作.
现列出经典部分代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <script language="javascript"> // Array还有sort()、reverse()、splice(pos,count)--从数组的第pos位开始,删除count个元素//;splice(pos,count,"element1","element2")--替换或删除 check = function(){ return false; } //堆栈类 function Stack(){ this.stack = new Array(); } Stack.prototype.push = function(obj){ this.stack.push(obj); } Stack.prototype.pop = function(){ if(this.length()==0){ //alert("暂无元素"); return } return this.stack.pop(); } Stack.prototype.lastElement = function(){ if(this.length()==0){ //alert("暂无元素"); return } return this.stack[this.length()-1]; } Stack.prototype.length = function(){ return this.stack.length; } Stack.prototype.toString = function (){ return this.stack.toString(); } function checkBracket(objArray){ var bracketStack = new Stack(); var sb = new Array(); //存放每次括号中运算的结果 var result = ""; /*** * 如果判断的字符串不是以(开头,则进行,首未加括号 */ if(objArray[0]!="("){ for(var i = 0 ;i< objArray.length;i++){ objArray[objArray.length-i] = objArray[objArray.length-i-1]; } objArray[0] = "("; objArray[objArray.length]= ")"; } if(objArray[objArray.length-1]!=")"){ for(var i = 0 ;i< objArray.length;i++){ objArray[objArray.length-i] = objArray[objArray.length-i-1]; } objArray[0] = "("; objArray[objArray.length]= ")"; } for(var i=0; i<objArray.length; i++) { var temp = objArray[i] switch(temp){ case ')': debugger; sb = new Array(); var lastElement = bracketStack.lastElement(); if(lastElement == undefined){ alert("括号匹配有问题!"); return ; } //将第一个括号中的内容出栈 while(lastElement !="(" && bracketStack.length()>0){ bracketStack.pop(); sb.push(lastElement); lastElement = bracketStack.lastElement(); if(lastElement =="("){ bracketStack.pop(); } } if(bracketStack.length() == 0){ result = check(sb); break; }else{ bracketStack.push(check(sb)); } alert(sb.toString()); break; case '(': /** * 将左括号进栈 */ bracketStack.push(temp); break; default : //将非括号的存放到临时数组 bracketStack.push(temp); break; } } if(bracketStack.length()>0){ alert("括号匹配有误!!!!!"); return false; }else{ alert(result); } return true; } var obj1 = { logic: "or", value: 1, leftBracket :"((", rightBracket :"" } var obj2 = { logic: "and", value: 2, leftBracket :"", rightBracket :"" } var obj3 = { logic: "and", value: 3, leftBracket :"(", rightBracket :"" } var obj4 = { logic: "or", value: 4, leftBracket :"", rightBracket :")" } var obj5 = { logic: "or", value: 5, leftBracket :"(", rightBracket :"" } var obj6 = { logic: "and", value: 6, leftBracket :"", rightBracket :")" } var obj7 = { logic: "or", value: 7, leftBracket :"", rightBracket :"" } var obj8 = { logic: "or", value: 8, leftBracket :"", rightBracket :"))))" } var objArray = new Array(); objArray.push(obj1); objArray.push(obj2); objArray.push(obj3); objArray.push(obj4); objArray.push(obj5); objArray.push(obj6); objArray.push(obj7); objArray.push(obj8); var array = new Array(); for(var i = 0 ;i<objArray.length;i++){ var condition = objArray[i]; var leftBracket =condition.leftBracket; var rightBracket = condition.rightBracket; if(leftBracket!=""){ for(var j = 0 ;j<leftBracket.length;j++){ array.push("("); } } array.push(condition); if(rightBracket!=""){ for(var j = 0 ;j<rightBracket.length;j++){ array.push(")"); } } array.push(condition.logic); } array.pop(); var flag = false; for(var i = 0 ;i<array.length;i++){ var obj = array[i]; if (obj == "(" || obj == ")" || obj == "and" || obj =="or") { continue; }else{ if(obj.value == 5){ flag = true; } if(flag){ array[i] ="-"; } } }; var bracketFlag = checkBracket(array); </script> </head> <body> </body> </html>
JAVA代码
/**
*
* @param str
* @return
*/
public static String checkBracket(String str ){
Stack bracketStack = new Stack();
StringBuffer sb = new StringBuffer();
String result = "";
/***
* 如果判断的字符串不是以(开头,则进行,首未加括号
*/
if(str.charAt(0)!='('){
str = "("+str+")";
}
try{
for(int i=0; i<str.length(); i++)
{
char temp = str.charAt(i);
switch(temp){
case ')':
if(!result.equals("")){
result = check(result+sb.toString());
// result = sb.toString();
System.out.println(sb.toString());
}else{
result = check(sb.toString());
// result = sb.toString();
System.out.println(sb.toString());
}
sb = new StringBuffer();
String lastElement = bracketStack.lastElement().toString();
//将第一个括号中的内容出栈
while(!lastElement.equals("(")){
bracketStack.pop();
lastElement = bracketStack.lastElement().toString();
}
//将左括号出栈
bracketStack.pop();
/**
* 看左括号前面是否为(
* 如果不是则进行出栈
* 如果是,继续下一个字符串的读取
*/
lastElement = bracketStack.lastElement().toString();
if(!lastElement.equals("(")){
lastElement = (String)bracketStack.pop();
// System.out.println(lastElement+result);
sb.append(lastElement+result);
}
break;
case '(':
bracketStack.push(sb.toString());
sb = new StringBuffer();
bracketStack.push(temp);
break;
default :
sb.append(temp);
break;
}
}
}catch(Exception e){
System.out.println("括号不匹配");
}
Iterator it= bracketStack.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
// System.out.println("first ==========="+bracketStack.lastElement());
return result;
}