diff --git a/src/xs_ibdpcompsci_javaintro/calculator.java b/src/xs_ibdpcompsci_javaintro/calculator.java new file mode 100644 index 0000000..e55e653 --- /dev/null +++ b/src/xs_ibdpcompsci_javaintro/calculator.java @@ -0,0 +1,114 @@ +package xs_ibdpcompsci_javaintro; +import java.util.*; + +class RPN_Calculator { + static void handle_error(int ERROR_TYPE) { + String ERROR_MESSAGE = ""; + + switch (ERROR_TYPE) { + case -1: + // Delete the previous line to allow users to edit it. + System.out.println("\033[A"); + break; + case -2: + ERROR_MESSAGE = "Memory exceeded."; + break; + case -3: + // Division by zero. + ERROR_MESSAGE = "Division by zero"; + break; + default: + ERROR_MESSAGE = "Error in defining the error."; + break; + }; + System.out.println(ERROR_MESSAGE); + } + + + static ArrayList request_input() { + + // Initialize variables. + ArrayList USER_INPUTS = new ArrayList(); + String USER_INPUT_CURRENT = ""; + Boolean USER_INPUT_MORE = true; + int USER_INPUT_LENGTH = 0; + + // Loop for input. + do { + Scanner input = new Scanner(System.in); + + USER_INPUT_CURRENT = input.nextLine(); + USER_INPUT_LENGTH = USER_INPUTS.size(); + + if (USER_INPUT_CURRENT.matches("-?\\d+(\\.\\d+)?")) { + USER_INPUTS.add(Double.parseDouble(USER_INPUT_CURRENT)); + } else if (USER_INPUTS.size() >= 2) { + if (USER_INPUT_CURRENT.matches("[-+\\/*×÷]") && (String.valueOf(USER_INPUTS.get(USER_INPUT_LENGTH-1)).matches("-?\\d+(\\.\\d+)?"))) { + System.out.println(USER_INPUTS.get(USER_INPUT_LENGTH-1)); + USER_INPUTS.add(USER_INPUT_CURRENT); + } else if (USER_INPUT_CURRENT.isBlank()) { + if (String.valueOf(USER_INPUTS.get(USER_INPUT_LENGTH-1)).isBlank()) { + System.exit(0); + } else if (String.valueOf(USER_INPUTS.get(USER_INPUT_LENGTH-1)).matches("-?\\d+(\\.\\d+)?")) { + handle_error(-1); + } else { + USER_INPUT_MORE = false; + }; + } else { + handle_error(-1); + }; + } else { + // The user inputs a wrong code. Let the user edit it. + handle_error(-1); + }; + + } while (USER_INPUT_MORE == true); + + + return (USER_INPUTS); + } + + static double calculate(ArrayList ENTRIES_LIST) { + // Calculate length. + int ENTRIES_LIST_LENGTH = ENTRIES_LIST.size(); + String OPERATION = ""; + double OPERATION_RESULT = (double) ENTRIES_LIST.get(0); + + if (ENTRIES_LIST_LENGTH > 1) { + for (int ENTRY_CURRENT = 0; ENTRY_CURRENT < ENTRIES_LIST_LENGTH; ENTRY_CURRENT++) { + if (String.valueOf(ENTRIES_LIST.get(ENTRY_CURRENT)).matches("[-+\\/*×÷]")) { + OPERATION = String.valueOf(ENTRIES_LIST.get(ENTRY_CURRENT)); + + if (OPERATION == "+") { + OPERATION_RESULT += (float) ENTRIES_LIST.get(ENTRY_CURRENT - 1); + } else if (OPERATION == "-") { + OPERATION_RESULT -= (float) ENTRIES_LIST.get(ENTRY_CURRENT - 1); + } else if (OPERATION == "*" || OPERATION == "×") { + OPERATION_RESULT *= (float) ENTRIES_LIST.get(ENTRY_CURRENT - 1); + } else if (OPERATION == "/" || OPERATION == "÷") { + if ((double) ENTRIES_LIST.get(ENTRY_CURRENT - 1) != 0) { + OPERATION_RESULT /= (float) ENTRIES_LIST.get(ENTRY_CURRENT - 1); + } else { + handle_error(-3); + break; + }; + }; + }; + }; + }; + + return (OPERATION_RESULT); + }; + + + public static void main(String[] args) { + // Initialize the input numbers. + ArrayList NUMBERS_INPUT = new ArrayList(); + double NUMBER_RESULT; + while (true) { + NUMBERS_INPUT = request_input(); + NUMBER_RESULT = calculate(NUMBERS_INPUT); + System.out.println(NUMBER_RESULT); + } + } +} \ No newline at end of file