From 245b17618c1180b973e3fc68791e4ca92d5500d7 Mon Sep 17 00:00:00 2001 From: Sh18RW Date: Sun, 1 Dec 2024 00:56:55 +0300 Subject: [PATCH] Add README.md --- README.md | 105 ++++++++++++++++++ .../java/ru/corvinella/CalculatorTest.java | 2 +- 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..2ebf1fa --- /dev/null +++ b/README.md @@ -0,0 +1,105 @@ +## Math Executor lib by Sh18RW +Here is a library to run as simple math expression like as `2 + 2` as `14 / (7 / (3 + (1 + (3/log(2, 4))) * 2 / 2 * 2) * (5 ^ 0 + 1))`. +### How to use +Library contains: +1. Parser class. + ```java + public class App { + public static void main(String[] args) { + Parser parser = new Parser("14 / (7 / (3 + (1 + (3/log(2, 4))) * 2 / 2 * 2) * (5 ^ 0 + 1))"); + try { + parser.parse(); + } catch (ParserUnknownEntityException | ParserIllegalTokenValueException e) { + // process exceptions + } + + List> result = parser.getResult(); + // do something + } + } + ``` + It parses an input expression to List of tokens. + **It doesn't check for valid sequence.** +2. ExpressionTree class. + ```java + public class App { + public static void process(List> tokens) { + ExpressionsTree expressionsTree = new ExpressionsTree(tokens); + try { + expressionsTree.build(); + } catch (ExpressionException e) { + // process exception + } + + Expression expression = expressionsTree.getRoot(); + // do something + } + } + ``` + **Doesn't check for valid sequence at all. Only critical misspells.** +3. Calculator class. + ```java + public class App { + public static void calculate(Expression expression) { + Calculator calculator = new Calculator(); + double result; + try { + result = calculator.calculate((SequenceExpression) expression); + } catch (CalculatorException e) { + // process exception + } + + System.out.println(result); + } + } + ``` + You needn't instantiate Calculator every use, you can make it once. +4. Number class, but I don't recommend you to use it. + ```java + public class App { + public static void main(String[] args) { + System.out.println( + Numbers.getPrettierDoubleAsString(1.2345478, 4) + .equals("1.2345")); // true + System.out.println(Numbers.countTensNumbers(11234455.01246) == 8); // true + System.out.println(Numbers.countNumbersAfterPoint(1.100293) == 6); // true + System.out.println(Numbers.equals(12.0056, 12.0040, 0.01)); // true + System.out.println(Numbers.lessThan(12.09, 12.0, 0.1, true)); // true + } + } + ``` +### Example +```java +package ru.corvinella; + +import ru.corvinella.expressions.ExpressionsTree; +import ru.corvinella.expressions.entries.SequenceExpression; +import ru.corvinella.expressions.exceptions.ExpressionException; +import ru.corvinella.math.Calculator; +import ru.corvinella.math.exceptions.CalculatorException; +import ru.corvinella.parser.Parser; +import ru.corvinella.parser.exceptions.ParserIllegalTokenValueException; +import ru.corvinella.parser.exceptions.ParserUnknownEntityException; + +import java.util.Scanner; + +public class App { + public static void main(String[] args) throws ParserIllegalTokenValueException, ParserUnknownEntityException, ExpressionException, CalculatorException { + Scanner scanner = new Scanner(System.in); + System.out.print("Please enter an expression: "); + + String expression = scanner.nextLine(); + + Parser parser = new Parser(expression); + parser.parse(); + + ExpressionsTree expressionsTree = new ExpressionsTree(parser.getResult()); + expressionsTree.build(); + + Calculator calculator = new Calculator(); + double result = calculator.calculate((SequenceExpression) expressionsTree.getRoot()); + + System.out.printf("%s = %f%n", expression, result); + } +} +``` \ No newline at end of file diff --git a/src/test/java/ru/corvinella/CalculatorTest.java b/src/test/java/ru/corvinella/CalculatorTest.java index 6cb201f..8474177 100644 --- a/src/test/java/ru/corvinella/CalculatorTest.java +++ b/src/test/java/ru/corvinella/CalculatorTest.java @@ -30,7 +30,7 @@ public void testCalculator() throws ParserIllegalTokenValueException, ParserUnkn makeTest("2 + (2 + 2) / (2 ^ 2)", 3.0); makeTest("2 * (3 * (5 - log(2, 16)) / 6)", 1.0); makeTest("Pi*2", Math.PI*2); - makeTest("14 / (7 / (3 + (1 + (3/2)) * 2 / 2 * 2) * (5 ^ 0 + 1))", 8.0); + makeTest("14 / (7 / (3 + (1 + (3/log(2, 4))) * 2 / 2 * 2) * (5 ^ 0 + 1))", 8.0); makeTest("1 + 2log(2,4) / 3 - 4", 1.0 + 2.0 * 2.0 / 3.0 - 4.0); makeTest("3*e", 3 * Math.E); }