update the function

This commit is contained in:
buzz-lightsnack-2007 2024-01-16 12:21:50 +08:00
parent 6f16f730b2
commit 3697c483d3

View file

@ -44,7 +44,6 @@ class RPN_Calculator {
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()) {
@ -71,28 +70,42 @@ class RPN_Calculator {
static double calculate(ArrayList<Object> ENTRIES_LIST) {
// Calculate length.
int ENTRIES_LIST_LENGTH = ENTRIES_LIST.size();
int OPERATION_ITERATIONS = 0;
String OPERATION = "";
double OPERATION_RESULT = (double) ENTRIES_LIST.get(0);
double OPERATION_RESULT = 0;
ArrayList<Double> NUMBERS_CURRENT = new ArrayList<Double>();
// TO DO: fix entries search
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;
OPERATION_RESULT = (OPERATION_ITERATIONS>0) ? OPERATION_RESULT : NUMBERS_CURRENT.get(0);
for (int ENTRY_CURRENT_NUMBERS = 1; ENTRY_CURRENT_NUMBERS < NUMBERS_CURRENT.size(); ENTRY_CURRENT_NUMBERS++) {
if (OPERATION == "*" || OPERATION == "×") {
OPERATION_RESULT *= NUMBERS_CURRENT.get(ENTRY_CURRENT_NUMBERS);
} else if (OPERATION == "/" || OPERATION == "÷") {
if (NUMBERS_CURRENT.get(ENTRY_CURRENT_NUMBERS) != 0) {
OPERATION_RESULT /= (float) ENTRIES_LIST.get(ENTRY_CURRENT - 1);
} else {
handle_error(-3);
break;
};
} else if (OPERATION == "+") {
OPERATION_RESULT += NUMBERS_CURRENT.get(ENTRY_CURRENT_NUMBERS);
} else if (OPERATION == "-") {
OPERATION_RESULT -= NUMBERS_CURRENT.get(ENTRY_CURRENT_NUMBERS);
};
OPERATION_ITERATIONS++;
};
NUMBERS_CURRENT.clear();
} else {
NUMBERS_CURRENT.add((double) ENTRIES_LIST.get(ENTRY_CURRENT));
};
};
};
@ -110,5 +123,5 @@ class RPN_Calculator {
NUMBER_RESULT = calculate(NUMBERS_INPUT);
System.out.println(NUMBER_RESULT);
}
}
}
};
};