fathzer / javaluator Goto Github PK
View Code? Open in Web Editor NEWA java infix evaluator based on "Shunting Yard" algorithm.
Home Page: http://javaluator.fathzer.com
License: Apache License 2.0
A java infix evaluator based on "Shunting Yard" algorithm.
Home Page: http://javaluator.fathzer.com
License: Apache License 2.0
Hi! This library is great. Im having just one requirement, however:
Is it possible to implement a method that returns the custom variables from the input given by the user?
For example, the user inputs:
String input = "(workHours^2 * workers)/100";
And for that given input, the method would return : "workHours", "workers".
I did a method that attempts to do that, it works fine except when the variable name contains a number.
For example:
String input = "workHours1^2 * workers2) / 100";
....
allBrackets = new HashSet<>();
allBrackets.add("(");
allBrackets.add(")");
allBrackets.add("[");
allBrackets.add("]");
allBrackets.add("}");
allBrackets.add("{");
allOperators = new HashSet<>();
allOperators.add(LocalizedEvaluator.MULTIPLY.getSymbol());
allOperators.add(LocalizedEvaluator.MODULO.getSymbol());
allOperators.add(LocalizedEvaluator.DIVIDE.getSymbol());
allOperators.add(LocalizedEvaluator.EXPONENT.getSymbol());
allOperators.add(LocalizedEvaluator.MINUS.getSymbol());
allOperators.add(LocalizedEvaluator.NEGATE.getSymbol());
allOperators.add(LocalizedEvaluator.NEGATE_HIGH.getSymbol());
allOperators.add(LocalizedEvaluator.PLUS.getSymbol());
allFunctions = new HashMap<>();
allFunctions.put(LocalizedEvaluator.RAIZ, LocalizedEvaluator.RAIZ.getName());
allFunctions.put(LocalizedEvaluator.ABS, LocalizedEvaluator.ABS.getName());
allFunctions.put(LocalizedEvaluator.ACOSINE, LocalizedEvaluator.ACOSINE.getName());
allFunctions.put(LocalizedEvaluator.ASINE, LocalizedEvaluator.ASINE.getName());
allFunctions.put(LocalizedEvaluator.ATAN, LocalizedEvaluator.ATAN.getName());
allFunctions.put(LocalizedEvaluator.AVERAGE, LocalizedEvaluator.AVERAGE.getName());
allFunctions.put(LocalizedEvaluator.CEIL, LocalizedEvaluator.CEIL.getName());
allFunctions.put(LocalizedEvaluator.COSINE, LocalizedEvaluator.COSINE.getName());
allFunctions.put(LocalizedEvaluator.COSINEH, LocalizedEvaluator.COSINEH.getName());
allFunctions.put(LocalizedEvaluator.FLOOR, LocalizedEvaluator.FLOOR.getName());
allFunctions.put(LocalizedEvaluator.LN, LocalizedEvaluator.LN.getName());
allFunctions.put(LocalizedEvaluator.LOG, LocalizedEvaluator.LOG.getName());
allFunctions.put(LocalizedEvaluator.MAX, LocalizedEvaluator.MAX.getName());
allFunctions.put(LocalizedEvaluator.MIN, LocalizedEvaluator.MIN.getName());
allFunctions.put(LocalizedEvaluator.TANGENTH, LocalizedEvaluator.TANGENTH.getName());
allFunctions.put(LocalizedEvaluator.TANGENT, LocalizedEvaluator.TANGENT.getName());
allFunctions.put(LocalizedEvaluator.SUM, LocalizedEvaluator.SUM.getName());
allFunctions.put(LocalizedEvaluator.SINEH, LocalizedEvaluator.SINEH.getName());
allFunctions.put(LocalizedEvaluator.SINE, LocalizedEvaluator.SINE.getName());
allFunctions.put(LocalizedEvaluator.ROUND, LocalizedEvaluator.ROUND.getName());
allFunctions.put(LocalizedEvaluator.RANDOM, LocalizedEvaluator.RANDOM.getName());
....
public static ArrayList<String> getVariables(String input) {
for (String operator : allOperators) {
input = input.replace(operator.trim(), "");
}
for (Map.Entry<Function, String> entry : allFunctions.entrySet()) {
String translated = entry.getValue();
input = myReplace(input, translated, "");
}
for (String bracket : allBrackets) {
input = input.replace(bracket.trim(), "");
}
input = input.trim();
String[] words = input.split("\\s+");
ArrayList<String> result = new ArrayList<>();
for (String word : words) {
word = word.trim();
if (!isNumeric(word)) {
//
word = word.replaceAll("\\d", "");
//
result.add(word);
}
}
return result;
}
I myself working on a similar project
Given an expression with a trailing, right-associative operator, the operator may be evaluated incorrectly.
When using the SimpleBooleanEvaluator
, an expression like true!
resolves to false
, but I believe it should result in a syntax error.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.