可以仅使用队列将中缀表示法中的字符串转换为前缀表示法吗? (考虑唯一的操作是 + 和 - 的情况)

发布于 2024-12-11 14:20:09 字数 1278 浏览 0 评论 0原文

public class Convert{ 
/* the algorithm works as follows after inserting all elements of infix string 
into an empty Queue iterate over queue for infix.length() number of times and 
check if element at front of queue is an operator if yes enque the element to 
back of the queue else deque the operand and concatenate it with the empty 
prefix string here is an example then finally deque the queue elements with 
the prefix string*/                  

    public static String Pre(String in){
        int i = 0;
        Queue Q = new Queue(in.length());     //assuming a Queue of characters
        while(i<in.length()){
            Q.enque(in.charAt(i));       
        }
        i = in.length()-1;
        String pre = "";
        while(i>=0){                         //move operators to end of queue
            if(Q.peek()=='+'||'-'){           //if character is oper enque at end 
                Q.enque(Q.deque);}
            else{
                pre  = pre + Q.deque;
            }            // concatenate operands with prefix 
        }
        while(!Q.isEmpty) {                        //concatenate the operators 
            pre = Q.deque + pre;
        }
        return pre;                               // end of method
    }
}
public class Convert{ 
/* the algorithm works as follows after inserting all elements of infix string 
into an empty Queue iterate over queue for infix.length() number of times and 
check if element at front of queue is an operator if yes enque the element to 
back of the queue else deque the operand and concatenate it with the empty 
prefix string here is an example then finally deque the queue elements with 
the prefix string*/                  

    public static String Pre(String in){
        int i = 0;
        Queue Q = new Queue(in.length());     //assuming a Queue of characters
        while(i<in.length()){
            Q.enque(in.charAt(i));       
        }
        i = in.length()-1;
        String pre = "";
        while(i>=0){                         //move operators to end of queue
            if(Q.peek()=='+'||'-'){           //if character is oper enque at end 
                Q.enque(Q.deque);}
            else{
                pre  = pre + Q.deque;
            }            // concatenate operands with prefix 
        }
        while(!Q.isEmpty) {                        //concatenate the operators 
            pre = Q.deque + pre;
        }
        return pre;                               // end of method
    }
}

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

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

发布评论

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

评论(2

他夏了夏天 2024-12-18 14:20:09

帖子生产系统仅使用队列。它具有与图灵机相当的能力。

A "Post" Production System uses only queues. It has equivalent power to a Turing Machine.

朱染 2024-12-18 14:20:09

鉴于您指定的语法非常有限,对我来说,您似乎可以使用双端队列将中缀转换为前缀。如果语法更复杂,我怀疑这种方法是否有效。

Given the very limited grammar you specified, to me it appears that you could convert infix to prefix using a double-ended queue. Were the grammar more complex I doubt that this approach would work.

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