Java 中缀到后缀的负数表示法?代码

Java 中缀到后缀的负数表示法?代码,java,Java,大家好,我正在尝试为一个大学项目制作一个带有中缀到后缀符号的计算器,现在我遇到的问题是,当我尝试引入负数时,老实说,我非常紧张,我真的不理解我看到的几个帖子,我一个都不懂,我至少需要一部分代码来理解,这是我的密码: import java.util.Stack; public class InfixPostfixEvaluator { public static void main(String[] args) { InfixPostfixEvaluator in

大家好,我正在尝试为一个大学项目制作一个带有中缀到后缀符号的计算器,现在我遇到的问题是,当我尝试引入负数时,老实说,我非常紧张,我真的不理解我看到的几个帖子,我一个都不懂,我至少需要一部分代码来理解,这是我的密码:

import java.util.Stack;
public class InfixPostfixEvaluator
{
    public static void main(String[] args)
    {
        InfixPostfixEvaluator infix = new InfixPostfixEvaluator();
        String a = "(342 / 2)";
        String cad = infix.postfixConversion(a);
        System.out.println("Postfix notation: " + cad);
        System.out.println("Result: " + infix.evaluate(cad));
    }
    public String postfixConversion(String input) 
    {
        String postfix = "";
        Stack<Character> stack = new Stack<Character>();
        for (int i = 0; i < input.length(); i++) 
        {
            while (input.charAt(i) == ' ')
                ++i;
            if (Character.isDigit(input.charAt(i))) 
            { 
                postfix += input.charAt(i);
                if (i + 1 < input.length() && !Character.isDigit(input.charAt(i + 1)))
                    postfix += ' ';
            }
            else if (getPrecedence(input.charAt(i)) != 0)
            {
                while ((!stack.isEmpty()) && (getPrecedence(stack.peek()) >= getPrecedence(input.charAt(i))) && (stack.peek() != '(')) 
                {
                    postfix += stack.pop();
                    postfix += ' ';
                }
                stack.push(input.charAt(i));
            }
            else if (input.charAt(i) == '(') 
                stack.push(input.charAt(i));
            else if (input.charAt(i) == ')') 
            {
                while (!stack.isEmpty() && stack.peek() != '(')
                {
                    postfix += stack.peek();
                    stack.pop();
                }
                stack.pop();
            }
        }
        while (!stack.isEmpty()) 
        {
            postfix += stack.pop();
            postfix += ' ';
        }
        return postfix;
    }

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

    public Double evaluate(String postfix) 
    {
        Stack<Double> s = new Stack<Double>();
        char[] chars = postfix.toCharArray();
        for (int i = 0; i < chars.length; i++) 
        {
            char ch = chars[i];
            if (isOperatorPost(ch)) 
            {
                switch (ch) 
                {
                    case '+':
                        s.push(s.pop() + s.pop());
                        break;
                    case '*':
                        s.push(s.pop() * s.pop());
                        break;
                    case '-':
                        s.push(-s.pop() + s.pop());
                        break;
                    case '/':
                        s.push(1 / s.pop() * s.pop()); 
                        break;
                }
            } 
            else if(Character.isDigit(ch)) 
            {
                s.push(0.0);
                while (Character.isDigit(chars[i]))
                    s.push(10.0 * s.pop() + (chars[i++] - '0'));
            }
        }
        if (!s.isEmpty()) 
            return s.pop();
        else
            return 0.0;
    }
    private boolean isOperatorPost(char ch) 
    {
        return ch == '*' || ch == '/' || ch == '+' || ch == '-';
    }
}
输出

Postfix notation: 342 2 /-
当然,在我调用求值的行上进行注释,因为如果我调用它,它会给我一个错误


无论我是否必须更改为前缀符号或RPN或任何其他都无关紧要,但如果我必须这样做,我需要代码,这是最重要的,如果他们试图解释我,这是很好的,但事实是我不知道我是否能理解他们,但是如果他们告诉我用这个替换这个等等。我将更好地理解一千次道歉,感谢所有人,对于带+或-符号的提取操作数,您可以参考以下代码:

import java.util.regex.Matcher;  
import java.util.regex.Pattern; 
public class MyClass {
    public static void main(String args[]) {
        String expression = "(-342 / 2)";
        String realNoPattern="\\d+(\\.\\d+)?(e{1}[+-]?\\d+)?"; //real No. pattern string
        String patternString="[+-]{0,1}"+realNoPattern; //real no. with + or - sign
        
        Pattern  p=Pattern.compile(patternString);   
        Matcher m=p.matcher(expression);  
        while (m.find()){
            System.out.println(" Start : "+m.start()+", End : "+m.end()+" Pattern group : "+m.group());  
        }
    }
}

因为您可以使用m.start和m.end来提取运算符以进行进一步处理。

我首先使用正则表达式提取操作数。@我已经尝试过的KNVB研究了如何使用正则表达式来解决它,但它没有给出您应该先使用正则表达式提取带+或-号的操作数。然后问题就解决了。
import java.util.regex.Matcher;  
import java.util.regex.Pattern; 
public class MyClass {
    public static void main(String args[]) {
        String expression = "(-342 / 2)";
        String realNoPattern="\\d+(\\.\\d+)?(e{1}[+-]?\\d+)?"; //real No. pattern string
        String patternString="[+-]{0,1}"+realNoPattern; //real no. with + or - sign
        
        Pattern  p=Pattern.compile(patternString);   
        Matcher m=p.matcher(expression);  
        while (m.find()){
            System.out.println(" Start : "+m.start()+", End : "+m.end()+" Pattern group : "+m.group());  
        }
    }
}