www.pudn.com > Operate2.rar > ConstituteTree.java
package Expression;
import java.util.ArrayList;
public class ConstituteTree {
private Expression expression;
private String Command;
private int Len,Order;
private int IncreasPRI = 0;//优先级增量
private ArrayList Paralist = new ArrayList();//值数组
private ArrayList Operlist = new ArrayList();//运算符数组
private ArrayList Bracketlist = new ArrayList();//括号数组
private ArrayList Alllist = new ArrayList();
private Expression[] para;
public ConstituteTree(String command) {
super();
// TODO Auto-generated constructor stub
Command = command.trim() + " ";
Len = Command.length();
expression = new Expression(2);
}
//下面的函数实现的了将表达式中的所有值,括号和运算符分类
public void Gen(){
String str = "";
boolean IsKey = false,ISDoubleKey = false,IsKeyWords =false;
char parameter ;
Order=0;
for(int i=0;i= oper[i].getPRI() && temp.getParent().getPRI() '0'&& ch [0]< '9'){
for (int j=1; j'9'){
System.out.println("存在非法的标识符。");
return;
}
}
}
}
Expression[] oper= new Expression[Operlist.size()];
Operlist.toArray(oper);
Bracket[] brac = new Bracket[Bracketlist.size()];
Bracketlist.toArray(brac);
int left=0,right=0,semi=0;
if (oper.length +1 != para.length){
System.out.println("值与运算符不能对应。");
return;
}
for (int i=0;i= Alllist.size()||(Alllist.get(i+1) instanceof Expression && ((Expression) Alllist.get(i+1)).getType() == 0)){
WrongOutput();
return;
}
if (i-1 > 0){
if ((Alllist.get(i-1) instanceof Expression && ((Expression) Alllist.get(i-1)).getType() == -1)){
WrongOutput();
return;
}
}
}
else if (((Bracket) Alllist.get(i)).getType() == 1){
if (i-1 < 0||((Alllist.get(i-1) instanceof Expression && ((Expression) Alllist.get(i-1)).getType() == 0))){
WrongOutput();
return;
}
if (i+1 < Alllist.size()){
if ((Alllist.get(i+1) instanceof Expression && ((Expression) Alllist.get(i+1)).getType() == -1)){
WrongOutput();
return;
}
}
}
}
}
for (int i= 0;ipara.length){
WrongOutput();
return i;
}
}
}
else{
if (expression != this.expression){
expression.setLeftExp(para[i]);
i++;
}
}
if (expression.getRightExp() != null && expression.getRightExp().getType() == 0){
i = AddPara(i,expression.getRightExp());
if (i>para.length){
WrongOutput();
return i;
}
}
else{
expression.setRightExp(para[i]);
i++;
}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
return i;
}
return i;
}
}