add line clearing and converted array to stack
This commit is contained in:
parent
b19efc6595
commit
2ad7bcb227
1 changed files with 37 additions and 25 deletions
|
@ -3,13 +3,27 @@ import java.util.*;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
class RPN_Calculator {
|
class RPN_Calculator {
|
||||||
|
static void line_clear(int TIMES) {
|
||||||
|
TIMES = Math.abs(TIMES);
|
||||||
|
|
||||||
|
if (TIMES > 0) {
|
||||||
|
for (int CLEAR_INSTANCES = 0; CLEAR_INSTANCES < TIMES; CLEAR_INSTANCES++) {
|
||||||
|
System.out.print(String.format("\033[2K",2));
|
||||||
|
System.out.print(String.format("\033[%dA",2)); // Move up
|
||||||
|
System.out.print(String.format("\033[2K",2));
|
||||||
|
System.out.print("\n");
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void handle_error(int ERROR_TYPE) {
|
static void handle_error(int ERROR_TYPE) {
|
||||||
String ERROR_MESSAGE = "";
|
String ERROR_MESSAGE = "";
|
||||||
|
|
||||||
switch (ERROR_TYPE) {
|
switch (ERROR_TYPE) {
|
||||||
case -1:
|
case -1:
|
||||||
// Delete the previous line to allow users to edit it.
|
// Delete the previous line to allow users to edit it.
|
||||||
System.out.print("\r");
|
line_clear(1);
|
||||||
break;
|
break;
|
||||||
case -2:
|
case -2:
|
||||||
ERROR_MESSAGE = "Memory exceeded.";
|
ERROR_MESSAGE = "Memory exceeded.";
|
||||||
|
@ -61,10 +75,12 @@ class RPN_Calculator {
|
||||||
USER_INPUTS.add(USER_INPUT_CURRENT);
|
USER_INPUTS.add(USER_INPUT_CURRENT);
|
||||||
} else {
|
} else {
|
||||||
// Correct the mathematical operator.
|
// Correct the mathematical operator.
|
||||||
System.out.print("\r\r"+ "> "+ USER_INPUT_CURRENT);
|
line_clear(1);
|
||||||
USER_INPUTS.set(USER_INPUT_LENGTH - 1, USER_INPUT_LENGTH);
|
System.out.print("> "+ USER_INPUT_CURRENT);
|
||||||
|
USER_INPUTS.set(USER_INPUT_LENGTH - 1, USER_INPUT_CURRENT);
|
||||||
};
|
};
|
||||||
} else if (USER_INPUT_CURRENT.isBlank() && USER_INPUTS.get(USER_INPUT_LENGTH - 1).getClass() == String.class) {
|
} else if (USER_INPUT_CURRENT.isBlank() && USER_INPUTS.get(USER_INPUT_LENGTH - 1).getClass() == String.class) {
|
||||||
|
line_clear(1);
|
||||||
USER_INPUT_MORE = false;
|
USER_INPUT_MORE = false;
|
||||||
} else {
|
} else {
|
||||||
handle_error(-1);
|
handle_error(-1);
|
||||||
|
@ -75,7 +91,6 @@ class RPN_Calculator {
|
||||||
// The user inputs a wrong code. Let the user edit it.
|
// The user inputs a wrong code. Let the user edit it.
|
||||||
handle_error(-1);
|
handle_error(-1);
|
||||||
};
|
};
|
||||||
|
|
||||||
} while (USER_INPUT_MORE == true);
|
} while (USER_INPUT_MORE == true);
|
||||||
|
|
||||||
return (USER_INPUTS);
|
return (USER_INPUTS);
|
||||||
|
@ -88,49 +103,47 @@ class RPN_Calculator {
|
||||||
String OPERATION = "";
|
String OPERATION = "";
|
||||||
double OPERATION_RESULT = 0;
|
double OPERATION_RESULT = 0;
|
||||||
|
|
||||||
ArrayList<Double> NUMBERS_CURRENT = new ArrayList<Double>();
|
Queue<Double> NUMBERS_CURRENT = new ArrayDeque<Double>();
|
||||||
|
|
||||||
// TO DO: fix entries search
|
// TO DO: fix entries search
|
||||||
if (ENTRIES_LIST_LENGTH > 1) {
|
if (ENTRIES_LIST_LENGTH > 1) {
|
||||||
for (int ENTRY_CURRENT = 0; ENTRY_CURRENT < ENTRIES_LIST_LENGTH; ENTRY_CURRENT++) {
|
for (int ENTRY_CURRENT = 0; ENTRY_CURRENT < ENTRIES_LIST_LENGTH; ENTRY_CURRENT++) {
|
||||||
if (String.valueOf(ENTRIES_LIST.get(ENTRY_CURRENT)).matches("[-+\\/*×÷]")) {
|
if (String.valueOf(ENTRIES_LIST.get(ENTRY_CURRENT)).matches("[-+\\/*×÷]")) {
|
||||||
OPERATION = String.valueOf(ENTRIES_LIST.get(ENTRY_CURRENT));
|
OPERATION = String.valueOf(ENTRIES_LIST.get(ENTRY_CURRENT));
|
||||||
if (OPERATION_ITERATIONS <= 0) {OPERATION_RESULT = NUMBERS_CURRENT.get(0); NUMBERS_CURRENT.remove(0);};
|
if (OPERATION_ITERATIONS <= 0) {OPERATION_RESULT = NUMBERS_CURRENT.poll();};
|
||||||
|
|
||||||
switch (OPERATION) {
|
switch (OPERATION) {
|
||||||
case "*":
|
case "*":
|
||||||
case "×":
|
case "×":
|
||||||
for (Double NUMBER_CURRENT : NUMBERS_CURRENT) {
|
do {
|
||||||
OPERATION_RESULT *= NUMBER_CURRENT;
|
OPERATION_RESULT *= NUMBERS_CURRENT.poll();
|
||||||
};
|
} while (!NUMBERS_CURRENT.isEmpty());
|
||||||
break;
|
break;
|
||||||
case "/":
|
case "/":
|
||||||
case "÷":
|
case "÷":
|
||||||
for (Double NUMBER_CURRENT : NUMBERS_CURRENT) {
|
Double NUMBER_CURRENT = NUMBERS_CURRENT.poll();
|
||||||
if (NUMBER_CURRENT != 0) {
|
if (NUMBER_CURRENT != 0) {
|
||||||
OPERATION_RESULT /= NUMBER_CURRENT;
|
OPERATION_RESULT /= NUMBER_CURRENT;
|
||||||
} else {
|
} else {
|
||||||
handle_error(-3);
|
handle_error(-3);
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
};
|
|
||||||
break;
|
break;
|
||||||
case "+":
|
case "+":
|
||||||
for (Double NUMBER_CURRENT : NUMBERS_CURRENT) {
|
do {
|
||||||
OPERATION_RESULT += NUMBER_CURRENT;
|
OPERATION_RESULT += NUMBERS_CURRENT.poll();
|
||||||
};
|
} while (!NUMBERS_CURRENT.isEmpty());
|
||||||
break;
|
break;
|
||||||
case "-":
|
case "-":
|
||||||
for (Double NUMBER_CURRENT : NUMBERS_CURRENT) {
|
do {
|
||||||
OPERATION_RESULT -= NUMBER_CURRENT;
|
OPERATION_RESULT -= NUMBERS_CURRENT.poll();
|
||||||
};
|
} while (!NUMBERS_CURRENT.isEmpty());
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
|
|
||||||
OPERATION_ITERATIONS++;
|
OPERATION_ITERATIONS++;
|
||||||
NUMBERS_CURRENT.clear();
|
|
||||||
} else {
|
} else {
|
||||||
NUMBERS_CURRENT.add((double) ENTRIES_LIST.get(ENTRY_CURRENT));
|
NUMBERS_CURRENT.add((double) ENTRIES_LIST.get(ENTRY_CURRENT));
|
||||||
};
|
};
|
||||||
|
@ -140,7 +153,6 @@ class RPN_Calculator {
|
||||||
return (OPERATION_RESULT);
|
return (OPERATION_RESULT);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Initialize the input numbers.
|
// Initialize the input numbers.
|
||||||
ArrayList<Object> NUMBERS_INPUT = new ArrayList<Object>();
|
ArrayList<Object> NUMBERS_INPUT = new ArrayList<Object>();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue