简体   繁体   中英

Java: switching between binary and decimal "modes" on a calculator

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

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)

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);
        }
    }
}

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

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

if the input is bin, then read the nextLine and pass to 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM