updated code

This commit is contained in:
buzz-lightsnack-2007 2024-01-16 11:53:23 +08:00
parent 1037fad955
commit 2bc53c5352

View file

@ -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<Object> request_input() {
// Initialize variables.
ArrayList<Object> USER_INPUTS = new ArrayList<Object>();
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<Object> 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<Object> NUMBERS_INPUT = new ArrayList<Object>();
double NUMBER_RESULT;
while (true) {
NUMBERS_INPUT = request_input();
NUMBER_RESULT = calculate(NUMBERS_INPUT);
System.out.println(NUMBER_RESULT);
}
}
}