gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:42:15 25 4
gpt4 key购买 nike

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
}
}

最佳答案

一个“PostProduction System仅使用队列。它具有与图灵机相当的能力。

关于java - 可以仅使用队列将中缀表示法中的字符串转换为前缀表示法吗? (考虑唯一的操作是 + 和 - 的情况),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7867256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com