You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+65-15Lines changed: 65 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,21 +27,20 @@
27
27
-[Getting the library from JitPack](#getting-the-library-from-jitpack)
28
28
-[Usage](#usage)
29
29
-[Supported operators](#supported-operators)
30
-
-[Basic usage](#basic-usage)
30
+
-[Customization process](#customization-process)
31
+
-[Solving process](#solving-process)
31
32
-[Exceptions handling](#exceptions-handling)
32
33
-[Related project (W.I.P.)](#related-project-wip)
33
34
-[About the docs](#about-the-docs)
34
35
-[Contribution](#contribution)
35
36
36
37
## Description of the project
37
38
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).
39
40
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:
41
42
42
43
-**((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**)
45
44
-**3 + 4 * 2 / (1 - 5)^2^3** (which is equals to **3.000122070313**)
46
45
-**1000 / (2^5) + (3!)^4 - 500 * (2 + 3)** (which is equals to **-1172.75**)
@@ -50,7 +49,7 @@ You can use this library to solve statements like, for example:
50
49
-**(-2^3) * (-(3! + 4) / 2) + 5** (which is equals to **45**)
51
50
-**2 + -3! * (-4^2) / (5 - 3)** (which is equals to **50**)
52
51
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).
54
53
55
54
## Getting the library
56
55
@@ -66,7 +65,7 @@ java --version
66
65
>
67
66
> The minimum Java version required to build this library is [Java 8](https://en.m.wikipedia.org/wiki/Java_version_history#Java_8).
68
67
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:
70
69
71
70
```bash
72
71
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
88
87
gradlew.bat build
89
88
```
90
89
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.
92
105
93
106
### Getting the library from JitPack
94
107
@@ -160,19 +173,53 @@ The following tables summarize the rest of the symbols or tokens currently suppo
160
173
> [!WARNING]
161
174
>
162
175
>- The support for the advanced operators is still under development.
163
-
>- Trigonometric functions are made to work with radians.
164
176
>- The library is case sensitive and that means that, for example, "E" and "e" are treated differently based on their casing.
165
177
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:
Alternatively, you can set up all those parameters all at once as it follows:
167
190
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.
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:
String result =JCalc.solveMathExpression(expression, configurationBuilder);
211
+
System.out.print(result); // Prints "3.0001220703"
212
+
```
169
213
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:
171
215
172
216
```java
173
217
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, newConfigurationBuilder()
219
+
.setPrecision(10)
220
+
.setBalanceParentheses(true)
221
+
.setUseRadians(false));
222
+
System.out.print(result); // Prints "3.0001220703"
176
223
```
177
224
178
225
### Exceptions handling
@@ -182,7 +229,10 @@ This library contains a small set of custom exceptions that should be controlled
182
229
```java
183
230
try {
184
231
String expression ="2 * 3 + 5 * 2^3)";
185
-
String result =JCalc.solveMathExpression(expression, true);
232
+
String result =JCalc.solveMathExpression(expression, newConfigurationBuilder()
0 commit comments