Skip to content

Commit d58dfb2

Browse files
committed
Added Math Module for doing algebra
1 parent 47b8fa2 commit d58dfb2

File tree

4 files changed

+128
-13
lines changed

4 files changed

+128
-13
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@ dependencies {
4040
implementation 'org.jsoup:jsoup:1.16.2'
4141
implementation fileTree(dir: 'libraries', include: '*.jar')
4242
implementation fileTree(dir: 'libraries', include: '*.dll')
43+
44+
45+
implementation 'org.mariuszgromada.math:MathParser.org-mXparser:5.2.1'
4346
}

src/main/java/de/jo/ConsoleTools.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public ConsoleTools(String[] args) throws Exception{
6363

6464
public void logo() {
6565
boolean b = false;
66-
for(String s : Files.sysLines("logo2.txt")) {
66+
for(String s : Files.sysLines("logo.txt")) {
6767
System.out.println((b ? ConsoleColors.YELLOW : ConsoleColors.YELLOW_BRIGHT)+ConsoleColors.BLACK_BACKGROUND+s+ConsoleColors.RESET);
6868
b = !b;
6969
}

src/main/java/de/jo/Main.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
package de.jo;
22

3-
import de.jo.modules.ModuleInfo;
4-
import de.jo.options.Option;
5-
import de.jo.options.Options;
6-
import de.jo.util.ConsoleColors;
7-
import de.jo.util.PackageScanner;
8-
import de.jo.util.Strings;
9-
import joptsimple.OptionParser;
10-
import joptsimple.OptionSet;
113
import org.apache.log4j.BasicConfigurator;
12-
13-
import java.io.IOException;
14-
import java.io.PrintStream;
15-
import java.util.Arrays;
4+
import org.mariuszgromada.math.mxparser.License;
5+
import org.mariuszgromada.math.mxparser.Tutorial;
166

177
/**
188
* @author Johannes Hans 27.10.2023
@@ -21,7 +11,9 @@
2111
public class Main {
2212

2313
public static void main(String[] args) throws Exception {
14+
2415
BasicConfigurator.configure();
16+
License.iConfirmNonCommercialUse("Johannes Hans");
2517
new ConsoleTools(args);
2618
}
2719

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package de.jo.modules.impl.other;
2+
3+
import de.jo.modules.Module;
4+
import de.jo.modules.ModuleInfo;
5+
import de.jo.util.ConsoleColors;
6+
import de.jo.util.Strings;
7+
import org.mariuszgromada.math.mxparser.Constant;
8+
import org.mariuszgromada.math.mxparser.Expression;
9+
import org.mariuszgromada.math.mxparser.Function;
10+
import org.mariuszgromada.math.mxparser.mXparser;
11+
12+
import java.util.HashMap;
13+
import java.util.Scanner;
14+
15+
/**
16+
* @author Johannes Hans 05.12.2023
17+
* @Project ConsoleTools
18+
*/
19+
@ModuleInfo(name = "math", description = "", syntax = "<Expression>")
20+
public class ModuleMath implements Module {
21+
22+
private final HashMap<String, Function> functions = new HashMap<>();
23+
private final HashMap<String, Constant> constants = new HashMap<>();
24+
25+
@Override
26+
public void run(String... args) throws Exception {
27+
boolean math = true;
28+
boolean verbose = false;
29+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+"You're now in Math-Mode!");
30+
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
31+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+"Use the command \":f Function\" to write functions");
32+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+"Use the command \":d Function\" to write get a quick derivative function named functionname+\"d\"");
33+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+"Use the command \":c Const\" to write constants");
34+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+"Use the command \":e Expression\" to evaluate expressions");
35+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+"Use the command \":v\" to enable/disable calculating documentation");
36+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+"Use the command \"exit\" to quit");
37+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+"For more documentation visit https://mathparser.org/mxparser-tutorial/");
38+
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
39+
40+
while(math) {
41+
Scanner scanner = new Scanner(System.in);
42+
String line = "";
43+
while((line = scanner.nextLine()) != null) {
44+
if(line.toLowerCase().startsWith(":f")) {
45+
Function function = new Function(line.substring(2));
46+
functions.values().forEach(f -> {
47+
if(!f.getFunctionName().equals(function.getFunctionName())) {
48+
function.addDefinitions(f);
49+
}
50+
});
51+
constants.values().forEach(f -> {
52+
if(!f.getConstantName().equals(function.getFunctionName())) {
53+
function.addDefinitions(f);
54+
}
55+
});
56+
if(!functions.containsKey(function.getFunctionName())) {
57+
functions.put(function.getFunctionName(), function);
58+
}else {
59+
functions.replace(function.getFunctionName(), function);
60+
}
61+
System.out.println(ConsoleColors.GREEN+"> "+function.getFunctionName()+": "+function.getFunctionExpressionString());
62+
}
63+
else if(line.toLowerCase().startsWith(":d")) {
64+
String fun = line.substring(2).trim();
65+
if(!functions.containsKey(fun)) {
66+
Strings.error("Invalid function name: "+fun);
67+
}else {
68+
Function function = functions.get(fun);
69+
70+
Function derivative = new Function(fun+"d", "der("+fun+"(y), y, x)", "x");
71+
functions.values().forEach(f -> {
72+
if(!f.getFunctionName().equals(derivative.getFunctionName())) {
73+
derivative.addDefinitions(f);
74+
}
75+
});
76+
if(!functions.containsKey(derivative.getFunctionName())) {
77+
functions.put(derivative.getFunctionName(), derivative);
78+
}else {
79+
functions.replace(derivative.getFunctionName(), derivative);
80+
}
81+
System.out.println(ConsoleColors.GREEN+"> "+derivative.getFunctionName()+": "+derivative.getFunctionExpressionString());
82+
}
83+
}
84+
else if(line.toLowerCase().startsWith(":c")) {
85+
Constant cons = new Constant(line.substring(2));
86+
if(!constants.containsKey(cons.getConstantName())) {
87+
constants.put(cons.getConstantName(), cons);
88+
}else {
89+
constants.replace(cons.getConstantName(), cons);
90+
}
91+
System.out.println(ConsoleColors.GREEN+"> "+cons.getConstantValue());
92+
} else if(line.toLowerCase().startsWith(":e")){
93+
Expression expression = new Expression(line.substring(2));
94+
functions.values().forEach(expression::addDefinitions);
95+
constants.values().forEach(expression::addDefinitions);
96+
expression.enableImpliedMultiplicationMode();
97+
if(verbose) expression.setVerboseMode();
98+
System.out.println(ConsoleColors.GREEN+"> "+expression.calculate());
99+
}else if(line.toLowerCase().startsWith(":v")){
100+
verbose = !verbose;
101+
System.out.println(ConsoleColors.GREEN+"> "+(verbose ? "enabled" : "disabled")+" calculation documentation");
102+
}else{
103+
if(line.equals("exit")) {
104+
functions.clear();
105+
constants.clear();;
106+
math = false;
107+
break;
108+
}else {
109+
Strings.error("Invalid operation");
110+
}
111+
}
112+
}
113+
114+
115+
}
116+
System.out.println(ConsoleColors.YELLOW_BRIGHT+"---------------------------------");
117+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+"Done with math!");
118+
System.out.println();
119+
}
120+
}

0 commit comments

Comments
 (0)