2

忍受我,我现在因为把这一切放在一起有点焦躁,但我正处于最后阶段。我在java中制作了一个计算器,它采用中缀方程,然后将其更改为后缀。它也需要变量!我做到了,所以我的后缀包含中缀的负数。这看起来像这样:

infix: 1+-2*(4/2)
postfix: 12_42/*+

所以很明显我让它'_'在后缀中的底片工作。酷是吗?好的,但是现在我得让我的计算器来读取它们,我只是在思考它在堆栈中的位置以及我如何使它工作而不为所有形式的操作数设置条件。这是我所拥有的:

import java.util.Stack;

/**
 *
 * @author rtibbetts268
 */
public class InfixToPostfix
{
        /**
         * Operators in reverse order of precedence.
         */
    private static final String operators = "-+/*_";
    private static final String operands = "0123456789x";

    /*public int evalInfix(String infix)
    {
        return evaluatePostfix(convert2Postfix(infix));
    }*/

    public String xToValue(String postfixExpr, String x)
    {
        char[] chars = postfixExpr.toCharArray();
        StringBuilder newPostfixExpr = new StringBuilder();

        for (char c : chars)
        {
            if (c == 'x')
            {
                newPostfixExpr.append(x);
            }
            else
            {
                newPostfixExpr.append(c);
            }
        }
        return newPostfixExpr.toString();
    }

    public String convert2Postfix(String infixExpr)
    {
        char[] chars = infixExpr.toCharArray();
        StringBuilder in = new StringBuilder(infixExpr.length());

        for (int i = 0; i<chars.length; i++)
        {
            if (infixExpr.charAt(i) == '-')
            {
                if (i == 0)
                {
                in.append('_');
                }
                else if(isOperand(infixExpr.charAt(i + 1)))
                {
                    if (i != infixExpr.length())
                    {
                    if (isOperator(infixExpr.charAt(i-1)))
                        in.append('_');
                    }
                    else
                    {
                        in.append(infixExpr.charAt(i));
                    }
                }
                else
                {
                   in.append(infixExpr.charAt(i));
                }
            }
            else
            {
                in.append(infixExpr.charAt(i));
            }
        }

        chars = in.toString().toCharArray();
        Stack<Character> stack = new Stack<Character>();
        StringBuilder out = new StringBuilder(in.toString().length());

        for (char c : chars)
        {
            if (isOperator(c))
            {
                while (!stack.isEmpty() && stack.peek() != '(')
                {
                    if (operatorGreaterOrEqual(stack.peek(), c))
                    {
                        out.append(stack.pop());
                    }
                    else
                    {
                        break;
                    }
                }
                stack.push(c);
            }
            else if (c == '(')
            {
                stack.push(c);
            }
            else if (c == ')')
            {
                while (!stack.isEmpty() && stack.peek() != '(')
                {
                    out.append(stack.pop());
                }
                if (!stack.isEmpty())
                {
                    stack.pop();
                }
            }
            else if (isOperand(c))
            {
                out.append(c);
            }
        }
        while (!stack.empty())
        {
            out.append(stack.pop());
        }
        return out.toString();
    }

    public int evaluatePostfix(String postfixExpr)//YBEYFCNUNKJKDV IT'S RIGHT HERE!!!
    {
        char[] chars = postfixExpr.toCharArray();
        Stack<Integer> stack = new Stack<Integer>();
        for (char c : chars)
        {
            if (isOperand(c))
            {
                stack.push(c - '0'); // convert char to int val
            }
            else if (isOperator(c))
            {
                int op1 = stack.pop();
                int op2 = stack.pop();
                int result;
                switch (c) {
                    case '_':
                        result = op1 * -1;
                        //stack.push(result);
                        //break;
                    case '*':
                        result = op1 * op2;
                        stack.push(result);
                        break;
                    case '/':
                        result = op2 / op1;
                        stack.push(result);
                        break;
                    case '+':
                        result = op1 + op2;
                        stack.push(result);
                        break;
                    case '-':
                        result = op2 - op1;
                        stack.push(result);
                        break;
                }
            }
        }
        return stack.pop();
    }

    private int getPrecedence(char operator)
    {
        int ret = 0;
        if (operator == '-' || operator == '+')
        {
            ret = 1;
        }
        else if (operator == '*' || operator == '/')
        {
            ret = 2;
        }
        if (operator == '_')
        {
            ret = 3;
        }
        return ret;
    }

    private boolean operatorGreaterOrEqual(char op1, char op2)
    {
        return getPrecedence(op1) >= getPrecedence(op2);
    }

    private boolean isOperator(char val)
    {
        return operators.indexOf(val) >= 0;
    }

    private boolean isOperand(char val)
    {
        return operands.indexOf(val) >= 0;
    }

}

我会少发帖,但是这一切都趋于一致,所以我会解释一下。仔细查看调用的方法evaluatePostfix()(从上数第三个)。

这是该类可以采用后缀表达式(例如我上面提到的那个)并计算它的地方。它计算一个包含所有正整数的表达式,但是当我尝试实现它以在它实际运行任何数学之前将一个数字更改为负数时,它会卡普特。

任何人都可以帮助我修复这个特定的方法,使其有效吗?我需要它来计算负整数以及正整数。

4

1 回答 1

2

基本上问题在于尝试从“_”案例的空堆栈中弹出。您需要做的只是尝试从堆栈中弹出 op2 以用于 '*/+-' 情况,而不是 '_' 情况。

public int evaluatePostfix(String postfixExpr)
{
    char[] chars = postfixExpr.toCharArray();
    Stack<Integer> stack = new Stack<Integer>();
    for (char c : chars)
    {
        if (isOperand(c))
        {
            stack.push(c - '0'); // convert char to int val
        }
        else if (isOperator(c))
        {
            int op1 = stack.pop();
            int op2;
            int result;
            switch (c) {
                case '_':
                    result = op1 * -1;
                    stack.push(result);
                    break;
                case '*':
                    op2 = stack.pop();
                    result = op1 * op2;
                    stack.push(result);
                    break;
                case '/':
                    op2 = stack.pop();
                    result = op2 / op1;
                    stack.push(result);
                    break;
                case '+':
                    op2 = stack.pop();
                    result = op1 + op2;
                    stack.push(result);
                    break;
                case '-':
                    op2 = stack.pop();
                    result = op2 - op1;
                    stack.push(result);
                    break;
            }
        }
    }
    return stack.pop();
}
于 2013-04-11T00:24:25.470 回答