根据一个字符串,如:
(((Name = '1' and Sex = '2') and (Age = '3' and Birthday = '4')) and Marriage = '5')怎样分解成如下字符串:
1、(((Name = '1' and Sex = '2') and (Age = '3' and Birthday = '4')) and Marriage = '5')
2、((Name = '1' and Sex = '2') and (Age = '3' and Birthday = '4'))
3、(Name = '1' and Sex = '2')
4、(Age = '3' and Birthday = '4')
(((Name = '1' and Sex = '2') and (Age = '3' and Birthday = '4')) and Marriage = '5')怎样分解成如下字符串:
1、(((Name = '1' and Sex = '2') and (Age = '3' and Birthday = '4')) and Marriage = '5')
2、((Name = '1' and Sex = '2') and (Age = '3' and Birthday = '4'))
3、(Name = '1' and Sex = '2')
4、(Age = '3' and Birthday = '4')
public static void main(String[] args) throws Exception
{
Deque<Character> stack = new ArrayDeque<Character>();
Deque<Integer> stackIdx = new ArrayDeque<Integer>();
String str = "(((Name = '1' and Sex = '2') and (Age = '3' and Birthday = '4')) and Marriage = '5'";
boolean flag = true;
char ch;
for(int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
if(ch == '(') {
stack.offerFirst(ch);
stackIdx.offerFirst(i);
}
else if(ch == ')') {
if(stack.peekFirst() != null && stack.peekFirst() == '(') {
stack.pollFirst();
int last = stackIdx.pollFirst();
System.out.println(str.substring(last,i+1));
}
else {
flag = false;
break;
}
}
}
}
<script>
var str = "(((Name = '1' and Sex = '2') and (Age = '3' and Birthday = '4')) and Marriage = '5')";
var content = new Array();
var arr = new Array();
for(var i=0;i<str.length;i++){
if(str.charAt(i)=="("){
arr.push(i);
}else if(str.charAt(i)==")"){
var s = arr.pop();
content.push(str.substring(s,i+1));
}
}
var len = content.length;
for(var i=0;i<len;i++){
alert(content.pop());
}
</script>
item:this,
box:FBuild.utils.createDiv('itemBox'),
icon:FBuild.utils.createDiv('iconBox'),
title:FBuild.utils.createDiv('title'),
show:FBuild.utils.createDiv('imgShow'),
init:function(){
this.title.innerHTML = this.item._title;//标题
this.icon.style.backgroundPosition = 'center ' + (-(+this.item._type*16)) + 'px';//图标定位
FBuild.utils.addEvent(this.show,'onclick',function(ev,obj,o){
if(!o.isShow){
o.item._detail.nameText.value = o.title.innerHTML;
o.item._item.box.style.borderBottomWidth = 0;
o.item._detail.box.style.display = 'block';
o.show.style.backgroundPosition = 'left 0px';
o.isShow = true;
o.item._displayBox.style.zIndex = 999;
}else{
o.item._item.box.style.borderBottomWidth = 1;
o.item._detail.box.style.display = 'none';
o.show.style.backgroundPosition = 'left -5px';
o.isShow = false;
o.item._displayBox.style.zIndex = 0;
}
},this);
FBuild.utils.addEvent(this.box,'onmouseover',function(ev,obj,o){
o.item._toolBar.box.style.visibility = 'visible';
o.item._toolBar.del.style.display = o.item._childs.length > 0 || o.item==o.item._canvas._items[0]?"none":"block";
var selectedItem = o.item._canvas._selectedItem;
if(selectedItem && selectedItem!=o.item && !selectedItem._childs.isContain(o.item) && !selectedItem._parent.isContain(o.item)){//有节点点击了连线事件
o.item._displayBox.style.filter = "progid:DXImageTransform.Microsoft.Shadow(Color=red,Direction=120,strength=5)";
}
},this);
FBuild.utils.addEvent(this.box,'onmouseout',function(ev,obj,o){
o.item._toolBar.box.style.visibility = 'hidden';
if(o.item._canvas._selectedItem && o.item._canvas._selectedItem != o.item){//有节点点击了连线事件
o.item._displayBox.style.filter = "progid:DXImageTransform.Microsoft.Shadow(Color=#555555,Direction=120,strength=5)";
}
},this);
FBuild.utils.addEvent(this.box,'onclick',function(ev,obj,o){
var selectedItem = o.item._canvas._selectedItem;
if(selectedItem){//有节点点击了连线事件
if(selectedItem != o.item && !selectedItem._childs.isContain(o.item) && !selectedItem._parent.isContain(o.item)){
new FBuild.Line(o.item._canvas._selectedItem,o.item);//构建线条 并 显示
}
o.item._displayBox.style.filter = o.item._canvas._selectedItem._displayBox.style.filter =
"progid:DXImageTransform.Microsoft.Shadow(Color=#555555,Direction=120,strength=5)";
o.item._canvas._selectedItem = null;
}
},this);
FBuild.utils.appends(this.box,[this.icon,this.title,this.show]);
}
}
item:this,
box:FBuild.utils.createDiv('detailBox'),
nameText:FBuild.utils.createInput("text",null,this._title,'text'),
nodeType:FBuild.utils.createSelect(FBuild.TYPES,this._type),
tab:document.createElement("table"),
init:function(){
FBuild.utils.addEvent(this.nameText,'onkeyup',function(ev,obj,o){
o.item._title = o.item._item.title.innerHTML = obj.value;
},this);
FBuild.utils.addEvent(this.nodeType,'onchange',function(ev,obj,o){
o.item._type = obj.value;
o.item._item.icon.style.backgroundPosition = 'center ' + (-(+o.item._type*16)) + 'px';//图标定位
},this);
try{//IE
var r0 = this.tab.insertRow();
var r0_c0 = r0.insertCell();r0_c0.innerHTML = "名称:";
var r0_c1 = r0.insertCell();r0_c1.appendChild(this.nameText);
var r1 = this.tab.insertRow();
var r1_c0 = r1.insertCell();r1_c0.innerHTML = "节点类型:";
var r1_c1 = r1.insertCell();r1_c1.appendChild(this.nodeType);
var r2 = this.tab.insertRow();
var r2_c0 = r2.insertCell();r2_c0.innerHTML = "语音文件:";
var r2_c1 = r2.insertCell();r2_c1.appendChild(FBuild.utils.createInput("text",'','','text'));
}catch(cex){//Firefox
var r0 = document.createElement("tr");
var r0_c0 = document.createElement("td");r0_c0.innerHTML = "名称:";
var r0_c1 = document.createElement("td");r0_c1.appendChild(this.nameText);
FBuild.utils.appends(r0,[r0_c0,r0_c1]);
var r1 = document.createElement("tr");
var r1_c0 = document.createElement("td");r1_c0.innerHTML = "节点类型:";
var r1_c1 = document.createElement("td");r1_c1.appendChild(this.nodeType);
FBuild.utils.appends(r1,[r1_c0,r1_c1]);
var r2 = document.createElement("tr");
var r2_c0 = document.createElement("td");r2_c0.innerHTML = "语音文件:";
var r2_c1 = document.createElement("td");r2_c1.appendChild(FBuild.utils.createInput("text",'','','text'));
FBuild.utils.appends(r2,[r2_c0,r2_c1]);
FBuild.utils.appends(this.tab,[r0,r1,r2]);
}
FBuild.utils.appends(this.box,[this.tab]);
}
}
this._displayBox = FBuild.utils.createDiv('item_wrap',this._left,this._top);
this._toolBar.init();
this._item.init();
this._detail.init();
FBuild.utils.appends(this._displayBox,[this._toolBar.box,this._item.box,this._detail.box]);
FBuild.utils.drag.init(this);//添加 拖曳 事件
};
//添加 子节点
FBuild.Item.prototype.addChildItem = function(){
var top = this._childs.length>0?this._childs[this._childs.length-1]._top+(this._item.box.offsetHeight + this._item.box.offsetTop + FBuild.ITEM_SPACE_Y):this._top;
var left = this._left + FBuild.ITEM_SPACE_X + this._item.box.offsetWidth;
var child = new FBuild.Item('放音收号',1,left,top);
child._canvas = this._canvas;
this._canvas._contentBox.appendChild(child._displayBox);
this._canvas._items.push(child);
new FBuild.Line(this,child);//构建线条 并 显示
this.moves();
};
FBuild.Line = function(fr,to,isAuto){
var itemW = fr._item.box.offsetWidth;
var itemH = fr._item.box.offsetHeight;
var toolBarH = fr._toolBar.box.offsetHeight;
var fr_rightP = {x:fr._left+itemW,y:fr._top+toolBarH+Math.floor(itemH/2)};
var fr_leftP = {x:fr._left,y:fr._top+toolBarH+Math.floor(itemH/2)};
var fr_topP = {x:fr._left+Math.floor(itemW/2),y:fr._top+toolBarH};
var fr_bottomP = {x:fr._left+Math.floor(itemW/2),y:fr._top+toolBarH+itemH};
var to_rightP = {x:to._left+itemW,y:to._top+toolBarH+Math.floor(itemH/2)};
var to_leftP = {x:to._left,y:to._top+toolBarH+Math.floor(itemH/2)};
var to_topP = {x:to._left+Math.floor(itemW/2),y:to._top+toolBarH};
var to_bottomP = {x:to._left+Math.floor(itemW/2),y:to._top+toolBarH+itemH};
var arrP = [];//线条 位置
if(to_leftP.x > fr_rightP.x){//→
arrP.push(fr_rightP);
if(to_leftP.y != fr_rightP.y){//折线
var X = fr_rightP.x+Math.floor((to_leftP.x-fr_rightP.x)/2);
arrP.push({x:X,y:fr_rightP.y});
arrP.push({x:X,y:to_leftP.y});
}
arrP.push(to_leftP);
}else if(to_rightP.x < fr_leftP.x){//←
arrP.push(fr_leftP);
if(to_leftP.y != fr_rightP.y){//折线
var X = fr_leftP.x-Math.floor((fr_leftP.x-to_rightP.x)/2);
arrP.push({x:X,y:fr_rightP.y});
arrP.push({x:X,y:to_leftP.y});
}
arrP.push(to_rightP);
}else if(to_topP.y > fr_bottomP.y){//↓
arrP.push(fr_bottomP);
if(to_topP.x != fr_bottomP.x){//折线
var H = Math.floor((to_topP.y - fr_bottomP.y)/2);
arrP.push({x:fr_bottomP.x,y:fr_bottomP.y+H});
arrP.push({x:to_topP.x,y:fr_bottomP.y+H});
}
arrP.push(to_topP);
}else{//↑
arrP.push(fr_topP);
if(to_topP.x != fr_bottomP.x){//折线
var H = Math.floor((fr_topP.y - to_bottomP.y)/2);
arrP.push({x:fr_bottomP.x,y:fr_topP.y-H});
arrP.push({x:to_topP.x,y:fr_topP.y-H});
}
arrP.push(to_bottomP);
} this._isAuto = isAuto || true;//是否自动产生的线条
this._frItem = fr;
this._toItem = to; this._lines = FBuild.utils.drawLine(arrP);
var lastLine = this._lines[this._lines.length-1];
this._imgWay = FBuild.utils.createDiv('imgWay',lastLine.imgWayPoint.x,lastLine.imgWayPoint.y,FBuild.IMGWAY_SIZE,FBuild.IMGWAY_SIZE);
this._imgWay.style.backgroundPosition = 'center ' + (-FBuild.IMGWAY_SIZE * lastLine.way) + 'px';
FBuild.utils.appends(this._frItem._canvas._contentBox,this._lines.concat(this._imgWay));
this._frItem._lines.push(this);
this._toItem._parent.push(this._frItem);
this._frItem._childs.push(this._toItem);
//点击线条箭头事件
FBuild.utils.addEvent(this._imgWay,'onclick',function(ev,obj,o){
var selectedLine = o._frItem._canvas._seletedLine;
if(selectedLine){
for(var i=0;i<selectedLine._lines.length;i++){
selectedLine._lines[i].className = 'lx';
}
o._frItem._canvas._seletedLine = null;
document.onkeydown = null;
}
if(selectedLine != o){
for(var i=0;i<o._lines.length;i++){
o._lines[i].className = 'lx_on';
}
o._frItem._canvas._seletedLine = o;
document.onkeydown = function(e){
var e = e || window.event;
if(e.keyCode == 46){//按下 Delete 键事件
o._frItem.removeLine(o._toItem);
document.onkeydown = null;
}
};
}
},this);
}
//画布
FBuild.Canvas = function(cBox,rootItem){
this._contentBox = FBuild.utils.get(cBox);
rootItem._canvas = this;
this._items = [rootItem];
this._contentBox.appendChild(rootItem._displayBox);
};