📄 postorder.java
字号:
package searchingEngine.booleanModel;
import java.util.List;
import java.util.LinkedList;
import java.util.Stack;
public class PostOrder extends LinkedList<String>{
private String input[];
//private List input;
private Stack<String> theStack;
/*
public PostOrder(List input) {
super();
this.input=input;
theStack = new Stack<String>();
doTrans();
}
private void doTrans() {
for (int j = 0; j < input.size(); j++) {
String ch = input[j];
if (ch.equals("and") || ch.equals("or") || ch.equals("not"))
gotOper(ch);
else if (ch.equals("(")) theStack.push(ch);
else if (ch.equals(")")) gotParen(ch);
else add(ch);
}
while (!theStack.isEmpty()) {
add(theStack.pop());
}
theStack = null;
}
*/
public PostOrder(String[] input) {
super();
this.input=input;
theStack = new Stack<String>();
doTrans();
}
private void doTrans() {
for (int j = 0; j < input.length; j++) {
String ch = input[j];
if (ch.equalsIgnoreCase("and") || ch.equals("&") || ch.equalsIgnoreCase("or") || ch.equals("|") ||ch.equalsIgnoreCase("not") || ch.equals("!"))
gotOper(ch);
else if (ch.equals("(")) theStack.push(ch);
else if (ch.equals(")")) gotParen(ch);
else add(ch);
}
while (!theStack.isEmpty()) {
add(theStack.pop());
}
theStack = null;
}
private void gotOper(String opThis) {
while (!theStack.isEmpty()) {
String opTop = theStack.pop();
if (opTop.equals("(")) {
theStack.push(opTop);
break;
}// it's an operator
if (opTop.equalsIgnoreCase("and") || opTop.equals("&") || opTop.equalsIgnoreCase("or") || opTop.equals("|") || opTop.equalsIgnoreCase("not") || opTop.equals("!"))
add(opTop);
}
theStack.push(opThis);
}
private void gotParen(String ch){
while (!theStack.isEmpty()) {
String chx = theStack.pop();
if (chx.equals("("))// == '(')
break;
else
add(chx);// = output + chx;
}
}
public String[] getInput(){
return input;
}
public static void main(String[] args) {
String input = new String("vsm and ( example or tf ) and idf");
System.out.println(input);
String inputS[] = input.split(" ");
PostOrder theTrans = new PostOrder(inputS);
System.out.println(theTrans);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -