mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-26 00:25:20 +00:00
Compare commits
23 Commits
stoppable
...
binary-num
| Author | SHA1 | Date | |
|---|---|---|---|
| 0be67410ec | |||
| 6f99f07150 | |||
| 2cf41c1029 | |||
| 0cd40b028a | |||
| 7cb04a1222 | |||
| 0a97eeb442 | |||
|
|
1ee8c7d231 | ||
|
|
f97d16c640 | ||
|
|
a0bba03c2c | ||
| 05d0755526 | |||
| 0b97a935bf | |||
| 211e963db0 | |||
| d8145acc8f | |||
| 63b8162a9b | |||
| c655c63233 | |||
| 27ff1a47b5 | |||
| 2941252f7d | |||
| 44ed0199d4 | |||
| 5b582a7dbe | |||
| a02086e791 | |||
|
|
8666e96420 | ||
|
|
fd40e6b297 | ||
|
|
79ccd61af3 |
@@ -1,6 +1,7 @@
|
|||||||
package org.nwapw.abacus;
|
package org.nwapw.abacus;
|
||||||
|
|
||||||
import org.nwapw.abacus.config.ConfigurationObject;
|
import org.nwapw.abacus.config.ConfigurationObject;
|
||||||
|
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;
|
||||||
import org.nwapw.abacus.parsing.LexerTokenizer;
|
import org.nwapw.abacus.parsing.LexerTokenizer;
|
||||||
@@ -11,9 +12,7 @@ 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;
|
||||||
import org.nwapw.abacus.window.Window;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
@@ -53,10 +52,7 @@ public class Abacus {
|
|||||||
* from a string.
|
* from a string.
|
||||||
*/
|
*/
|
||||||
private TreeBuilder treeBuilder;
|
private TreeBuilder treeBuilder;
|
||||||
private Window window;
|
|
||||||
public boolean getStop(){
|
|
||||||
return window.getStop();
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance of the Abacus calculator.
|
* Creates a new instance of the Abacus calculator.
|
||||||
*/
|
*/
|
||||||
@@ -82,13 +78,7 @@ public class Abacus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
AbacusApplication.launch(AbacusApplication.class, args);
|
||||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
||||||
} catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
new Window(new Abacus()).setVisible(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -136,7 +126,7 @@ public class Abacus {
|
|||||||
* @return the resulting tree, null if the tree builder or the produced tree are null.
|
* @return the resulting tree, null if the tree builder or the produced tree are null.
|
||||||
*/
|
*/
|
||||||
public TreeNode parseString(String input) {
|
public TreeNode parseString(String input) {
|
||||||
return treeBuilder.fromString(input,this);
|
return treeBuilder.fromString(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -168,8 +158,4 @@ public class Abacus {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setWindow(Window window) {
|
|
||||||
this.window = window;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
24
src/main/java/org/nwapw/abacus/fx/AbacusApplication.java
Normal file
24
src/main/java/org/nwapw/abacus/fx/AbacusApplication.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package org.nwapw.abacus.fx;
|
||||||
|
|
||||||
|
import javafx.application.Application;
|
||||||
|
import javafx.fxml.FXMLLoader;
|
||||||
|
import javafx.scene.Parent;
|
||||||
|
import javafx.scene.Scene;
|
||||||
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main application class for JavaFX responsible for loading
|
||||||
|
* and displaying the fxml file.
|
||||||
|
*/
|
||||||
|
public class AbacusApplication extends Application {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(Stage primaryStage) throws Exception {
|
||||||
|
Parent parent = FXMLLoader.load(getClass().getResource("/abacus.fxml"));
|
||||||
|
Scene mainScene = new Scene(parent, 320, 480);
|
||||||
|
primaryStage.setScene(mainScene);
|
||||||
|
primaryStage.setTitle("Abacus");
|
||||||
|
primaryStage.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
94
src/main/java/org/nwapw/abacus/fx/AbacusController.java
Normal file
94
src/main/java/org/nwapw/abacus/fx/AbacusController.java
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package org.nwapw.abacus.fx;
|
||||||
|
|
||||||
|
import javafx.collections.FXCollections;
|
||||||
|
import javafx.collections.ObservableList;
|
||||||
|
import javafx.fxml.FXML;
|
||||||
|
import javafx.scene.control.*;
|
||||||
|
import javafx.scene.text.Text;
|
||||||
|
import javafx.util.Callback;
|
||||||
|
import org.nwapw.abacus.Abacus;
|
||||||
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The controller for the abacus FX UI, responsible
|
||||||
|
* for all the user interaction.
|
||||||
|
*/
|
||||||
|
public class AbacusController {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant string that is displayed if the text could not be lexed or parsed.
|
||||||
|
*/
|
||||||
|
private static final String ERR_SYNTAX = "Syntax Error";
|
||||||
|
/**
|
||||||
|
* Constant string that is displayed if the tree could not be reduced.
|
||||||
|
*/
|
||||||
|
private static final String ERR_EVAL = "Evaluation Error";
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private TableView<HistoryModel> historyTable;
|
||||||
|
@FXML
|
||||||
|
private TableColumn<HistoryModel, String> inputColumn;
|
||||||
|
@FXML
|
||||||
|
private TableColumn<HistoryModel, String> parsedColumn;
|
||||||
|
@FXML
|
||||||
|
private TableColumn<HistoryModel, String> outputColumn;
|
||||||
|
@FXML
|
||||||
|
private Text outputText;
|
||||||
|
@FXML
|
||||||
|
private TextField inputField;
|
||||||
|
@FXML
|
||||||
|
private Button inputButton;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The list of history entries, created by the users.
|
||||||
|
*/
|
||||||
|
private ObservableList<HistoryModel> historyData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The abacus instance used for calculations and all
|
||||||
|
* other main processing code.
|
||||||
|
*/
|
||||||
|
private Abacus abacus;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
public void initialize(){
|
||||||
|
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
||||||
|
param -> new CopyableCell<>();
|
||||||
|
|
||||||
|
abacus = new Abacus();
|
||||||
|
historyData = FXCollections.observableArrayList();
|
||||||
|
historyTable.setItems(historyData);
|
||||||
|
historyTable.getSelectionModel().setCellSelectionEnabled(true);
|
||||||
|
inputColumn.setCellFactory(cellFactory);
|
||||||
|
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty());
|
||||||
|
parsedColumn.setCellFactory(cellFactory);
|
||||||
|
parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty());
|
||||||
|
outputColumn.setCellFactory(cellFactory);
|
||||||
|
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private void performCalculation(){
|
||||||
|
inputButton.setDisable(true);
|
||||||
|
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
||||||
|
if(constructedTree == null){
|
||||||
|
outputText.setText(ERR_SYNTAX);
|
||||||
|
inputButton.setDisable(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
|
||||||
|
if(evaluatedNumber == null){
|
||||||
|
outputText.setText(ERR_EVAL);
|
||||||
|
inputButton.setDisable(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
outputText.setText(evaluatedNumber.toString());
|
||||||
|
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
|
||||||
|
|
||||||
|
inputButton.setDisable(false);
|
||||||
|
inputField.setText("");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
35
src/main/java/org/nwapw/abacus/fx/CopyableCell.java
Normal file
35
src/main/java/org/nwapw/abacus/fx/CopyableCell.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package org.nwapw.abacus.fx;
|
||||||
|
|
||||||
|
import javafx.scene.control.TableCell;
|
||||||
|
import javafx.scene.input.MouseEvent;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.datatransfer.StringSelection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A cell that copies its value to the clipboard
|
||||||
|
* when double clicked.
|
||||||
|
* @param <S> The type of the table view generic type.
|
||||||
|
* @param <T> The type of the value contained in the cell.
|
||||||
|
*/
|
||||||
|
public class CopyableCell<S, T> extends TableCell<S, T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new copyable cell.
|
||||||
|
*/
|
||||||
|
public CopyableCell(){
|
||||||
|
addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
|
||||||
|
if(event.getClickCount() == 2){
|
||||||
|
Toolkit.getDefaultToolkit().getSystemClipboard()
|
||||||
|
.setContents(new StringSelection(getText()), null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void updateItem(T item, boolean empty) {
|
||||||
|
super.updateItem(item, empty);
|
||||||
|
setText((empty || item == null) ? null : item.toString());
|
||||||
|
setGraphic(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
87
src/main/java/org/nwapw/abacus/fx/HistoryModel.java
Normal file
87
src/main/java/org/nwapw/abacus/fx/HistoryModel.java
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package org.nwapw.abacus.fx;
|
||||||
|
|
||||||
|
import javafx.beans.property.SimpleStringProperty;
|
||||||
|
import javafx.beans.property.StringProperty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The data model used for storing history entries.
|
||||||
|
*/
|
||||||
|
public class HistoryModel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The property used for displaying the column
|
||||||
|
* for the user input.
|
||||||
|
*/
|
||||||
|
private final StringProperty input;
|
||||||
|
/**
|
||||||
|
* The property used for displaying the column
|
||||||
|
* that contains the parsed input.
|
||||||
|
*/
|
||||||
|
private final StringProperty parsed;
|
||||||
|
/**
|
||||||
|
* The property used for displaying the column
|
||||||
|
* that contains the program output.
|
||||||
|
*/
|
||||||
|
private final StringProperty output;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new history model with the given variables.
|
||||||
|
* @param input the user input
|
||||||
|
* @param parsed the parsed input
|
||||||
|
* @param output the program output.
|
||||||
|
*/
|
||||||
|
public HistoryModel(String input, String parsed, String output){
|
||||||
|
this.input = new SimpleStringProperty();
|
||||||
|
this.parsed = new SimpleStringProperty();
|
||||||
|
this.output = new SimpleStringProperty();
|
||||||
|
this.input.setValue(input);
|
||||||
|
this.parsed.setValue(parsed);
|
||||||
|
this.output.setValue(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the input property.
|
||||||
|
* @return the input property.
|
||||||
|
*/
|
||||||
|
public StringProperty inputProperty() {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Gets the input.
|
||||||
|
* @return the input.
|
||||||
|
*/
|
||||||
|
public String getInput() {
|
||||||
|
return input.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the parsed input property.
|
||||||
|
* @return the parsed input property.
|
||||||
|
*/
|
||||||
|
public StringProperty parsedProperty() {
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Gets the parsed input.
|
||||||
|
* @return the parsed input.
|
||||||
|
*/
|
||||||
|
public String getParsed() {
|
||||||
|
return parsed.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the output property.
|
||||||
|
* @return the output property.
|
||||||
|
*/
|
||||||
|
public StringProperty outputProperty() {
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Gets the program output.
|
||||||
|
* @return the output.
|
||||||
|
*/
|
||||||
|
public String getOutput() {
|
||||||
|
return output.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -93,6 +93,11 @@ public class NaiveNumber implements NumberInterface {
|
|||||||
return this.compareTo(ZERO);
|
return this.compareTo(ZERO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int ceiling() {
|
||||||
|
return (int) Math.ceil(value);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||||
if (toClass == this.getClass()) return this;
|
if (toClass == this.getClass()) return this;
|
||||||
|
|||||||
@@ -79,6 +79,12 @@ public interface NumberInterface {
|
|||||||
*/
|
*/
|
||||||
int signum();
|
int signum();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the least integer greater than or equal to the number.
|
||||||
|
* @return the least integer >= the number, if int can hold the value.
|
||||||
|
*/
|
||||||
|
int ceiling();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Promotes this class to another number class.
|
* Promotes this class to another number class.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -3,6 +3,10 @@ package org.nwapw.abacus.number;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A number that uses a BigDecimal to store its value,
|
||||||
|
* leading to infinite possible precision.
|
||||||
|
*/
|
||||||
public class PreciseNumber implements NumberInterface {
|
public class PreciseNumber implements NumberInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,12 +48,12 @@ public class PreciseNumber implements NumberInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getMaxPrecision() {
|
public int getMaxPrecision() {
|
||||||
return 54;
|
return 65;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface multiply(NumberInterface multiplier) {
|
public NumberInterface multiply(NumberInterface multiplier) {
|
||||||
return new PreciseNumber(value.multiply(((PreciseNumber) multiplier).value));
|
return new PreciseNumber(this.value.multiply(((PreciseNumber) multiplier).value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -94,6 +98,11 @@ public class PreciseNumber implements NumberInterface {
|
|||||||
return value.signum();
|
return value.signum();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int ceiling() {
|
||||||
|
return (int) Math.ceil(value.doubleValue());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface negate() {
|
public NumberInterface negate() {
|
||||||
return new PreciseNumber(value.negate());
|
return new PreciseNumber(value.negate());
|
||||||
@@ -109,7 +118,7 @@ public class PreciseNumber implements NumberInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
BigDecimal rounded = value.setScale(getMaxPrecision() - 4, RoundingMode.HALF_UP);
|
BigDecimal rounded = value.setScale(getMaxPrecision() - 15, RoundingMode.HALF_UP);
|
||||||
return rounded.stripTrailingZeros().toPlainString();
|
return rounded.stripTrailingZeros().toPlainString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.nwapw.abacus.parsing;
|
package org.nwapw.abacus.parsing;
|
||||||
|
|
||||||
import org.nwapw.abacus.Abacus;
|
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -19,5 +18,5 @@ public interface Parser<T> {
|
|||||||
* @param tokens the tokens to construct a tree from.
|
* @param tokens the tokens to construct a tree from.
|
||||||
* @return the constructed tree, or null on error.
|
* @return the constructed tree, or null on error.
|
||||||
*/
|
*/
|
||||||
public TreeNode constructTree(List<T> tokens,Abacus trace);
|
public TreeNode constructTree(List<T> tokens);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,8 +34,6 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
*/
|
*/
|
||||||
private Map<String, OperatorType> typeMap;
|
private Map<String, OperatorType> typeMap;
|
||||||
|
|
||||||
//private Abacus trace;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Shunting Yard parser with the given Abacus instance.
|
* Creates a new Shunting Yard parser with the given Abacus instance.
|
||||||
*
|
*
|
||||||
@@ -118,8 +116,7 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
* @param matches the list of tokens from the source string.
|
* @param matches the list of tokens from the source string.
|
||||||
* @return the construct tree expression.
|
* @return the construct tree expression.
|
||||||
*/
|
*/
|
||||||
public TreeNode constructRecursive(List<Match<TokenType>> matches,Abacus trace) {
|
public TreeNode constructRecursive(List<Match<TokenType>> matches) {
|
||||||
//this.trace = trace;
|
|
||||||
if (matches.size() == 0) return null;
|
if (matches.size() == 0) return null;
|
||||||
Match<TokenType> match = matches.remove(0);
|
Match<TokenType> match = matches.remove(0);
|
||||||
TokenType matchType = match.getType();
|
TokenType matchType = match.getType();
|
||||||
@@ -127,22 +124,22 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
String operator = match.getContent();
|
String operator = match.getContent();
|
||||||
OperatorType type = typeMap.get(operator);
|
OperatorType type = typeMap.get(operator);
|
||||||
if (type == OperatorType.BINARY_INFIX) {
|
if (type == OperatorType.BINARY_INFIX) {
|
||||||
TreeNode right = constructRecursive(matches,trace);
|
TreeNode right = constructRecursive(matches);
|
||||||
TreeNode left = constructRecursive(matches,trace);
|
TreeNode left = constructRecursive(matches);
|
||||||
if (left == null || right == null) return null;
|
if (left == null || right == null) return null;
|
||||||
else return new BinaryInfixNode(operator, left, right,trace);
|
else return new BinaryInfixNode(operator, left, right);
|
||||||
} else {
|
} else {
|
||||||
TreeNode applyTo = constructRecursive(matches,trace);
|
TreeNode applyTo = constructRecursive(matches);
|
||||||
if (applyTo == null) return null;
|
if (applyTo == null) return null;
|
||||||
else return new UnaryPrefixNode(operator, applyTo,trace);
|
else return new UnaryPrefixNode(operator, applyTo);
|
||||||
}
|
}
|
||||||
} else if (matchType == TokenType.NUM) {
|
} else if (matchType == TokenType.NUM) {
|
||||||
return new NumberNode(abacus.numberFromString(match.getContent()));
|
return new NumberNode(abacus.numberFromString(match.getContent()));
|
||||||
} else if (matchType == TokenType.FUNCTION) {
|
} else if (matchType == TokenType.FUNCTION) {
|
||||||
String functionName = match.getContent();
|
String functionName = match.getContent();
|
||||||
FunctionNode node = new FunctionNode(functionName,trace);
|
FunctionNode node = new FunctionNode(functionName);
|
||||||
while (!matches.isEmpty() && matches.get(0).getType() != TokenType.INTERNAL_FUNCTION_END) {
|
while (!matches.isEmpty() && matches.get(0).getType() != TokenType.INTERNAL_FUNCTION_END) {
|
||||||
TreeNode argument = constructRecursive(matches,trace);
|
TreeNode argument = constructRecursive(matches);
|
||||||
if (argument == null) return null;
|
if (argument == null) return null;
|
||||||
node.prependChild(argument);
|
node.prependChild(argument);
|
||||||
}
|
}
|
||||||
@@ -154,10 +151,10 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TreeNode constructTree(List<Match<TokenType>> tokens,Abacus trace) {
|
public TreeNode constructTree(List<Match<TokenType>> tokens) {
|
||||||
tokens = intoPostfix(new ArrayList<>(tokens));
|
tokens = intoPostfix(new ArrayList<>(tokens));
|
||||||
Collections.reverse(tokens);
|
Collections.reverse(tokens);
|
||||||
return constructRecursive(tokens,trace);
|
return constructRecursive(tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.nwapw.abacus.parsing;
|
package org.nwapw.abacus.parsing;
|
||||||
|
|
||||||
import org.nwapw.abacus.Abacus;
|
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -42,10 +41,10 @@ public class TreeBuilder<T> {
|
|||||||
* @param input the string to parse into a tree.
|
* @param input the string to parse into a tree.
|
||||||
* @return the resulting tree.
|
* @return the resulting tree.
|
||||||
*/
|
*/
|
||||||
public TreeNode fromString(String input,Abacus trace) {
|
public TreeNode fromString(String input) {
|
||||||
List<T> tokens = tokenizer.tokenizeString(input);
|
List<T> tokens = tokenizer.tokenizeString(input);
|
||||||
if (tokens == null) return null;
|
if (tokens == null) return null;
|
||||||
return parser.constructTree(tokens,trace);
|
return parser.constructTree(tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import org.nwapw.abacus.number.NaiveNumber;
|
|||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
import org.nwapw.abacus.number.PreciseNumber;
|
import org.nwapw.abacus.number.PreciseNumber;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,6 +18,8 @@ import java.util.function.BiFunction;
|
|||||||
*/
|
*/
|
||||||
public class StandardPlugin extends Plugin {
|
public class StandardPlugin extends Plugin {
|
||||||
|
|
||||||
|
private static HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> factorialLists = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The addition operator, +
|
* The addition operator, +
|
||||||
*/
|
*/
|
||||||
@@ -152,17 +156,40 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
boolean takeReciprocal = params[0].signum() == -1;
|
NumberInterface maxError = getMaxError(params[0]);
|
||||||
params[0] = FUNCTION_ABS.apply(params[0]);
|
int n = 0;
|
||||||
NumberInterface sum = sumSeries(params[0], StandardPlugin::getExpSeriesTerm, getNTermsExp(getMaxError(params[0]), params[0]));
|
if(params[0].signum() <= 0){
|
||||||
if (takeReciprocal) {
|
NumberInterface currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm;
|
||||||
sum = NaiveNumber.ONE.promoteTo(sum.getClass()).divide(sum);
|
while(FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0){
|
||||||
|
n++;
|
||||||
|
currentTerm = currentTerm.multiply(params[0]).divide((new NaiveNumber(n)).promoteTo(params[0].getClass()));
|
||||||
|
sum = sum.add(currentTerm);
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
//We need n such that x^(n+1) * 3^ceil(x) <= maxError * (n+1)!.
|
||||||
|
//right and left refer to lhs and rhs in the above inequality.
|
||||||
|
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
||||||
|
NumberInterface nextNumerator = params[0];
|
||||||
|
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling())), right = maxError;
|
||||||
|
do{
|
||||||
|
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1)));
|
||||||
|
n++;
|
||||||
|
nextNumerator = nextNumerator.multiply(params[0]);
|
||||||
|
left = left.multiply(params[0]);
|
||||||
|
NumberInterface nextN = (new NaiveNumber(n+1)).promoteTo(params[0].getClass());
|
||||||
|
right = right.multiply(nextN);
|
||||||
|
//System.out.println(left + ", " + right);
|
||||||
|
}
|
||||||
|
while(left.compareTo(right) > 0);
|
||||||
|
//System.out.println(n+1);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* The natural log function, ln(exp(1)) = 1
|
* The natural log function.
|
||||||
*/
|
*/
|
||||||
public static final Function FUNCTION_LN = new Function() {
|
public static final Function FUNCTION_LN = new Function() {
|
||||||
@Override
|
@Override
|
||||||
@@ -239,7 +266,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* The square root function, sqrt(4) = 2
|
* The square root function.
|
||||||
*/
|
*/
|
||||||
public static final Function FUNCTION_SQRT = new Function() {
|
public static final Function FUNCTION_SQRT = new Function() {
|
||||||
@Override
|
@Override
|
||||||
@@ -257,39 +284,6 @@ public class StandardPlugin extends Plugin {
|
|||||||
super(manager);
|
super(manager);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the nth term of the Taylor series (centered at 0) of e^x
|
|
||||||
*
|
|
||||||
* @param n the term required (n >= 0).
|
|
||||||
* @param x the real number at which the series is evaluated.
|
|
||||||
* @return the nth term of the series.
|
|
||||||
*/
|
|
||||||
private static NumberInterface getExpSeriesTerm(int n, NumberInterface x) {
|
|
||||||
return x.intPow(n).divide(OP_FACTORIAL.getFunction().apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the number of terms needed to evaluate the exponential function (at x)
|
|
||||||
* such that the error is at most maxError.
|
|
||||||
*
|
|
||||||
* @param maxError Maximum error permissible (This should probably be positive.)
|
|
||||||
* @param x where the function is evaluated.
|
|
||||||
* @return the number of terms needed to evaluate the exponential function.
|
|
||||||
*/
|
|
||||||
private static int getNTermsExp(NumberInterface maxError, NumberInterface x) {
|
|
||||||
//We need n such that |x^(n+1)| <= (n+1)! * maxError
|
|
||||||
//The variables LHS and RHS refer to the above inequality.
|
|
||||||
int n = 0;
|
|
||||||
x = FUNCTION_ABS.apply(x);
|
|
||||||
NumberInterface LHS = x, RHS = maxError;
|
|
||||||
while (LHS.compareTo(RHS) > 0) {
|
|
||||||
n++;
|
|
||||||
LHS = LHS.multiply(x);
|
|
||||||
RHS = RHS.multiply(new NaiveNumber(n + 1).promoteTo(RHS.getClass()));
|
|
||||||
}
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a partial sum of a series whose terms are given by the nthTermFunction, evaluated at x.
|
* Returns a partial sum of a series whose terms are given by the nthTermFunction, evaluated at x.
|
||||||
*
|
*
|
||||||
@@ -339,4 +333,19 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
||||||
|
if(!factorialLists.containsKey(numberClass)){
|
||||||
|
factorialLists.put(numberClass, new ArrayList<NumberInterface>());
|
||||||
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
|
}
|
||||||
|
ArrayList<NumberInterface> list = factorialLists.get(numberClass);
|
||||||
|
if(n >= list.size()){
|
||||||
|
while(list.size() < n + 16){
|
||||||
|
list.add(list.get(list.size()-1).multiply(new NaiveNumber(list.size()).promoteTo(numberClass)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list.get(n);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
import org.nwapw.abacus.Abacus;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A tree node that represents an operation being applied to two operands.
|
* A tree node that represents an operation being applied to two operands.
|
||||||
*/
|
*/
|
||||||
@@ -19,7 +17,6 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
* The right node of the operation.
|
* The right node of the operation.
|
||||||
*/
|
*/
|
||||||
private TreeNode right;
|
private TreeNode right;
|
||||||
private Abacus trace;
|
|
||||||
|
|
||||||
private BinaryInfixNode() {
|
private BinaryInfixNode() {
|
||||||
}
|
}
|
||||||
@@ -30,8 +27,8 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
*
|
*
|
||||||
* @param operation the operation.
|
* @param operation the operation.
|
||||||
*/
|
*/
|
||||||
public BinaryInfixNode(String operation,Abacus trace) {
|
public BinaryInfixNode(String operation) {
|
||||||
this(operation, null, null,trace);
|
this(operation, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,11 +39,10 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
* @param left the left node of the expression.
|
* @param left the left node of the expression.
|
||||||
* @param right the right node of the expression.
|
* @param right the right node of the expression.
|
||||||
*/
|
*/
|
||||||
public BinaryInfixNode(String operation, TreeNode left, TreeNode right,Abacus trace) {
|
public BinaryInfixNode(String operation, TreeNode left, TreeNode right) {
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
this.trace = trace;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -96,16 +92,11 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
public <T> T reduce(Reducer<T> reducer) {
|
||||||
if(!trace.getStop()) {
|
|
||||||
T leftReduce = left.reduce(reducer);
|
T leftReduce = left.reduce(reducer);
|
||||||
|
|
||||||
T rightReduce = right.reduce(reducer);
|
T rightReduce = right.reduce(reducer);
|
||||||
if (leftReduce == null || rightReduce == null) return null;
|
if (leftReduce == null || rightReduce == null) return null;
|
||||||
return reducer.reduceNode(this, leftReduce, rightReduce);
|
return reducer.reduceNode(this, leftReduce, rightReduce);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
import org.nwapw.abacus.Abacus;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -18,7 +16,6 @@ public class FunctionNode extends TreeNode {
|
|||||||
* The list of arguments to the function.
|
* The list of arguments to the function.
|
||||||
*/
|
*/
|
||||||
private List<TreeNode> children;
|
private List<TreeNode> children;
|
||||||
private Abacus trace;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a function node with no function.
|
* Creates a function node with no function.
|
||||||
@@ -31,10 +28,9 @@ public class FunctionNode extends TreeNode {
|
|||||||
*
|
*
|
||||||
* @param function the function name.
|
* @param function the function name.
|
||||||
*/
|
*/
|
||||||
public FunctionNode(String function,Abacus trace) {
|
public FunctionNode(String function) {
|
||||||
this.function = function;
|
this.function = function;
|
||||||
children = new ArrayList<>();
|
children = new ArrayList<>();
|
||||||
this.trace = trace;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
import org.nwapw.abacus.Abacus;
|
|
||||||
|
|
||||||
public class UnaryPrefixNode extends TreeNode {
|
public class UnaryPrefixNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,15 +10,14 @@ public class UnaryPrefixNode extends TreeNode {
|
|||||||
* The tree node to apply the operation to.
|
* The tree node to apply the operation to.
|
||||||
*/
|
*/
|
||||||
private TreeNode applyTo;
|
private TreeNode applyTo;
|
||||||
private Abacus trace;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new node with the given operation and no child.
|
* Creates a new node with the given operation and no child.
|
||||||
*
|
*
|
||||||
* @param operation the operation for this node.
|
* @param operation the operation for this node.
|
||||||
*/
|
*/
|
||||||
public UnaryPrefixNode(String operation,Abacus trace) {
|
public UnaryPrefixNode(String operation) {
|
||||||
this(operation, null,trace);
|
this(operation, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,21 +26,17 @@ public class UnaryPrefixNode extends TreeNode {
|
|||||||
* @param operation the operation for this node.
|
* @param operation the operation for this node.
|
||||||
* @param applyTo the node to apply the function to.
|
* @param applyTo the node to apply the function to.
|
||||||
*/
|
*/
|
||||||
public UnaryPrefixNode(String operation, TreeNode applyTo,Abacus trace) {
|
public UnaryPrefixNode(String operation, TreeNode applyTo) {
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.applyTo = applyTo;
|
this.applyTo = applyTo;
|
||||||
this.trace = trace;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
public <T> T reduce(Reducer<T> reducer) {
|
||||||
if(!trace.getStop()) {
|
|
||||||
Object reducedChild = applyTo.reduce(reducer);
|
Object reducedChild = applyTo.reduce(reducer);
|
||||||
if (reducedChild == null) return null;
|
if (reducedChild == null) return null;
|
||||||
return reducer.reduceNode(this, reducedChild);
|
return reducer.reduceNode(this, reducedChild);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the operation of this node.
|
* Gets the operation of this node.
|
||||||
|
|||||||
@@ -16,13 +16,12 @@ import java.awt.event.MouseEvent;
|
|||||||
*/
|
*/
|
||||||
public class Window extends JFrame {
|
public class Window extends JFrame {
|
||||||
|
|
||||||
|
|
||||||
private static final String CALC_STRING = "Calculate";
|
private static final String CALC_STRING = "Calculate";
|
||||||
private static final String SYNTAX_ERR_STRING = "Syntax Error";
|
private static final String SYNTAX_ERR_STRING = "Syntax Error";
|
||||||
private static final String EVAL_ERR_STRING = "Evaluation Error";
|
private static final String EVAL_ERR_STRING = "Evaluation Error";
|
||||||
private static final String NUMBER_SYSTEM_LABEL = "Number Type:";
|
private static final String NUMBER_SYSTEM_LABEL = "Number Type:";
|
||||||
private static final String FUNCTION_LABEL = "Functions:";
|
private static final String FUNCTION_LABEL = "Functions:";
|
||||||
private static final String STOP_STRING = "Stop";
|
|
||||||
/**
|
/**
|
||||||
* Array of Strings to which the "calculate" button's text
|
* Array of Strings to which the "calculate" button's text
|
||||||
* changes. For instance, in the graph tab, the name will
|
* changes. For instance, in the graph tab, the name will
|
||||||
@@ -91,10 +90,7 @@ public class Window extends JFrame {
|
|||||||
* The "submit" button.
|
* The "submit" button.
|
||||||
*/
|
*/
|
||||||
private JButton inputEnterButton;
|
private JButton inputEnterButton;
|
||||||
/**
|
|
||||||
* The stop calculations button.
|
|
||||||
*/
|
|
||||||
private JButton inputStopButton;
|
|
||||||
/**
|
/**
|
||||||
* The side panel for separate configuration.
|
* The side panel for separate configuration.
|
||||||
*/
|
*/
|
||||||
@@ -117,80 +113,26 @@ public class Window extends JFrame {
|
|||||||
* The list of functions available to the user.
|
* The list of functions available to the user.
|
||||||
*/
|
*/
|
||||||
private JComboBox<String> functionList;
|
private JComboBox<String> functionList;
|
||||||
/**
|
|
||||||
* Thread used for calculations
|
|
||||||
*/
|
|
||||||
private Thread calculateThread;
|
|
||||||
/**
|
|
||||||
* Check if currently calculating.
|
|
||||||
*/
|
|
||||||
private boolean calculating;
|
|
||||||
/**
|
|
||||||
* Checks if thread should stop calculating.
|
|
||||||
*/
|
|
||||||
private boolean stopCalculating;
|
|
||||||
/**
|
|
||||||
* Test variable to check number of threads running in the calculator.
|
|
||||||
*/
|
|
||||||
private int count;
|
|
||||||
/**
|
|
||||||
* ActionListener that stops calculations.
|
|
||||||
*/
|
|
||||||
private ActionListener stopListener = (event) -> {
|
|
||||||
//System.out.println(count++); //Shows about how many calculation threads are running with the calculating=false command
|
|
||||||
//calculating = false; //Ignores result of calculation and allows for the start of a new calculation
|
|
||||||
stopCalculating = true; //Stops calculation at the next node check
|
|
||||||
//calculateThread.stop(); //Stops thread no matter what, Unsafe
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Node check to get whether nodes should continue calculating.
|
* Action listener that causes the input to be evaluated.
|
||||||
* @return boolean stop calculations if true.
|
|
||||||
*/
|
|
||||||
public boolean getStop(){
|
|
||||||
return stopCalculating;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ActionListener that runs all calculations.
|
|
||||||
*/
|
*/
|
||||||
private ActionListener evaluateListener = (event) -> {
|
private ActionListener evaluateListener = (event) -> {
|
||||||
Runnable calculate = new Runnable() {
|
TreeNode parsedExpression = abacus.parseString(inputField.getText());
|
||||||
public void run() {
|
|
||||||
boolean skip = false;
|
|
||||||
calculating = true;
|
|
||||||
TreeNode parsedExpression = null;
|
|
||||||
parsedExpression = abacus.parseString(inputField.getText());
|
|
||||||
if (parsedExpression == null) {
|
if (parsedExpression == null) {
|
||||||
lastOutputArea.setText(SYNTAX_ERR_STRING);
|
lastOutputArea.setText(SYNTAX_ERR_STRING);
|
||||||
skip = true;
|
return;
|
||||||
}
|
}
|
||||||
if(!skip){
|
|
||||||
NumberInterface numberInterface = abacus.evaluateTree(parsedExpression);
|
NumberInterface numberInterface = abacus.evaluateTree(parsedExpression);
|
||||||
if (numberInterface == null) {
|
if (numberInterface == null) {
|
||||||
lastOutputArea.setText(EVAL_ERR_STRING);
|
lastOutputArea.setText(EVAL_ERR_STRING);
|
||||||
calculating = false;
|
|
||||||
stopCalculating = false;
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(calculateThread.equals(Thread.currentThread())) {
|
|
||||||
lastOutput = numberInterface.toString();
|
lastOutput = numberInterface.toString();
|
||||||
historyModel.addEntry(new HistoryTableModel.HistoryEntry(inputField.getText(), parsedExpression, lastOutput));
|
historyModel.addEntry(new HistoryTableModel.HistoryEntry(inputField.getText(), parsedExpression, lastOutput));
|
||||||
historyTable.invalidate();
|
historyTable.invalidate();
|
||||||
lastOutputArea.setText(lastOutput);
|
lastOutputArea.setText(lastOutput);
|
||||||
inputField.setText("");
|
inputField.setText("");
|
||||||
calculating = false;
|
|
||||||
stopCalculating = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if(!calculating) {
|
|
||||||
calculateThread = new Thread(calculate);
|
|
||||||
calculateThread.setName("a-"+System.currentTimeMillis());
|
|
||||||
calculateThread.start();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -201,14 +143,6 @@ public class Window extends JFrame {
|
|||||||
evaluateListener,
|
evaluateListener,
|
||||||
null
|
null
|
||||||
};
|
};
|
||||||
/**
|
|
||||||
* Array of listeners that tell the stop button how to behave
|
|
||||||
* at a given input tab.
|
|
||||||
*/
|
|
||||||
private ActionListener[] stopListeners = {
|
|
||||||
stopListener,
|
|
||||||
null
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new window with the given manager.
|
* Creates a new window with the given manager.
|
||||||
@@ -218,7 +152,6 @@ public class Window extends JFrame {
|
|||||||
public Window(Abacus abacus) {
|
public Window(Abacus abacus) {
|
||||||
this();
|
this();
|
||||||
this.abacus = abacus;
|
this.abacus = abacus;
|
||||||
abacus.setWindow(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -227,36 +160,25 @@ public class Window extends JFrame {
|
|||||||
private Window() {
|
private Window() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
//variables related to when calculations occur
|
|
||||||
stopCalculating = false;
|
|
||||||
calculating = true;
|
|
||||||
|
|
||||||
lastOutput = "";
|
lastOutput = "";
|
||||||
|
|
||||||
//default window properties
|
|
||||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
setSize(320, 480);
|
setSize(320, 480);
|
||||||
|
|
||||||
//initiates input objects
|
|
||||||
inputField = new JTextField();
|
inputField = new JTextField();
|
||||||
inputEnterButton = new JButton(CALC_STRING);
|
inputEnterButton = new JButton(CALC_STRING);
|
||||||
inputStopButton = new JButton(STOP_STRING);
|
|
||||||
|
|
||||||
//adds input objects into Panel
|
|
||||||
inputPanel = new JPanel();
|
inputPanel = new JPanel();
|
||||||
inputPanel.setLayout(new BorderLayout());
|
inputPanel.setLayout(new BorderLayout());
|
||||||
inputPanel.add(inputStopButton, BorderLayout.SOUTH);
|
inputPanel.add(inputField, BorderLayout.CENTER);
|
||||||
inputPanel.add(inputField, BorderLayout.NORTH);
|
inputPanel.add(inputEnterButton, BorderLayout.SOUTH);
|
||||||
inputPanel.add(inputEnterButton, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
//initiates output objects in calculator tab
|
|
||||||
historyModel = new HistoryTableModel();
|
historyModel = new HistoryTableModel();
|
||||||
historyTable = new JTable(historyModel);
|
historyTable = new JTable(historyModel);
|
||||||
historyScroll = new JScrollPane(historyTable);
|
historyScroll = new JScrollPane(historyTable);
|
||||||
lastOutputArea = new JTextArea(lastOutput);
|
lastOutputArea = new JTextArea(lastOutput);
|
||||||
lastOutputArea.setEditable(false);
|
lastOutputArea.setEditable(false);
|
||||||
|
|
||||||
//adds output objects into Panel
|
|
||||||
calculationPanel = new JPanel();
|
calculationPanel = new JPanel();
|
||||||
calculationPanel.setLayout(new BorderLayout());
|
calculationPanel.setLayout(new BorderLayout());
|
||||||
calculationPanel.add(historyScroll, BorderLayout.CENTER);
|
calculationPanel.add(historyScroll, BorderLayout.CENTER);
|
||||||
@@ -286,8 +208,6 @@ public class Window extends JFrame {
|
|||||||
settingsPanel.add(numberSystemPanel);
|
settingsPanel.add(numberSystemPanel);
|
||||||
settingsPanel.add(functionSelectPanel);
|
settingsPanel.add(functionSelectPanel);
|
||||||
|
|
||||||
calculating = false;
|
|
||||||
|
|
||||||
pane = new JTabbedPane();
|
pane = new JTabbedPane();
|
||||||
pane.add("Calculator", calculationPanel);
|
pane.add("Calculator", calculationPanel);
|
||||||
pane.add("Settings", settingsPanel);
|
pane.add("Settings", settingsPanel);
|
||||||
@@ -295,23 +215,17 @@ public class Window extends JFrame {
|
|||||||
int selectionIndex = pane.getSelectedIndex();
|
int selectionIndex = pane.getSelectedIndex();
|
||||||
boolean enabled = INPUT_ENABLED[selectionIndex];
|
boolean enabled = INPUT_ENABLED[selectionIndex];
|
||||||
ActionListener listener = listeners[selectionIndex];
|
ActionListener listener = listeners[selectionIndex];
|
||||||
ActionListener stopUsed = stopListeners[selectionIndex];
|
|
||||||
inputEnterButton.setText(BUTTON_NAMES[selectionIndex]);
|
inputEnterButton.setText(BUTTON_NAMES[selectionIndex]);
|
||||||
inputField.setEnabled(enabled);
|
inputField.setEnabled(enabled);
|
||||||
inputEnterButton.setEnabled(enabled);
|
inputEnterButton.setEnabled(enabled);
|
||||||
inputStopButton.setEnabled(enabled);
|
|
||||||
|
|
||||||
for (ActionListener removingListener : inputEnterButton.getActionListeners()) {
|
for (ActionListener removingListener : inputEnterButton.getActionListeners()) {
|
||||||
inputEnterButton.removeActionListener(removingListener);
|
inputEnterButton.removeActionListener(removingListener);
|
||||||
inputField.removeActionListener(removingListener);
|
inputField.removeActionListener(removingListener);
|
||||||
}
|
}
|
||||||
for(ActionListener removingListener : inputStopButton.getActionListeners()){
|
|
||||||
inputStopButton.removeActionListener(removingListener);
|
|
||||||
}
|
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
inputEnterButton.addActionListener(listener);
|
inputEnterButton.addActionListener(listener);
|
||||||
inputField.addActionListener(listener);
|
inputField.addActionListener(listener);
|
||||||
inputStopButton.addActionListener(stopUsed);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
add(pane, BorderLayout.CENTER);
|
add(pane, BorderLayout.CENTER);
|
||||||
@@ -319,7 +233,6 @@ public class Window extends JFrame {
|
|||||||
|
|
||||||
inputEnterButton.addActionListener(evaluateListener);
|
inputEnterButton.addActionListener(evaluateListener);
|
||||||
inputField.addActionListener(evaluateListener);
|
inputField.addActionListener(evaluateListener);
|
||||||
inputStopButton.addActionListener(stopListener);
|
|
||||||
historyTable.addMouseListener(new MouseAdapter() {
|
historyTable.addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
|||||||
46
src/main/resources/abacus.fxml
Normal file
46
src/main/resources/abacus.fxml
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import javafx.geometry.Insets?>
|
||||||
|
<?import javafx.scene.control.*?>
|
||||||
|
<?import javafx.scene.layout.BorderPane?>
|
||||||
|
<?import javafx.scene.layout.VBox?>
|
||||||
|
<?import javafx.scene.text.Text?>
|
||||||
|
<BorderPane xmlns="http://javafx.com/javafx"
|
||||||
|
xmlns:fx="http://javafx.com/fxml"
|
||||||
|
fx:controller="org.nwapw.abacus.fx.AbacusController">
|
||||||
|
<center>
|
||||||
|
<TabPane>
|
||||||
|
<Tab text="Calculator" closable="false">
|
||||||
|
<BorderPane>
|
||||||
|
<center>
|
||||||
|
<TableView fx:id="historyTable">
|
||||||
|
<columnResizePolicy>
|
||||||
|
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
|
||||||
|
</columnResizePolicy>
|
||||||
|
<columns>
|
||||||
|
<TableColumn fx:id="inputColumn" text="Input" sortable="false"/>
|
||||||
|
<TableColumn fx:id="parsedColumn" text="Parsed" sortable="false"/>
|
||||||
|
<TableColumn fx:id="outputColumn" text="Output" sortable="false"/>
|
||||||
|
</columns>
|
||||||
|
</TableView>
|
||||||
|
</center>
|
||||||
|
<bottom>
|
||||||
|
<VBox>
|
||||||
|
<ScrollPane prefHeight="50" vbarPolicy="NEVER">
|
||||||
|
<padding>
|
||||||
|
<Insets top="10" bottom="10" left="10" right="10"/>
|
||||||
|
</padding>
|
||||||
|
<Text fx:id="outputText"/>
|
||||||
|
</ScrollPane>
|
||||||
|
<TextField fx:id="inputField" onAction="#performCalculation"/>
|
||||||
|
<Button fx:id="inputButton" text="Calculate" maxWidth="Infinity"
|
||||||
|
onAction="#performCalculation"/>
|
||||||
|
</VBox>
|
||||||
|
</bottom>
|
||||||
|
</BorderPane>
|
||||||
|
</Tab>
|
||||||
|
<Tab text="Settings" closable="false"/>
|
||||||
|
</TabPane>
|
||||||
|
</center>
|
||||||
|
|
||||||
|
</BorderPane>
|
||||||
Reference in New Issue
Block a user