Skip to content

Commit 3d1cb3b

Browse files
committed
Updated README to reflect latest API updates
1 parent cad537c commit 3d1cb3b

File tree

1 file changed

+65
-15
lines changed

1 file changed

+65
-15
lines changed

README.md

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,20 @@
2727
- [Getting the library from JitPack](#getting-the-library-from-jitpack)
2828
- [Usage](#usage)
2929
- [Supported operators](#supported-operators)
30-
- [Basic usage](#basic-usage)
30+
- [Customization process](#customization-process)
31+
- [Solving process](#solving-process)
3132
- [Exceptions handling](#exceptions-handling)
3233
- [Related project (W.I.P.)](#related-project-wip)
3334
- [About the docs](#about-the-docs)
3435
- [Contribution](#contribution)
3536

3637
## Description of the project
3738

38-
This repo holds the code of a [Java library](https://en.wikipedia.org/wiki/Java_Class_Library) named JCalc that is especially made for those who, for any reason, want to allow users to input basic Math expressions in Java apps and quickly solve them during runtime (like, for example, when building a basic calculator). In order to make that possible, this library makes use of a custom implementation of the [Shunting Yard algorithm](https://en.wikipedia.org/wiki/Shunting_yard_algorithm) that allows it to quickly parse and solve Math expressions written in the notation commonly used to write Math expressions; also known as [infix notation](https://en.wikipedia.org/wiki/Infix_notation).
39+
This repo holds the code of a [Java library](https://en.wikipedia.org/wiki/Java_Class_Library) named JCalc made for parsing Math expressions as strings and quickly solve them. In order to make that possible, this library makes use of a custom implementation of the [Shunting Yard algorithm](https://en.wikipedia.org/wiki/Shunting_yard_algorithm) that allows it to quickly parse and solve Math expressions written in the notation commonly used to write Math expressions; also known as [infix notation](https://en.wikipedia.org/wiki/Infix_notation).
3940

40-
You can use this library to solve statements like, for example:
41+
You can use this library to solve Math expressions like, for example:
4142

4243
- **((25\*3-9)/(4+2)+5^3)-(48/8)\*(7+2)+14** (which is equals to **96**)
43-
- **2 \* 3 + 5 \* 2^3** (which is equals to **46**)
44-
- **2 * -(3 + 4! / 2) + 5^2** (which is equals to **-5**)
4544
- **3 + 4 * 2 / (1 - 5)^2^3** (which is equals to **3.000122070313**)
4645
- **1000 / (2^5) + (3!)^4 - 500 * (2 + 3)** (which is equals to **-1172.75**)
4746
- **(8^2 + 15 \* 4 - 7) / (3 + 5)\*(12 - 9) + 6^2 - (18 /3) + 11** (which is equals to **84.875**)
@@ -50,7 +49,7 @@ You can use this library to solve statements like, for example:
5049
- **(-2^3) * (-(3! + 4) / 2) + 5** (which is equals to **45**)
5150
- **2 + -3! * (-4^2) / (5 - 3)** (which is equals to **50**)
5251

53-
All the results previously shown were obtained with JCalc and checked using an online calculator app provided by [Desmos](https://www.desmos.com/about) in [this website](https://www.desmos.com/scientific).
52+
All the expressions previously shown are just a few examples. The results were obtained with JCalc and checked using an online calculator app provided by [Desmos](https://www.desmos.com/about) in [this website](https://www.desmos.com/scientific).
5453

5554
## Getting the library
5655

@@ -66,7 +65,7 @@ java --version
6665
>
6766
> The minimum Java version required to build this library is [Java 8](https://en.m.wikipedia.org/wiki/Java_version_history#Java_8).
6867
69-
If the execution of that command generates any output instead of an error message, then your installation of the JDK should be correct; and after effectively checking the JDK installation, you must create a local copy of this repo in your device. In order to do that, launch a terminal and execute the following command:
68+
If the execution of that command generates any output instead of an error message, then your installation of the JDK should be correct. After effectively checking the JDK installation, you must create a local copy of this repo in your device and, in order to do that, launch a terminal and execute the following command:
7069

7170
```bash
7271
git clone https://github.com/jr20xx/JCalc
@@ -88,7 +87,21 @@ If you decide to build the library without using any IDE, then open the newly cr
8887
gradlew.bat build
8988
```
9089

91-
Once the execution of that command is done, open the `jcalc` directory, then open the `builds` directory and, once you are watching its content, you'll see a folder named `libs`. In that last folder you'll find compiled files of the library, the [Javadocs](https://en.wikipedia.org/wiki/Javadoc) and the source code compressed as single JAR files that you can use as you wish.
90+
Once the execution of that command is done, open the `jcalc` directory, then open the `builds` directory and, once you are watching its content, you'll see a folder named `libs`. In that last folder you'll find compiled files of the library, the [Javadocs](https://en.wikipedia.org/wiki/Javadoc) and the source code compressed as independent JAR files that you can use as you wish. Here's a representation of the portion of the directories tree mentioned before, in plaintext format:
91+
92+
```txt
93+
JCalc (parent folder)
94+
├── jcalc
95+
│   ├── build
96+
│   │   ├── libs
97+
│   │   │   ├── jcalc-X.Y.Z.jar
98+
│   │   │   ├── jcalc-X.Y.Z-javadoc.jar
99+
│   │   │   └── jcalc-X.Y.Z-sources.jar
100+
```
101+
102+
> [!TIP]
103+
>
104+
> Please notice that **"X"**, **"Y"** and **"Z"** are used as placeholders in the names of the JAR files. They take the place of the version numbers of the library and are not part of the actual names of the compiled files.
92105
93106
### Getting the library from JitPack
94107

@@ -160,19 +173,53 @@ The following tables summarize the rest of the symbols or tokens currently suppo
160173
> [!WARNING]
161174
>
162175
>- The support for the advanced operators is still under development.
163-
>- Trigonometric functions are made to work with radians.
164176
>- The library is case sensitive and that means that, for example, "E" and "e" are treated differently based on their casing.
165177
166-
### Basic usage
178+
### Customization process
179+
180+
One of the main ideas behind the creation of JCalc is to allow a certain level of customization in the way it works; and that's precisely why it involves a customization (or configuration) step to perform the solving process of a Math expression. This is done with the help of the `ConfigurationBuilder` class, introduced in the third version of the library. This class has a method to control the precision of the final result obtained when the Math expression is completely solved. In addition to that, there's also a method that allows you to toggle the functionality to make an attempt to balance the parentheses in a given Math expression. Finally, it also contains another method that lets you control whether to use radians or degrees when dealing with trigonometric functions. Here's an example of how to create a new instance of the `ConfigurationBuilder` class and how to set up all the parameters previously mentioned one by one:
181+
182+
```java
183+
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
184+
configurationBuilder.setPrecision(10);
185+
configurationBuilder.setBalanceParentheses(true);
186+
configurationBuilder.setUseRadians(false);
187+
```
188+
189+
Alternatively, you can set up all those parameters all at once as it follows:
167190

168-
To make use of this library, you simply have to call the method `JCalc.solveMathExpression(...)` providing it with a String containing the Math expression that you want to get solved and a boolean value used to specify when to automatically attempt to balance the parentheses in the given Math expression. When called, that method will return another String with the result of solving the given Math expression; but if the given expression is empty or contains only whitespaces, `null` will be returned instead of any result. In addition to all that, if the Math expression contains any whitespace, they'll be automatically removed.
191+
```java
192+
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
193+
.setPrecision(10)
194+
.setBalanceParentheses(true)
195+
.setUseRadians(false);
196+
```
197+
198+
If you forget to customize any parameter or just create a new instance of `ConfigurationBuilder` without setting up any parameter, the default values will be used instead. The default value for precision is 12 and the minimum accepted value for that setting is 3; so if you try to set it to a lower number, it will default to 3. Besides all that, the library defaults to radians when dealing with trigonometric functions and, by default, it disables the process to balance the parentheses in a Math expression.
199+
200+
### Solving process
201+
202+
Once the instance of the `ConfigurationBuilder` class that will be used to solve Math expressions is ready to use, you just have to call the `JCalc.solveMathExpression(...)` method passing a String to it containing the Math expression that you want to get solved and the previously created instance of the `ConfigurationBuilder` class. When called, that method will return another String with the result of solving the given Math expression; but if the given expression is either empty, `null` or contains only whitespaces, `null` will be returned as result. In addition to all that, any whitespace in the Math expression will be ignored. It's also important to highlight that if you pass `null` as the `ConfigurationBuilder` parameter, a new `ConfigurationBuilder` will be initialized using the default values described in the last part of the [customization process](#customization-process). To help you to get an idea of how to perform this last step, here's a code example:
203+
204+
```java
205+
String expression = "3 + 4 * 2 / (1 - 5)^2^3";
206+
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
207+
configurationBuilder.setPrecision(10);
208+
configurationBuilder.setBalanceParentheses(true);
209+
configurationBuilder.setUseRadians(false);
210+
String result = JCalc.solveMathExpression(expression, configurationBuilder);
211+
System.out.print(result); // Prints "3.0001220703"
212+
```
169213

170-
Here's a clear code example for you to get an idea of how to work with the library and to allow you to know which is the method that must be called to get the work done:
214+
Like before, you can simplify things up and get a result using a, perhaps, "quicker" or "shorter" syntax as it follows:
171215

172216
```java
173217
String expression = "3 + 4 * 2 / (1 - 5)^2^3";
174-
String result = JCalc.solveMathExpression(expression, true);
175-
System.out.print(result); // Prints "3.000122070313"
218+
String result = JCalc.solveMathExpression(expression, new ConfigurationBuilder()
219+
.setPrecision(10)
220+
.setBalanceParentheses(true)
221+
.setUseRadians(false));
222+
System.out.print(result); // Prints "3.0001220703"
176223
```
177224

178225
### Exceptions handling
@@ -182,7 +229,10 @@ This library contains a small set of custom exceptions that should be controlled
182229
```java
183230
try {
184231
String expression = "2 * 3 + 5 * 2^3)";
185-
String result = JCalc.solveMathExpression(expression, true);
232+
String result = JCalc.solveMathExpression(expression, new ConfigurationBuilder()
233+
.setPrecision(10)
234+
.setBalanceParentheses(true)
235+
.setUseRadians(false));
186236
}
187237
catch (UnbalancedParenthesesException exception) {
188238
// This exception occurs when the parentheses were not placed correctly and `false` is provided as second parameter

0 commit comments

Comments
 (0)