Simple calculator management

I am trying to simplify my long calculator code, but I have a road block. I have an else else if statement for each calculator operator, but I want the user to manually enter on the same line all the operation that they would like to perform and calculate its code.

Here is what I have:

do {
        System.out.println("What function would you like to perform?");
        System.out.print("Exit Calculator (Q), Add (+), Subtract (-), Multiply (x), Divide (/): ");
        maininput = in.next();

        if (maininput.equals("+")) {
            System.out.print("Enter the first number to add: ");
            num1 = in.nextDouble();
            System.out.print("Enter the second number to add: ");
            num2 = in.nextDouble();
            System.out.println();

            answer = num1 + num2;

            System.out.println(num1 + " + " + num2 + " = " + answer);
            System.out.println();
        }
        else if (maininput.equals("-")) {
            System.out.print("Enter the first number to subtract: ");
            num1 = in.nextDouble();
            System.out.print("Enter the second number to subtract: ");
            num2 = in.nextDouble();
            System.out.println();

            answer = num1 - num2;

            System.out.println(num1 + " - " + num2 + " = " + answer);
            System.out.println();
        }
        else if(maininput.equals("x")) {
            System.out.print("Enter the first number to multiply: ");
            num1 = in.nextDouble();
            System.out.print("Enter the second number to multiply: ");
            num2 = in.nextDouble();
            System.out.println();

            answer = num1 * num2;

            System.out.println(num1 + " x " + num2 + " = " + answer);
            System.out.println();
        }
        else if(maininput.equals("/")) {
            System.out.print("Enter the first number to divide: ");
            num1 = in.nextDouble();
            do {
                System.out.print("Enter the second number to divide: ");
                num2 = in.nextDouble();
                System.out.println();
                if (num2 == 0) {
                    System.out.println("Cannot divide by 0!  Please enter a different number.");
                }
            } while (num2 == 0);

            answer = num1 / num2;

            System.out.println(num1 + " / " + num2 + " = " + answer);
            System.out.println();
        }
        else if(maininput.equals("Q") || maininput.equals("q") || maininput.equals("EXIT") || maininput.equals("exit")) {
            in.close();
            System.exit(0);
        }
        else {
            System.out.println(maininput + " is not a valid operand.  Please try again.");
            System.out.println();
        }
    } while (maininput != "Q" && maininput != "q");

This is what I want to get:

Enter operation:
4 * 6
4 * 6 = 24

Must be able to enter any operation here on one line. I do not ask you to write me a calculator, I ask how to allow the computer to read the entire operation from one line and calculate it, and then print.

+4
source share
3 answers

If you use readLine scanner, you can read the whole line

eg.

 4 * 6

,

 String tokens [] = line.split (" ");

, [1]

 if (token[1].equals ("-") {

      //lets minus token[2] from token[0]
      // need to convert String to Number
 }
+3

String.split . , . , . x.

    if(maininput.contains("+")) {
        String[] stringarr = string.split("\\+");
        int x = Integer.parseInt(stringarr[0])  +  Integer.parseInt(stringarr[1]);
        System.out.println(stringarr[0] + " + " + stringarr[1] + " = " + x);
    } else if(maininput.contains("-")) {
        String[] stringarr = string.split("\\-");
        int x = Integer.parseInt(stringarr[0])  -  Integer.parseInt(stringarr[1]);
        System.out.println(stringarr[0] + " - " + stringarr[1] + " = " x);
    } 

... .

0

You can try parsing a string using the Pattern object, something like this:

Pattern opPattern = Pattern.compile("(\\d+) *([+-*/]) *(\\d+)");
Matcher matcher = opPattern.matcher(userLine);
if(matcher.find()) {
    int op1 = Integer.toValue(matcher.group(1));
    int op2 = Integer.toValue(matcher.group(3));
    String op = matcher.group(2);

    if(op.equals("+")) {
        // do + op ...
    } else ... {
        // etc...
    }

} else {
    // error in line, not the form of an operation
}

Take a look at javadoc, as I'm not sure that I used the correct method names and the like, just tried to illustrate the idea ...

0
source

Source: https://habr.com/ru/post/1531257/


All Articles