diff options
author | olim <bobq4582@gmail.com> | 2024-05-02 19:38:55 +0100 |
---|---|---|
committer | olim <bobq4582@gmail.com> | 2024-05-09 22:42:35 +0100 |
commit | 08140d6b262a9b694cbc9d14189249d10c10eef6 (patch) | |
tree | b3c3a966f4d60f89411e9f8de2ebd5a9569675ba /src/main/java/de/hysky/skyblocker/utils | |
parent | feee94e2c2ce01262f3eb47d7b356ca9821bdb79 (diff) | |
download | Skyblocker-08140d6b262a9b694cbc9d14189249d10c10eef6.tar.gz Skyblocker-08140d6b262a9b694cbc9d14189249d10c10eef6.tar.bz2 Skyblocker-08140d6b262a9b694cbc9d14189249d10c10eef6.zip |
clean code and fix to 1.20.6
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/utils/Calculator.java | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/Calculator.java b/src/main/java/de/hysky/skyblocker/utils/Calculator.java index 19155708..bb6ed768 100644 --- a/src/main/java/de/hysky/skyblocker/utils/Calculator.java +++ b/src/main/java/de/hysky/skyblocker/utils/Calculator.java @@ -11,11 +11,13 @@ public class Calculator { public enum TokenType { NUMBER, OPERATOR, L_PARENTHESIS, R_PARENTHESIS } + public static class Token { public TokenType type; String value; int tokenLength; } + private static final Pattern NUMBER_PATTERN = Pattern.compile("(\\d+\\.?\\d*)([kmbs]?)"); private static final HashMap<String, Integer> magnitudeValues = Util.make(new HashMap<>(), map -> { map.put("s", 64); @@ -24,15 +26,14 @@ public class Calculator { map.put("b", 1000000000); }); - private static List<Token> lex(String input) { List<Token> tokens = new ArrayList<>(); - input = input.replace(" ", "").toLowerCase().replace("x","*"); + input = input.replace(" ", "").toLowerCase().replace("x", "*"); int i = 0; while (i < input.length()) { Token token = new Token(); switch (input.charAt(i)) { - case '+','-','*','/' -> { + case '+', '-', '*', '/' -> { token.type = TokenType.OPERATOR; token.value = String.valueOf(input.charAt(i)); token.tokenLength = 1; @@ -43,8 +44,8 @@ public class Calculator { token.value = String.valueOf(input.charAt(i)); token.tokenLength = 1; //add implicit multiplication when there is a number before brackets - if (!tokens.isEmpty() ) { - TokenType lastType = tokens.get(tokens.size()-1).type; + if (!tokens.isEmpty()) { + TokenType lastType = tokens.get(tokens.size() - 1).type; if (lastType == TokenType.R_PARENTHESIS || lastType == TokenType.NUMBER) { Token mutliplyToken = new Token(); mutliplyToken.type = TokenType.OPERATOR; @@ -66,8 +67,8 @@ public class Calculator { if (!numberMatcher.find()) {//invalid value to lex throw new UnsupportedOperationException("invalid character"); } - int end = numberMatcher.end(); - token.value = input.substring(i,i + end); + int end = numberMatcher.end(); + token.value = input.substring(i, i + end); token.tokenLength = end; } } @@ -80,7 +81,8 @@ public class Calculator { } /** - * This is an implementation of the shunting yard algorithm to convert the equation to reverse polish notation + * This is an implementation of the shunting yard algorithm to convert the equation to reverse polish notation + * * @param tokens equation in infix notation order * @return equation in RPN order */ @@ -114,7 +116,7 @@ public class Calculator { case L_PARENTHESIS -> { operatorStack.push(shuntingToken); } - case R_PARENTHESIS -> { + case R_PARENTHESIS -> { while (true) { if (operatorStack.isEmpty()) { throw new UnsupportedOperationException("Unbalanced left parenthesis"); @@ -139,12 +141,13 @@ public class Calculator { return outputQueue.stream().toList(); } + private static int getPrecedence(String operator) { switch (operator) { - case "+","-" -> { + case "+", "-" -> { return 0; } - case "*","/" -> { + case "*", "/" -> { return 1; } default -> throw new UnsupportedOperationException("Invalid operator"); @@ -152,7 +155,6 @@ public class Calculator { } /** - * * @param tokens list of Tokens in reverse polish notation * @return answer to equation */ @@ -216,5 +218,4 @@ public class Calculator { public static double calculate(String equation) { return evaluate(shunt(lex(equation))); } - } |