简体   繁体   中英

Simple Calculator Operation

I am attempting to simplify my long code of a calculator program, but I have a road block. I have a new else if statement for each calculator operator, but what I want to do is allow the user to manually type in, on one line, the entire operation they would like to perform and have the code compute it.

Here's 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 the output to be:

Enter operation:
4 * 6
4 * 6 = 24

Should be able to enter any operation here on one line. I am not asking you to write my calculator for me, I am asking how to allow the computer to read in the entire operation off one line and compute it, then print it.

If you use scanner readLine then you can read a whole line

eg

 4 * 6

This line can then be split to get three tokens

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

then you can see what operation to do based upon token[1]

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

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

You can use String.split and store it in an array. Then it will return an array of string, parse those back to integers. the do the operation you want. The x variable will be the result.

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

... And so on.

You could try parsing the line using a 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
}

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

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