mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-26 08:35:20 +00:00
Compare commits
1 Commits
plugins-up
...
binary-num
| Author | SHA1 | Date | |
|---|---|---|---|
| 0be67410ec |
@@ -1,6 +1,6 @@
|
|||||||
package org.nwapw.abacus;
|
package org.nwapw.abacus;
|
||||||
|
|
||||||
import org.nwapw.abacus.config.Configuration;
|
import org.nwapw.abacus.config.ConfigurationObject;
|
||||||
import org.nwapw.abacus.fx.AbacusApplication;
|
import org.nwapw.abacus.fx.AbacusApplication;
|
||||||
import org.nwapw.abacus.number.NaiveNumber;
|
import org.nwapw.abacus.number.NaiveNumber;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
@@ -9,7 +9,7 @@ import org.nwapw.abacus.parsing.ShuntingYardParser;
|
|||||||
import org.nwapw.abacus.parsing.TreeBuilder;
|
import org.nwapw.abacus.parsing.TreeBuilder;
|
||||||
import org.nwapw.abacus.plugin.ClassFinder;
|
import org.nwapw.abacus.plugin.ClassFinder;
|
||||||
import org.nwapw.abacus.plugin.PluginManager;
|
import org.nwapw.abacus.plugin.PluginManager;
|
||||||
//import org.nwapw.abacus.plugin.StandardPlugin;
|
import org.nwapw.abacus.plugin.StandardPlugin;
|
||||||
import org.nwapw.abacus.tree.NumberReducer;
|
import org.nwapw.abacus.tree.NumberReducer;
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ public class Abacus {
|
|||||||
/**
|
/**
|
||||||
* The configuration loaded from a file.
|
* The configuration loaded from a file.
|
||||||
*/
|
*/
|
||||||
private Configuration configuration;
|
private ConfigurationObject configuration;
|
||||||
/**
|
/**
|
||||||
* The tree builder used to construct a tree
|
* The tree builder used to construct a tree
|
||||||
* from a string.
|
* from a string.
|
||||||
@@ -59,30 +59,24 @@ public class Abacus {
|
|||||||
public Abacus() {
|
public Abacus() {
|
||||||
pluginManager = new PluginManager();
|
pluginManager = new PluginManager();
|
||||||
numberReducer = new NumberReducer(this);
|
numberReducer = new NumberReducer(this);
|
||||||
configuration = new Configuration(CONFIG_FILE);
|
configuration = new ConfigurationObject(CONFIG_FILE);
|
||||||
configuration.saveTo(CONFIG_FILE);
|
configuration.save(CONFIG_FILE);
|
||||||
LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
||||||
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
|
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
|
||||||
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
|
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
|
||||||
|
|
||||||
pluginManager.addListener(lexerTokenizer);
|
pluginManager.addListener(lexerTokenizer);
|
||||||
pluginManager.addListener(shuntingYardParser);
|
pluginManager.addListener(shuntingYardParser);
|
||||||
//pluginManager.addInstantiated(new StandardPlugin(pluginManager));
|
pluginManager.addInstantiated(new StandardPlugin(pluginManager));
|
||||||
/*
|
|
||||||
try {
|
try {
|
||||||
ClassFinder.loadJars("plugins")
|
ClassFinder.loadJars("plugins")
|
||||||
.forEach(plugin -> pluginManager.addClass(plugin));
|
.forEach(plugin -> pluginManager.addClass(plugin));
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}//*/
|
}
|
||||||
pluginManager.load();
|
pluginManager.load();
|
||||||
}
|
}
|
||||||
public void loadClass(Class<?> newClass){
|
|
||||||
pluginManager.addClass(newClass);
|
|
||||||
}
|
|
||||||
public void unloadClass(Class<?> newClass){
|
|
||||||
pluginManager.removeClass(newClass);
|
|
||||||
}
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
AbacusApplication.launch(AbacusApplication.class, args);
|
AbacusApplication.launch(AbacusApplication.class, args);
|
||||||
}
|
}
|
||||||
@@ -120,7 +114,7 @@ public class Abacus {
|
|||||||
*
|
*
|
||||||
* @return the configuration object.
|
* @return the configuration object.
|
||||||
*/
|
*/
|
||||||
public Configuration getConfiguration() {
|
public ConfigurationObject getConfiguration() {
|
||||||
return configuration;
|
return configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,83 +1,14 @@
|
|||||||
package org.nwapw.abacus.config;
|
package org.nwapw.abacus.config;
|
||||||
|
|
||||||
import com.moandjiezana.toml.Toml;
|
|
||||||
import com.moandjiezana.toml.TomlWriter;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The configuration object that stores
|
* Serializable class that will be used to load TOML
|
||||||
* options that the user can change.
|
* configurations.
|
||||||
*/
|
*/
|
||||||
public class Configuration {
|
public class Configuration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The TOML writer used to write this configuration to a file.
|
* The type of number this calculator should use.
|
||||||
*/
|
*/
|
||||||
private static final TomlWriter TOML_WRITER = new TomlWriter();
|
public String numberType;
|
||||||
/**
|
|
||||||
* The TOML reader used to load this config from a file.
|
|
||||||
*/
|
|
||||||
private static final Toml TOML_READER = new Toml();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The implementation of the number that should be used.
|
|
||||||
*/
|
|
||||||
private String numberImplementation = "naive";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new configuration with the given values.
|
|
||||||
* @param numberImplementation the number implementation, like "naive" or "precise"
|
|
||||||
*/
|
|
||||||
public Configuration(String numberImplementation){
|
|
||||||
this.numberImplementation = numberImplementation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads a configuration from a given file, keeping non-specified fields default.
|
|
||||||
* @param fromFile the file to load from.
|
|
||||||
*/
|
|
||||||
public Configuration(File fromFile){
|
|
||||||
if(!fromFile.exists()) return;
|
|
||||||
copyFrom(TOML_READER.read(fromFile).to(Configuration.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Copies the values from the given configuration into this one.
|
|
||||||
* @param otherConfiguration the configuration to copy from.
|
|
||||||
*/
|
|
||||||
public void copyFrom(Configuration otherConfiguration){
|
|
||||||
this.numberImplementation = otherConfiguration.numberImplementation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves this configuration to the given file, creating
|
|
||||||
* any directories that do not exist.
|
|
||||||
* @param file the file to save to.
|
|
||||||
*/
|
|
||||||
public void saveTo(File file){
|
|
||||||
if(file.getParentFile() != null) file.getParentFile().mkdirs();
|
|
||||||
try {
|
|
||||||
TOML_WRITER.write(this, file);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the number implementation from this configuration.
|
|
||||||
* @return the number implementation.
|
|
||||||
*/
|
|
||||||
public String getNumberImplementation() {
|
|
||||||
return numberImplementation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the number implementation for the configuration
|
|
||||||
* @param numberImplementation the number implementation.
|
|
||||||
*/
|
|
||||||
public void setNumberImplementation(String numberImplementation) {
|
|
||||||
this.numberImplementation = numberImplementation;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
111
src/main/java/org/nwapw/abacus/config/ConfigurationObject.java
Normal file
111
src/main/java/org/nwapw/abacus/config/ConfigurationObject.java
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
package org.nwapw.abacus.config;
|
||||||
|
|
||||||
|
import com.moandjiezana.toml.Toml;
|
||||||
|
import com.moandjiezana.toml.TomlWriter;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A configuration object, which essentially
|
||||||
|
* manages saving, loading, and getting values
|
||||||
|
* from the configuration. While Configuration is
|
||||||
|
* the data model, this is the interface with it.
|
||||||
|
*/
|
||||||
|
public class ConfigurationObject {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The writer used to store the configuration.
|
||||||
|
*/
|
||||||
|
private static final TomlWriter TOML_WRITER = new TomlWriter();
|
||||||
|
/**
|
||||||
|
* The configuration instance being modeled.
|
||||||
|
*/
|
||||||
|
private Configuration configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new configuration object with the given config.
|
||||||
|
*
|
||||||
|
* @param config the config to use.
|
||||||
|
*/
|
||||||
|
public ConfigurationObject(Configuration config) {
|
||||||
|
setup(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a configuration object by attempting to
|
||||||
|
* load a config from the given path, using the
|
||||||
|
* default configuration otherwise.
|
||||||
|
*
|
||||||
|
* @param path the path to attempt to load.
|
||||||
|
*/
|
||||||
|
public ConfigurationObject(File path) {
|
||||||
|
Configuration config;
|
||||||
|
if (!path.exists()) {
|
||||||
|
config = getDefaultConfig();
|
||||||
|
} else {
|
||||||
|
Toml parse = new Toml();
|
||||||
|
parse.read(path);
|
||||||
|
config = parse.to(Configuration.class);
|
||||||
|
}
|
||||||
|
setup(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new configuration object with the
|
||||||
|
* default configuration.
|
||||||
|
*/
|
||||||
|
public ConfigurationObject() {
|
||||||
|
setup(getDefaultConfig());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up the ConfigurationObject.
|
||||||
|
* different constructors do different things,
|
||||||
|
* but they all lead here.
|
||||||
|
*
|
||||||
|
* @param configuration the configuration to set up with.
|
||||||
|
*/
|
||||||
|
private void setup(Configuration configuration) {
|
||||||
|
this.configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a default configuration.
|
||||||
|
*
|
||||||
|
* @return the newly created default configuration.
|
||||||
|
*/
|
||||||
|
private Configuration getDefaultConfig() {
|
||||||
|
configuration = new Configuration();
|
||||||
|
configuration.numberType = "naive";
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the implementation the user has requested to
|
||||||
|
* represent their numbers.
|
||||||
|
*
|
||||||
|
* @return the implementation name.
|
||||||
|
*/
|
||||||
|
public String getNumberImplementation() {
|
||||||
|
return configuration.numberType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the ConfigurationObject to the given file.
|
||||||
|
*
|
||||||
|
* @param toFile the file to save ot.
|
||||||
|
* @return true if the save succeed, false if otherwise.
|
||||||
|
*/
|
||||||
|
public boolean save(File toFile) {
|
||||||
|
if (toFile.getParentFile() != null) toFile.getParentFile().mkdirs();
|
||||||
|
try {
|
||||||
|
TOML_WRITER.write(configuration, toFile);
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,11 +8,8 @@ import javafx.scene.text.Text;
|
|||||||
import javafx.util.Callback;
|
import javafx.util.Callback;
|
||||||
import org.nwapw.abacus.Abacus;
|
import org.nwapw.abacus.Abacus;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
import org.nwapw.abacus.plugin.ClassFinder;
|
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The controller for the abacus FX UI, responsible
|
* The controller for the abacus FX UI, responsible
|
||||||
@@ -43,14 +40,6 @@ public class AbacusController {
|
|||||||
private TextField inputField;
|
private TextField inputField;
|
||||||
@FXML
|
@FXML
|
||||||
private Button inputButton;
|
private Button inputButton;
|
||||||
@FXML
|
|
||||||
private ComboBox<String> numberImplementationBox;
|
|
||||||
@FXML
|
|
||||||
private Button loadButton;
|
|
||||||
@FXML
|
|
||||||
private Button unloadButton;
|
|
||||||
@FXML
|
|
||||||
private TextField loadField;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of history entries, created by the users.
|
* The list of history entries, created by the users.
|
||||||
@@ -61,8 +50,6 @@ public class AbacusController {
|
|||||||
* The abacus instance used for calculations and all
|
* The abacus instance used for calculations and all
|
||||||
* other main processing code.
|
* other main processing code.
|
||||||
*/
|
*/
|
||||||
private ObservableList<String> numberImplementationOptions;
|
|
||||||
|
|
||||||
private Abacus abacus;
|
private Abacus abacus;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@@ -70,15 +57,9 @@ public class AbacusController {
|
|||||||
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
||||||
param -> new CopyableCell<>();
|
param -> new CopyableCell<>();
|
||||||
|
|
||||||
|
abacus = new Abacus();
|
||||||
historyData = FXCollections.observableArrayList();
|
historyData = FXCollections.observableArrayList();
|
||||||
historyTable.setItems(historyData);
|
historyTable.setItems(historyData);
|
||||||
numberImplementationOptions = FXCollections.observableArrayList();
|
|
||||||
numberImplementationBox.setItems(numberImplementationOptions);
|
|
||||||
numberImplementationBox.valueProperty().addListener((observable, oldValue, newValue)
|
|
||||||
-> {
|
|
||||||
abacus.getConfiguration().setNumberImplementation(newValue);
|
|
||||||
abacus.getConfiguration().saveTo(Abacus.CONFIG_FILE);
|
|
||||||
});
|
|
||||||
historyTable.getSelectionModel().setCellSelectionEnabled(true);
|
historyTable.getSelectionModel().setCellSelectionEnabled(true);
|
||||||
inputColumn.setCellFactory(cellFactory);
|
inputColumn.setCellFactory(cellFactory);
|
||||||
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty());
|
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty());
|
||||||
@@ -86,12 +67,6 @@ public class AbacusController {
|
|||||||
parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty());
|
parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty());
|
||||||
outputColumn.setCellFactory(cellFactory);
|
outputColumn.setCellFactory(cellFactory);
|
||||||
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty());
|
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty());
|
||||||
|
|
||||||
abacus = new Abacus();
|
|
||||||
numberImplementationOptions.addAll(abacus.getPluginManager().getAllNumbers());
|
|
||||||
String actualImplementation = abacus.getConfiguration().getNumberImplementation();
|
|
||||||
String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "naive";
|
|
||||||
numberImplementationBox.getSelectionModel().select(toSelect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@@ -115,38 +90,5 @@ public class AbacusController {
|
|||||||
inputButton.setDisable(false);
|
inputButton.setDisable(false);
|
||||||
inputField.setText("");
|
inputField.setText("");
|
||||||
}
|
}
|
||||||
@FXML
|
|
||||||
private void loadClass(){
|
|
||||||
try {
|
|
||||||
for(Class<?> plugin :ClassFinder.loadJars("plugins")){
|
|
||||||
String name = "";
|
|
||||||
//String name = plugin.getName();
|
|
||||||
while(!(name.indexOf('/') ==-1)){
|
|
||||||
name=name.substring(name.indexOf('/')+1);
|
|
||||||
}
|
|
||||||
if(loadField.getText().equals("")||loadField.getText().equals(name)||(loadField.getText()+".class").equals(name)){
|
|
||||||
//abacus.loadClass(plugin);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@FXML
|
|
||||||
private void unloadClass(){
|
|
||||||
try {
|
|
||||||
for(Class<?> plugin :ClassFinder.loadJars("plugins")){
|
|
||||||
String name = plugin.getName();
|
|
||||||
while(!(name.indexOf('/') ==-1)){
|
|
||||||
name=name.substring(name.indexOf('/')+1);
|
|
||||||
}
|
|
||||||
if(loadField.getText().equals("")||loadField.getText().equals(name)||(loadField.getText()+".class").equals(name)){
|
|
||||||
//abacus.unloadClass(plugin);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
162
src/main/java/org/nwapw/abacus/number/BinaryNumber.java
Normal file
162
src/main/java/org/nwapw/abacus/number/BinaryNumber.java
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
package org.nwapw.abacus.number;
|
||||||
|
|
||||||
|
public class BinaryNumber implements NumberInterface{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number zero.
|
||||||
|
*/
|
||||||
|
public static final BinaryNumber ZERO = new BinaryNumber(0);
|
||||||
|
/**
|
||||||
|
* The number one.
|
||||||
|
*/
|
||||||
|
public static final BinaryNumber ONE = new BinaryNumber(1);
|
||||||
|
/**
|
||||||
|
* The value of this number.
|
||||||
|
*/
|
||||||
|
private double value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new BinaryNumber with the given string.
|
||||||
|
*
|
||||||
|
* @param value the value, which will be parsed as a double.
|
||||||
|
*/
|
||||||
|
public BinaryNumber(String value) {
|
||||||
|
toStandard(value);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private void toStandard(String value) {
|
||||||
|
String before;
|
||||||
|
String after = "";
|
||||||
|
if(value.indexOf(".")==-1){
|
||||||
|
before=value;
|
||||||
|
}else{
|
||||||
|
before = value.substring(0,value.indexOf("."));
|
||||||
|
after = value.substring(value.indexOf(".")+1);
|
||||||
|
}
|
||||||
|
double sum = 0;
|
||||||
|
for(int it=0;before.length()>0;it++){
|
||||||
|
if(before.charAt(before.length()-1)=='1'){
|
||||||
|
sum+=Math.pow(2,it);
|
||||||
|
}
|
||||||
|
if(before.length()>1) {
|
||||||
|
before = before.substring(0, before.length()-1);
|
||||||
|
}else{
|
||||||
|
before = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int it=-1;after.length()>0;it--) {
|
||||||
|
if (after.charAt(0) == '1') {
|
||||||
|
sum += Math.pow(2, it);
|
||||||
|
}
|
||||||
|
if (after.length() > 1) {
|
||||||
|
after = after.substring(1);
|
||||||
|
} else {
|
||||||
|
after = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.value = sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new BinaryNumber with the given value.
|
||||||
|
*
|
||||||
|
* @param value the value to use.
|
||||||
|
*/
|
||||||
|
public BinaryNumber(double value) {
|
||||||
|
toStandard(""+value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMaxPrecision() {
|
||||||
|
return 18;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface multiply(NumberInterface multiplier) {
|
||||||
|
return new BinaryNumber(value * ((BinaryNumber) multiplier).value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface divide(NumberInterface divisor) {
|
||||||
|
return new BinaryNumber(value / ((BinaryNumber) divisor).value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface add(NumberInterface summand) {
|
||||||
|
return new BinaryNumber(value + ((BinaryNumber) summand).value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface subtract(NumberInterface subtrahend) {
|
||||||
|
return new BinaryNumber(value - ((BinaryNumber) subtrahend).value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface negate() {
|
||||||
|
return new BinaryNumber(-value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface intPow(int exponent) {
|
||||||
|
if (exponent == 0) {
|
||||||
|
return BinaryNumber.ONE;
|
||||||
|
}
|
||||||
|
boolean takeReciprocal = exponent < 0;
|
||||||
|
exponent = Math.abs(exponent);
|
||||||
|
NumberInterface power = this;
|
||||||
|
for (int currentExponent = 1; currentExponent < exponent; currentExponent++) {
|
||||||
|
power = power.multiply(this);
|
||||||
|
}
|
||||||
|
if (takeReciprocal) {
|
||||||
|
power = BinaryNumber.ONE.divide(power);
|
||||||
|
}
|
||||||
|
return power;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(NumberInterface number) {
|
||||||
|
BinaryNumber num = (BinaryNumber) number;
|
||||||
|
return Double.compare(value, num.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int signum() {
|
||||||
|
return this.compareTo(ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||||
|
if (toClass == this.getClass()) return this;
|
||||||
|
else if (toClass == PreciseNumber.class) {
|
||||||
|
return new PreciseNumber(Double.toString(value));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
double sum = 0;
|
||||||
|
double tempValue = Math.floor(value);
|
||||||
|
double fraction = value-tempValue;
|
||||||
|
for (int it=0;tempValue > 0;it++) {
|
||||||
|
|
||||||
|
if (tempValue % 2 == 1) {
|
||||||
|
sum+=Math.pow(10,it);
|
||||||
|
tempValue-=1;
|
||||||
|
}
|
||||||
|
tempValue=tempValue/2;
|
||||||
|
}
|
||||||
|
for (int it=0;fraction > 0;it--) {
|
||||||
|
|
||||||
|
if (fraction % 2 >= 1) {
|
||||||
|
sum+=Math.pow(10,it);
|
||||||
|
fraction-=1;
|
||||||
|
}
|
||||||
|
fraction=fraction*2;
|
||||||
|
}
|
||||||
|
double shiftBy = Math.pow(10, 10);
|
||||||
|
return Double.toString(Math.round(sum * shiftBy) / shiftBy);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -140,11 +140,6 @@ public class PluginManager {
|
|||||||
plugins.add(plugin);
|
plugins.add(plugin);
|
||||||
loadedPluginClasses.add(plugin.getClass());
|
loadedPluginClasses.add(plugin.getClass());
|
||||||
}
|
}
|
||||||
public void removeInstantiated(Plugin plugin){
|
|
||||||
if (loadedPluginClasses.contains(plugin.getClass())) return;
|
|
||||||
plugins.remove(plugin);
|
|
||||||
loadedPluginClasses.remove(plugin.getClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a class of plugin, and adds it to this
|
* Instantiates a class of plugin, and adds it to this
|
||||||
@@ -160,14 +155,6 @@ public class PluginManager {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void removeClass(Class<?> newClass){
|
|
||||||
if (!Plugin.class.isAssignableFrom(newClass) || newClass == Plugin.class) return;
|
|
||||||
try {
|
|
||||||
removeInstantiated((Plugin) newClass.getConstructor(PluginManager.class).newInstance(this));
|
|
||||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads all the plugins in the PluginManager.
|
* Loads all the plugins in the PluginManager.
|
||||||
|
|||||||
@@ -5,8 +5,6 @@
|
|||||||
<?import javafx.scene.layout.BorderPane?>
|
<?import javafx.scene.layout.BorderPane?>
|
||||||
<?import javafx.scene.layout.VBox?>
|
<?import javafx.scene.layout.VBox?>
|
||||||
<?import javafx.scene.text.Text?>
|
<?import javafx.scene.text.Text?>
|
||||||
<?import javafx.scene.layout.HBox?>
|
|
||||||
<?import javafx.scene.layout.GridPane?>
|
|
||||||
<BorderPane xmlns="http://javafx.com/javafx"
|
<BorderPane xmlns="http://javafx.com/javafx"
|
||||||
xmlns:fx="http://javafx.com/fxml"
|
xmlns:fx="http://javafx.com/fxml"
|
||||||
fx:controller="org.nwapw.abacus.fx.AbacusController">
|
fx:controller="org.nwapw.abacus.fx.AbacusController">
|
||||||
@@ -41,16 +39,7 @@
|
|||||||
</bottom>
|
</bottom>
|
||||||
</BorderPane>
|
</BorderPane>
|
||||||
</Tab>
|
</Tab>
|
||||||
<Tab text="Settings" closable="false">
|
<Tab text="Settings" closable="false"/>
|
||||||
<GridPane hgap="10" vgap="10">
|
|
||||||
<padding><Insets left="10" right="10" top="10" bottom="10"/></padding>
|
|
||||||
<Label text="Number Implementation" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
|
|
||||||
<ComboBox fx:id="numberImplementationBox" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
|
|
||||||
<Button fx:id="loadButton" text="Load" GridPane.rowIndex="1" GridPane.columnIndex="0" maxWidth = "Infinity" onAction="#loadClass"/>
|
|
||||||
<Button fx:id="unloadButton" text="Unload" GridPane.rowIndex="1" GridPane.columnIndex="1" maxWidth = "Infinity" onAction="#unloadClass"/>
|
|
||||||
<TextField fx:id="loadField" GridPane.rowIndex="1" GridPane.columnIndex="2"/>
|
|
||||||
</GridPane>
|
|
||||||
</Tab>
|
|
||||||
</TabPane>
|
</TabPane>
|
||||||
</center>
|
</center>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user