1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-22 15:30:09 -08:00

Add the four external essential functions.

This commit is contained in:
Arthur Drobot 2017-07-24 14:48:16 -07:00
parent d4a55f71f5
commit 3b91304175

56
src/org/nwapw/abacus/number/FunctionDatabase.java Normal file → Executable file
View File

@ -8,10 +8,66 @@ public class FunctionDatabase {
private void registerDefault(){ 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(){ public FunctionDatabase(){
functions = new HashMap<>(); functions = new HashMap<>();
registerDefault();
} }
public Function getFunction(String name){ public Function getFunction(String name){