2011-04-17 92 views
0

我想知道如何将跟踪添加到此代码的堆栈中,该代码将中缀转换为表达式的postix。代码中的堆栈跟踪

class Node { 
    public Object data; 
    public Node next; 
    public Node() { 
     data =' '; next = null; } 
    public Node (Object val) { 
     data = val; next = null; } 
} 

public class LinkStack { 
    private Node top; 
    public LinkStack() { 
     top = null; } 
    public boolean empty(){ 
     return top == null; } 


    public boolean full(){ 
     return false; 
    } 
public void push(Object e){ 
     Node tmp = new Node(e); 
     tmp.next = top; 
     top = tmp; 
    } 
public Object pop(){ 

    Object e = top.data; 
    top = top.next; 
    return e; 
} 
public Object peek(){ 

    Object e = top.data; 

    return e; 
} 


public void matching(String x) 
{ 
    LinkStack S=new LinkStack(); 

    for(int i=0;i<x.length();i++) 
    { 
     char c=x.charAt(i); 
     if(c=='(') 
      S.push(c); 
     else 
     { 
      if(c==')') 
      if(S.empty()) 
       System.out.println("NOT MATCHING !!!"); 
      else 
       S.pop(); 
     } 
    } 
    if(!S.empty()) 
     System.out.println("NOT MATCHING !!!"); 
    else 
     System.out.println("MATCHING !!!"); 
} 
public void Evaluation(String x) 
{ 

    LinkStack S=new LinkStack(); 
    for(int i=0;i<x.length();i++) 
    { 
     char c=x.charAt(i); 
     String s="0"+c; 

     if(c=='+') 
     { 
      int z=Integer.parseInt((String)S.pop())+Integer.parseInt((String)S.pop()); 
      S.push(Integer.toString(z)); 
     } 
     else if(c=='*') 
     { 
      int z=Integer.parseInt((String)S.pop())*Integer.parseInt((String)S.pop()); 
      S.push(Integer.toString(z)); 

     } 
     else if(c=='/') 
     { int u=Integer.parseInt((String)S.pop()); 

      int z=Integer.parseInt((String)S.pop())/u; 
      S.push(Integer.toString(z)); 

     } 
     else if(c=='-') 
     { int u=Integer.parseInt((String)S.pop()); 
      int z=Integer.parseInt((String)S.pop())-u; 
      S.push(Integer.toString(z)); 
     } 
     else 
     S.push(s); 
    } 
    System.out.println("THE POSTFIX = "+x); 
    System.out.println("THE RESULT = "+S.pop()); 
} 
public void postfix(String x) 
{ 
    String output=""; 
    LinkStack S=new LinkStack(); 
    for(int i=0;i<x.length();i++) 
    { 
     char c=x.charAt(i); 

     if(c==('+')||c==('*')||c==('-')||c==('/')) 
      {while(!S.empty() && priority(S.peek())>= priority(c)) 
       output+=S.pop(); 
      S.push(c); 
      System.out.println(output); 
      } 
     else if(c=='(') 
     { 
      S.push(c); 
     } 
     else if(c==')') 
     { 
      while(!S.peek().equals('(')) 
        output+=S.pop(); 
      S.pop(); 
      System.out.println(output); 
     } 
     else 
     { 
      output+=c; 
      System.out.println(output); 
     } 
    } 
    while(!S.empty()) 
     output+=S.pop(); 
    System.out.println("THE INFIX = "+x); 
    System.out.println("THE POSTFIX = "+output); 
} 
public int priority(Object x) 
{ 
    if(x.equals('+')||x.equals('-')) 
     return 1; 
    else if(x.equals('*')||x.equals('/')) 
     return 2; 
    else 
     return 0; 
} 

public static void main(String args[]) 
{ 


    LinkStack s=new LinkStack(); 
    s.postfix("x*y–z+(a–c/d)"); 
    System.out.println("------------------------------------------"); 
    s.matching("x*y–z+(a–c/d)"); 
    System.out.println("------------------------------------------"); 
} 
} 
+2

这是什么语言?看起来像C#或Java。无论如何,**不要使用公共字段**。 – 2011-04-17 23:19:15

+0

创建一个新的异常,不扔它,只是printStackTrace ...如果这是你想要的。 – govi 2011-04-17 23:58:29

+0

我非常想在整个转换过程中遵循堆栈的内容 – 2011-04-18 00:32:02

回答

0

我非常想跟随栈的内容在整个转换

有没有火箭科学地解决了这个。

只需在相关位置添加System.err.println(...)电话即可。或者,如果您是在生产代码(天堂禁止!)中执行此操作,则可以使用Logger而不是System.err


(根据记录,术语“堆栈跟踪”通常是指一个程序调用堆栈,而不是发生了什么应用程序特定的堆栈数据结构的一丝一丝的,你可能想选择你的术语下次更仔细一点。)