暑假在家闲得要命,写了个简单的算术式解析器,大家来批评一下吧
class Node <E> {
//o:object
public E o;
//p:priority
public int p;
//l:left,r:right
public Node l,r;
public Node(E o){
this.o = o;
}
}
class Stack <E> {
private Node cur;
private int size = 0;
public void push(E o){
Node <E> n = new Node <E> (o);
if(cur!=null){
cur.r = n;
n.l = cur;
}
cur = n;
size++;
}
public E pop(){
if(size==0)
return null;
try{
size--;
return (E)cur.o;
}finally{
cur = cur.l;
if(cur!=null)
cur.r = null;
}
}
public int size(){
return size;
}
}
class Tree{
private Node <String> cur,root,start;
private Stack <Node> nodes;
private String s[],result;
public void insert(String s[]){
if(s.length <=2)
return;
int x = Parser.X_INIT;
nodes = new Stack <Node> ();
//create a root in order to get a start
//and solve the condition of starting with "( "
cur = new Node <String> (Parser.operators[0]);
cur.l = new Node <String> ( "0 ");
cur.p = Parser.p(Parser.operators[0])+x;
root = cur;
start = root;
for(int i=0;i <s.length;i++){
//a new node
Node <String> n = new Node <String> (s[i]);