|
| 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