Java:在计算器上切换二进制和十进制“模式”

时间:2022-09-24 20:48:41

I've essentially created 2 separate convoluted calculators, one that calculates decimal numbers and another that calculates binary numbers.

我基本上创建了2个独立的复杂计算器,一个计算十进制数,另一个计算二进制数。

The user inputs the equation they want calculated on the command line as so and the result is printed.

用户输入他们想要在命令行上计算的等式,并打印结果。

>1+1
>2

or for binary

或二进制

>0b111 + 0b101
>0b1100

What I'm having trouble with is being able to switch between calculating binary and decimal numbers on the fly. I'd ultimately like to make it work like this: (by default it would be in decimal mode)

我遇到的问题是能够在动态计算二进制和十进制数之间切换。我最终想让它像这样工作:(默认情况下它将是十进制模式)

>1+1
>2
>bin
>0b111 + 0b101 = 0b1100
>dec
>5 * 10
>50
>exit

(while typing exit at any point will exit the program)

(在任何时候输入exit都会退出程序)

Is there a way that I can pull this off?

有没有办法可以解决这个问题?

//Decimal Calculator
import java.util.*;
public class DecArithmetic
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        while(true)
        {
            String equation = input.nextLine();
            if (equation.equalsIgnoreCase("exit"))
            {
                System.exit(0);
            }
            equation = equation.replaceAll("\\s+","");
            int opLocation = equation.indexOf("+");
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("-");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("*");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("/");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("|");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("&");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("^");
            }
            String number = equation.substring(0,opLocation);
            int operandOne = Integer.parseInt(number);

            number = equation.substring(opLocation + 1);
            int operandTwo = Integer.parseInt(number);

            String op = equation.substring(opLocation, opLocation+1);

            int result = calculate(op, operandOne, operandTwo);
            System.out.println(result);
        }
    }
    public static int calculate(String operator, int operandOne, int operandTwo)
    {
        if(operator.equals("+") == true)
        {
            return operandOne + operandTwo;
        }
        else if(operator.equals("-") == true)
        {
            return operandOne - operandTwo;
        }
        else if(operator.equals("*") == true)
        {
            return operandOne * operandTwo;
        }
        else if(operator.equals("/") == true)
        {
            return operandOne / operandTwo;
        }
        else if(operator.equals("^") == true)
        {
            return operandOne ^ operandTwo;
        }
        else if (operator.equals("|") == true)
        {
            return operandOne | operandTwo;
        }
        else if(operator.equals("&") == true)
        {
            return operandOne & operandTwo;
        }
        else
        {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter a valid operator. (+, -, *, /, ^, |, &)");
            return calculate(scan.nextLine(), operandOne, operandTwo);
        }
    }
}

//Binary Calculator
import java.util.*;
public class BinaryArithmetic
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        while(true)
        {
            String equation = input.nextLine();
            if (equation.equalsIgnoreCase("exit"))
            {
                System.exit(0);
            }
            equation = equation.replaceAll("\\s+","");
            equation = equation.replaceAll("0b","");
            int opLocation = equation.indexOf("+");
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("-");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("*");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("/");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("|");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("&");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("^");
            }
            String number = equation.substring(0,opLocation);
            int bin1 = Integer.parseInt(number);
            String b1 = Integer.toString(bin1);
            int operandOne = Integer.parseInt(b1, 2);

            number = equation.substring(opLocation + 1);
            int bin2 = Integer.parseInt(number);
            String b2 = Integer.toString(bin2);
            int operandTwo = Integer.parseInt(b2, 2);

            String op = equation.substring(opLocation, opLocation+1);
            String result = Integer.toBinaryString(calculate(op, operandOne, operandTwo));
            System.out.println("0b"+result);

        }
    }
    public static int calculate(String operator, int operandOne, int operandTwo)
    {
        if(operator.equals("+") == true)
        {
            return operandOne + operandTwo;
        }
        else if(operator.equals("-") == true)
        {
            return operandOne - operandTwo;
        }
        else if(operator.equals("*") == true)
        {
            return operandOne * operandTwo;
        }
        else if(operator.equals("/") == true)
        {
            return operandOne / operandTwo;
        }
        else if(operator.equals("^") == true)
        {
            return operandOne ^ operandTwo;
        }
        else if (operator.equals("|") == true)
        {
            return operandOne | operandTwo;
        }
        else if(operator.equals("&") == true)
        {
            return operandOne & operandTwo;
        }
        else
        {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter a valid operator. (+, -, *, /, ^, |, &)");
            return calculate(scan.nextLine(), operandOne, operandTwo);
        }
    }
}

1 个解决方案

#1


1  

have a main class called Calculator which will take input from user. So this class will have your main method

有一个名为Calculator的主类,它将从用户那里获取输入。所以这个课将有你的主要方法

if the input is dec, then read the nextLine and pass to Decimal Calculator

如果输入为dec,则读取nextLine并传递给Decimal Calculator

if the input is bin, then read the nextLine and pass to Binary Calculator

如果输入是bin,则读取nextLine并传递给Binary Calculator

if the input is exit, you exit.

如果输入是退出,则退出。

if the input is neither dec, nor bin nor exit, then you assume that the user has entered a decimal equation and call the Decimal calculator.

如果输入既不是dec,也不是bin也不是exit,那么你假设用户输入了一个十进制方程并调用了Decimal计算器。

try to write some code as per i have suggested. if you struggle, i can help you out.

尝试按照我的建议编写一些代码。如果你挣扎,我可以帮助你。

#1


1  

have a main class called Calculator which will take input from user. So this class will have your main method

有一个名为Calculator的主类,它将从用户那里获取输入。所以这个课将有你的主要方法

if the input is dec, then read the nextLine and pass to Decimal Calculator

如果输入为dec,则读取nextLine并传递给Decimal Calculator

if the input is bin, then read the nextLine and pass to Binary Calculator

如果输入是bin,则读取nextLine并传递给Binary Calculator

if the input is exit, you exit.

如果输入是退出,则退出。

if the input is neither dec, nor bin nor exit, then you assume that the user has entered a decimal equation and call the Decimal calculator.

如果输入既不是dec,也不是bin也不是exit,那么你假设用户输入了一个十进制方程并调用了Decimal计算器。

try to write some code as per i have suggested. if you struggle, i can help you out.

尝试按照我的建议编写一些代码。如果你挣扎,我可以帮助你。