中缀到具有堆栈和队列的后缀转换.正在尝试转换(A+B 转换为 AB+).现在不用担心括号



我现在拥有的程序适用于所有运算符的 A+B 之类的东西。它也适用于 A+BxC 等示例,但是如果您输入 AxB+C,它会出错。我只是没有看到我哪里出错了。(我知道"x"不是 Java 的乘法运算符,但星号不会出现)。此外,我们还使用我们创建的堆栈和队列类。

为什么 A/B+C 不起作用?

提前谢谢。

public class PostfixEval {
public static void main(String[] args) throws IOException {
    File file = new File("infile.txt"); // infile contains single expression
    Scanner kb = new Scanner(file);
    Queue Q1 = new Queue();
    Queue Q2 = new Queue();
    Stack S1 = new Stack();
    while (kb.hasNext()) {
        String equation = kb.next();
        char ch;
        for (int i = 0; i < equation.length(); i++) {
            ch = equation.charAt(i);
            Q1.add2Rear(ch);
        }
        while (!Q1.ismtQ()) {
            ch = Q1.removeFront();
            if (Character.isLetter(ch)) {
                Q2.add2Rear(ch);
            } else if (isOperator(ch)) {
                if (!S1.ismt() && checkPrecedence(ch) <= checkPrecedence(S1.top())) {
                    Q2.add2Rear(S1.pop());
                    ;
                }
                S1.push(ch);
            }
        }
        while (!S1.ismt()) {
            Q2.add2Rear(S1.pop());
        }
    }
    while (!Q2.ismtQ()) {
        System.out.print(Q2.removeFront());
    }
    kb.close();
}
public static boolean isOperator(char ch) {
    boolean retval = false;
    if (ch == '+' || ch == '-' || ch == '/' || ch == '*')
        retval = true;
    return retval;
}
public static int checkPrecedence(char ch) {
    switch (ch) {
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
        return 2;
    }
    return -1;
}
}

静态类

public class Stack implements StackInterface {
ArrayList<Character> stack = new ArrayList<Character>();
public void push(char ch) {
    stack.add(ch);
}
public char top() {
    char myCh;
    myCh = stack.get(stack.size() - 1);
    return myCh;
}
public char pop() {
    char myCh;
    myCh = stack.get(stack.size() - 1);
    stack.remove(stack.get(stack.size() - 1));
    return myCh;
}
public boolean ismt() {
    boolean retval = true;
    retval = stack.isEmpty();
    return retval;
}
}

队列类

public class Queue implements QueueInterface {
ArrayList<Character> que = new ArrayList<Character>();
@Override
public void add2Rear(char ch) { // add element to rear of queue
    que.add(ch);
}
@Override
public char removeFront() { // returns first element AND removes it
    char retval = '1';
    retval = que.remove(0);
    return retval;
}
@Override
public char front() { // returns first element
    char retval = '1';
    retval = que.get(0);
    return retval;
}
@Override
public boolean ismtQ() { // true: if empty, false: if otherwise
    boolean retval = true;
    retval = que.isEmpty();
    return retval;
}
}

所以我是新来的,不确定我是否应该回答我自己的问题,但我想通了(就我的任务而言),也许其他人需要答案。

这是更新的代码:

if (!S1.ismt()) {
                    if(checkPrecedence(ch) <= checkPrecedence(S1.top())){ 
                        Q2.add2Rear(S1.pop());
                    }
                }

如果有人有更好的选择,请随时发布!

最新更新