2016-11-24 51 views
0
public static int readConvert(char[]post)throws IOException{ 
    Stack S = new Stack(); 
    int j=0; 
    char c; 
    System.out.printf("Type an infix expression and press Enter\n"); 
    char token =getToken(); 
    while (token!='\0'){ 
     if (Character.isDigit(token))post [j++]=token; 
     else if (token =='(')S.push(new NodeData('(')); 
     else if (token ==')') 
      while ((c=S.pop().getData())!='(')post[j++]=c; 
     else { 
      while (!S.empty() && precedence(S.peek().getData())>= precedence(token)) 
      post[j++]=S.pop().getData(); 
      S.push(new NodeData(token)); 
     } 
     token=getToken(); 
    } 
    while (!S.empty())post[j++]=S.pop().getData(); 
    return j; 

} 

問題:(1)那爲什麼日食是告訴我,我的getData()是未定義的類型對象。未定義的類型對象綴與postfix

類我創建:

public class Node{ 
    public NodeData data; 
    NodeData next; 
    public Node(Nodedata d){ 
     data = d; 
     next = null; 
    } 
    public class NodeData{ 
    char ch; 

    public NodeData(char c){ 
     ch =c; 
    } 
    public char getData() {return ch;} 

    public int compareTo(NodeData nd){ 
     if (this.ch ==nd.ch)return 0; 
     if (this.ch<nd.ch) return -1; 
     return 1; 
    } 
    public String toString(){ 
     return ch + " "; //" " needed to convert num to a string 
    } 
    } 
    } 

問題:(2)爲什麼,當我嘗試編譯日食表明NODEDATA不能成爲解決的類型。

回答

0
  1. pop方法返回Stack使用的類型,所以儘量

堆棧<NODEDATA>堆棧=新堆棧<NODEDATA>();

  1. 拼寫?

邊注:也許很好看naming convention