计算器法

Calculator method

我正在尝试制作一个计算器并且想到了一个想法但不知道如何在 Java 中实现它。我正在尝试制作一个计算器,当用户输入时它可以工作,例如:

1+2-3/4 + 9 * 7= x

我应该得到的答案是63

问题

  1. 我没有学过以下方法,需要这些方面的帮助:

    从左边的字符串中的每个偶数字符或从右边的每个奇数字符收集运算符

    收集要计算的数字是从左起每个奇数或从右起每个偶数。

  2. 我目前制作了一个计算器程序,当您在 单独输入 中输入每个数字和运算符时,它就可以工作。但我需要做到这一点,以便用户在 一个输入 中输入方程,而不是在 单独输入 中输入方程。

这是我当前的程序:

    import java.io.*;
    public class Original_Calc
    {
    public static void main()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        double no,k=0,result;
        System.out.println("Enter number");
        int no1=Integer.parseInt(br.readLine());
        System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide)");
        String operand=br.readLine();
        String check="=";
        switch (operand)
        {
            case "+":
                    result=no1;
                    System.out.println("Enter number");
                    no=Integer.parseInt(br.readLine());
                    result+=no;
                    System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                    operand=br.readLine();
                    k=check.compareTo(operand);
                    for(;;)
                    {
                        if(k!=0)
                        {
                            switch (operand)
                            {
                                case "+": 

                                        System.out.println("Enter number");
                                        no=Integer.parseInt(br.readLine());
                                        result+=no;
                                        System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                        operand=br.readLine();
                                        k=check.compareTo(operand);
                                        break;
                                case "-":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result-=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                case "*":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result*=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                case "/":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result/=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                default:
                                        System.out.println("Please follow the instructions carefully");
                            }   
                        }
                        else
                        {
                            System.out.println("The result is "+result);
                            break;
                        }
                    }
                    break;
            case "-":
                    result=no1;
                    System.out.println("Enter number");
                    no=Integer.parseInt(br.readLine());
                    result-=no;
                    System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                    operand=br.readLine();
                    k=check.compareTo(operand);
                    for(;;)
                    {
                        if(k!=0)
                        {
                            switch (operand)
                            {
                                case "+": 

                                        System.out.println("Enter number");
                                        no=Integer.parseInt(br.readLine());
                                        result+=no;
                                        System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                        operand=br.readLine();
                                        k=check.compareTo(operand);
                                        break;
                                case "-":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result-=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                case "*":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result*=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                case "/":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result/=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                default:
                                        System.out.println("Please follow the instructions carefully");
                            }   
                        }
                        else
                        {
                            System.out.println("The result is "+result);
                            break;
                        }
                    }
                    break;
            case "*":
                    result=no1;
                    System.out.println("Enter number");
                    no=Integer.parseInt(br.readLine());
                    result*=no;
                    System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                    operand=br.readLine();
                    k=check.compareTo(operand);
                    for(;;)
                    {
                        if(k!=0)
                        {
                            switch (operand)
                            {
                                case "+": 

                                        System.out.println("Enter number");
                                        no=Integer.parseInt(br.readLine());
                                        result+=no;
                                        System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                        operand=br.readLine();
                                        k=check.compareTo(operand);
                                        break;
                                case "-":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result-=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                case "*":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result*=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                case "/":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result/=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                default:
                                        System.out.println("Please follow the instructions carefully");
                            }   
                        }
                        else
                        {
                            System.out.println("The result is "+result);
                            break;
                        }
                    }
                    break;
            case "/":
                    result=no1;
                    System.out.println("Enter number");
                    no=Integer.parseInt(br.readLine());
                    result/=no;
                    System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                    operand=br.readLine();
                    k=check.compareTo(operand);
                    for(;;)
                    {
                        if(k!=0)
                        {
                            switch (operand)
                            {
                                case "+": 

                                        System.out.println("Enter number");
                                        no=Integer.parseInt(br.readLine());
                                        result+=no;
                                        System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                        operand=br.readLine();
                                        k=check.compareTo(operand);
                                        break;
                                case "-":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result-=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                case "*":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result*=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                case "/":

                                         System.out.println("Enter number");
                                         no=Integer.parseInt(br.readLine());
                                         result/=no;
                                         System.out.println("Enter Operator (+ for add, - for subtract, * for multiply, / for divide or = for result)");
                                         operand=br.readLine();
                                         k=check.compareTo(operand);
                                         break;
                                default:
                                        System.out.println("Please follow the instructions carefully");
                            }   
                        }
                        else
                        {
                            System.out.println("The result is "+result);
                            break;
                        }
                    }
                    break;
            default:
                    System.out.println("Please follow the instructions carefully");
        }
    }
}

听说过 postfix expression or prefix expression 吗?要正确评估答案,您必须将表达式转换为 postfix/prefix 并对其进行评估。就像仅仅看到中缀一样,由于运算符的优先顺序,您可能无法正确评估表达式。您可以轻松地将任何数学表达式转换为 postfix/prefix,然后编写用于评估 posfix/prefix 表达式的代码 :)

编辑: 添加教程链接。

将中缀转换为后缀教程:https://www.youtube.com/watch?v=vq-nUF0G4fI

正在计算 post/prefix 表达式:https://www.youtube.com/watch?v=MeRb_1bddWg

关于你的问题,我认为最好的方法是编写一个算法,使用 Dijkstra 的调车场算法将你从用户那里获得的输入(中缀表示法)转换为类似逆波兰表示法的东西。这将允许您考虑运算符优先级。

这是解释它的视频,我认为它有一个 C++ 的后续视频,但是将其转换为 Java 将是微不足道的。

https://www.youtube.com/watch?v=QzVVjboyb0s

这是 Wiki 页面:

http://en.wikipedia.org/wiki/Shunting-yard_algorithm