aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorRoman / Linnea Gräf <roman.graef@gmail.com>2023-06-19 19:23:43 +0200
committerGitHub <noreply@github.com>2023-06-19 19:23:43 +0200
commita82819ec18bed8212fa73fda987cff951b810450 (patch)
tree2e18b55a6870ce5bd75516dd140ab3e6441054e5 /src/test
parent7fd26d14e742c974ec50686c375ccdfd9c968faf (diff)
downloadNotEnoughUpdates-a82819ec18bed8212fa73fda987cff951b810450.tar.gz
NotEnoughUpdates-a82819ec18bed8212fa73fda987cff951b810450.tar.bz2
NotEnoughUpdates-a82819ec18bed8212fa73fda987cff951b810450.zip
Add variables to calculator (#721)
* Add variables to calculator * Fix unary minus for variables
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/io/github/moulberry/notenoughupdates/util/CalculatorTest.java33
1 files changed, 22 insertions, 11 deletions
diff --git a/src/test/java/io/github/moulberry/notenoughupdates/util/CalculatorTest.java b/src/test/java/io/github/moulberry/notenoughupdates/util/CalculatorTest.java
index 66ba5442..eff4a8de 100644
--- a/src/test/java/io/github/moulberry/notenoughupdates/util/CalculatorTest.java
+++ b/src/test/java/io/github/moulberry/notenoughupdates/util/CalculatorTest.java
@@ -21,21 +21,32 @@ package io.github.moulberry.notenoughupdates.util;
import java.math.BigDecimal;
import java.util.List;
+import java.util.Optional;
+import java.util.Scanner;
public class CalculatorTest {
public static void main(String[] args) throws Calculator.CalculatorException {
- List<Calculator.Token> lex = Calculator.lex("10k + 3 * 4m");
- List<Calculator.Token> shunted = Calculator.shuntingYard(lex);
- for (Calculator.Token rawToken : shunted) {
- System.out.printf(
- "%s(%s)",
- rawToken.type,
- rawToken.operatorValue == null ? rawToken.numericValue + " * 10 ^ " + rawToken.exponent : rawToken.operatorValue
- );
+ Scanner s = new Scanner(System.in);
+ while (true) {
+ try {
+ List<Calculator.Token> lex = Calculator.lex(s.nextLine());
+ List<Calculator.Token> shunted = Calculator.shuntingYard(lex);
+ for (Calculator.Token rawToken : shunted) {
+ System.out.printf(
+ "%s(%s)",
+ rawToken.type,
+ rawToken.operatorValue == null
+ ? rawToken.numericValue + " * 10 ^ " + rawToken.exponent
+ : rawToken.operatorValue
+ );
+ }
+ System.out.println();
+ BigDecimal evaluate = Calculator.evaluate(name -> Optional.of(BigDecimal.valueOf(16)), shunted);
+ System.out.println("Eval: " + evaluate);
+ } catch (Calculator.CalculatorException e) {
+ e.printStackTrace();
+ }
}
- System.out.println();
- BigDecimal evaluate = Calculator.evaluate(shunted);
- System.out.println("Eval: " + evaluate);
}
}