diff --git a/README.md b/README.md index c1ed9f2..41ecc8d 100644 --- a/README.md +++ b/README.md @@ -1,73 +1,100 @@ -## 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 { +## Math Executor Library by Sh18RW + +The Math Executor Library. It can handle simple expressions like 2 + 2 as well as more advanced ones like: +``` +14 / (7 / (3 + (1 + (3/log(2, 4))) * 2 / 2 * 2) * (5 ^ 0 + 1)) +``` + +Features: +- Parse mathematical expressions into tokens. +- Build expression trees for evaluation. +- Calculate mathematical expression trees. + +### How to Use + +1. Parser Class + + The Parser class tokenizes a given mathematical expression. \ + *Note: The parser does not validate the logical sequence of tokens.* + + Example usage: + ```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))"); + Parser parser = new Parser("14 / (7 / (3 + (1 + (3/log(2tg(Pi/4), 4))) * 2 / 2 * 2) * (5 ^ 0 + 1))"); try { - parser.parse(); + parser.parse(); } catch (ParserUnknownEntityException | ParserIllegalTokenValueException e) { - // process exceptions + // Handle exceptions } + + List> result = parser.getResult(); + // Process the list of tokens + } + } + ``` +2. ExpressionsTree Class - 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 + The ExpressionsTree class creates a structured tree representation of a mathematical expression for evaluation. \ + *Note: It only validates critical errors, not token sequence correctness.* + + Example usage: + ```java + public class App { + public static void process(List> tokens) { + ExpressionsTree expressionsTree = new ExpressionsTree(tokens); + try { + expressionsTree.build(); + } catch (ExpressionException e) { + // Handle exception } - } - ``` - **Doesn't check for valid sequence at all. Only critical misspells.** -3. Calculator class. - ```java + + Expression expression = expressionsTree.getRoot(); + // Use the expression object + } + } + ``` + +3. Calculator Class + + The Calculator class evaluates the mathematical expression. It can be reused for multiple calculations, avoiding repetitive instantiation. + + Example usage: + ```java public class App { - public static void calculate(Expression expression) { - double result = 0; - try { - result = Calculator.getInstance().calculate((SequenceExpression) expression); - } catch (CalculatorException e) { - // process exception - } + public static void calculate(Expression expression) { + double result = 0.0; + try { + result = Calculator.getInstance().calculate((SequenceExpression) expression); + } catch (CalculatorException e) { + // Handle exception + } + + System.out.println(result); + } + } + ``` + +4. Numbers Utility Class + + The Numbers class provides utility methods for working with numbers, though direct usage is not typically required. - System.out.println(result); - } + Example usage: + ```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 + } } - ``` - 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 + ``` + +### Full Example + +Below is a complete example demonstrating the parsing, building, and evaluating of a mathematical expression: ```java package ru.corvinella; @@ -83,22 +110,33 @@ 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: "); + 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(); + String expression = scanner.nextLine(); - Parser parser = new Parser(expression); - parser.parse(); + // Step 1: Parse the expression + Parser parser = new Parser(expression); + parser.parse(); - ExpressionsTree expressionsTree = new ExpressionsTree(parser.getResult()); - expressionsTree.build(); + // Step 2: Build the expression tree + ExpressionsTree expressionsTree = new ExpressionsTree(parser.getResult()); + expressionsTree.build(); - double result = Calculator.getInstance() - .calculate((SequenceExpression) expressionsTree.getRoot()); + // Step 3: Calculate the result + double result = Calculator.getInstance() + .calculate((SequenceExpression) expressionsTree.getRoot()); - System.out.printf("%s = %f%n", expression, result); - } + System.out.printf("%s = %f%n", expression, result); + } } -``` \ No newline at end of file +``` + +### Adding New Function and Constants +You can expand the library by adding new mathematical functions. Here’s how: +1. Add the function name to `FunctionExpression#supportedFunctions`. +2. Implement the function logic in `FunctionCalculator`. +3. Don’t forget to register your new calculation function in the `processors`. + +The same process applies if you want to add new constants. \ No newline at end of file