Skip to content

Commit

Permalink
Add README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh18RW committed Nov 30, 2024
1 parent 58f0da9 commit 245b176
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
105 changes: 105 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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<Token<?>> 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<Token<?>> 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);
}
}
```
2 changes: 1 addition & 1 deletion src/test/java/ru/corvinella/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 245b176

Please sign in to comment.