使用 java 在 infix 到 postfix 应用程序中得到错误的输出
我最近编写了一个java程序,它采用中缀表达式并将其转换为后缀表达式。它在大多数情况下都有效,但我得到了某些表达式的错误输出。例如表达式 a+b+c+d+e 在应该输出的时候会输出 abcde+++++ ab+c+d+e+。
import java.util.Stack;
public class ITP {
public static Stack<Character> stack;
public static String inFixExp;
public static String postFixExp = "";
public static String infixToPostfix(String exp){
ITP o = new ITP();
stack = new Stack<Character>();
inFixExp = exp;
for (int i = 0; i < inFixExp.length(); i++) {
if (inFixExp.charAt(i) == '(')
stack.push(inFixExp.charAt(i));
else if (inFixExp.charAt(i)==')'){
while (stack.peek()!='('){
postFixExp += stack.pop();
}
stack.pop();
}else if ((inFixExp.charAt(i)=='*')||(inFixExp.charAt(i)=='/')||(inFixExp.charAt(i)=='+')||(inFixExp.charAt(i)=='-')){
while(!stack.isEmpty() && o.getPredence(inFixExp.charAt(i)) < o.getPredence(stack.peek()))
postFixExp += stack.pop();
stack.push(inFixExp.charAt(i));
}else
postFixExp += inFixExp.charAt(i);
}
while(!stack.isEmpty())
postFixExp += stack.pop();
return postFixExp;
}
public int getPredence(Object op) {
if((op.equals("*")) || (op.equals("/")))
return 3;
else if((op.equals("+"))||(op.equals("-")))
return 1;
else
return 0;
}
}
我发现如果我改变 <在第 24 行使用 <= 它将解决这个问题,但随后我会得到一个空堆栈错误,并且其他一些表达式将输出不正确,例如 a+b*c 它将输出 ab+c*,而它应该是abc*+.
i recently wrote a java program that takes an infix expression and converts it into a postfix expression. It works for the most part but i am getting wrong outputs for some expressions. For example the expression a+b+c+d+e will output abcde+++++ when it should output
a b + c + d + e +.
import java.util.Stack;
public class ITP {
public static Stack<Character> stack;
public static String inFixExp;
public static String postFixExp = "";
public static String infixToPostfix(String exp){
ITP o = new ITP();
stack = new Stack<Character>();
inFixExp = exp;
for (int i = 0; i < inFixExp.length(); i++) {
if (inFixExp.charAt(i) == '(')
stack.push(inFixExp.charAt(i));
else if (inFixExp.charAt(i)==')'){
while (stack.peek()!='('){
postFixExp += stack.pop();
}
stack.pop();
}else if ((inFixExp.charAt(i)=='*')||(inFixExp.charAt(i)=='/')||(inFixExp.charAt(i)=='+')||(inFixExp.charAt(i)=='-')){
while(!stack.isEmpty() && o.getPredence(inFixExp.charAt(i)) < o.getPredence(stack.peek()))
postFixExp += stack.pop();
stack.push(inFixExp.charAt(i));
}else
postFixExp += inFixExp.charAt(i);
}
while(!stack.isEmpty())
postFixExp += stack.pop();
return postFixExp;
}
public int getPredence(Object op) {
if((op.equals("*")) || (op.equals("/")))
return 3;
else if((op.equals("+"))||(op.equals("-")))
return 1;
else
return 0;
}
}
I found out that if i change the < with <= in line 24 it will fix this problem but then i will get an empty stack error and some other expressions will output incorrectly, such as a+b*c which will output ab+c*, when it is supposed to be abc*+.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您
检查
charAt()
但您的getPredence(precedence?)
检查String
,尝试与char
进行比较反而。Your
checks
charAt()
but yourgetPredence(precedence?)
checks for aString
, try comparing against achar
instead.