mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 07:20:09 -08:00
Add the four external essential functions.
This commit is contained in:
parent
d4a55f71f5
commit
3b91304175
56
src/org/nwapw/abacus/number/FunctionDatabase.java
Normal file → Executable file
56
src/org/nwapw/abacus/number/FunctionDatabase.java
Normal file → Executable file
|
@ -8,10 +8,66 @@ public class FunctionDatabase {
|
|||
|
||||
private void registerDefault(){
|
||||
|
||||
functions.put("+", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(Number[] params) {
|
||||
return params.length >= 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Number applyInternal(Number[] params) {
|
||||
Number sum = params[0];
|
||||
for(int i = 1; i < params.length; i++){
|
||||
sum = sum.add(params[i]);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
});
|
||||
|
||||
functions.put("-", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(Number[] params) {
|
||||
return params.length == 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Number applyInternal(Number[] params) {
|
||||
return params[0].subtract(params[1]);
|
||||
}
|
||||
});
|
||||
|
||||
functions.put("*", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(Number[] params) {
|
||||
return params.length >= 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Number applyInternal(Number[] params) {
|
||||
Number product = params[1];
|
||||
for(int i = 1; i < params.length; i++){
|
||||
product = product.multiply(params[i]);
|
||||
}
|
||||
return product;
|
||||
}
|
||||
});
|
||||
|
||||
functions.put("/", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(Number[] params) {
|
||||
return params.length == 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Number applyInternal(Number[] params) {
|
||||
return params[0].divide(params[1]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public FunctionDatabase(){
|
||||
functions = new HashMap<>();
|
||||
registerDefault();
|
||||
}
|
||||
|
||||
public Function getFunction(String name){
|
||||
|
|
Loading…
Reference in New Issue
Block a user