代码中的堆栈跟踪

发布于 2024-11-02 03:15:10 字数 3568 浏览 0 评论 0原文

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

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("------------------------------------------");
}
}

I was wondering how I would add a trace to the stack of this code that converts infix to postix for expressions.

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("------------------------------------------");
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

oО清风挽发oО 2024-11-09 03:15:10

我非常想在整个转换过程中跟踪堆栈的内容

对此没有火箭科学的解决方案。

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


(郑重声明,术语“堆栈跟踪”通常表示程序调用堆栈的跟踪,而不是某些特定于应用程序的堆栈数据结构所发生情况的跟踪。您可能需要更仔细地选择术语下次。)

I am pretty much wanting to follow the contents of the stack throughout the conversion

There's no rocket science solution to this.

Just add System.err.println(...) calls at the relevant places. Or if you were doing this in production code (heaven forbid!) you might use a Logger instead of System.err.


(For the record, the term "stack trace" normally means a trace of a program's call stack, not a trace of what happens to some application-specific stack data structure. You might want to choose your terminology a little more carefully next time.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文