mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 08:05:19 +00:00
Compare commits
3 Commits
config-rew
...
binary-num
| Author | SHA1 | Date | |
|---|---|---|---|
| 0be67410ec | |||
| 6f99f07150 | |||
| 2cf41c1029 |
@@ -6,6 +6,10 @@ 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
|
||||
|
||||
@@ -11,9 +11,19 @@ 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
|
||||
@@ -31,8 +41,15 @@ public class AbacusController {
|
||||
@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
|
||||
|
||||
@@ -6,8 +6,17 @@ 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){
|
||||
|
||||
@@ -3,12 +3,33 @@ 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();
|
||||
@@ -18,23 +39,47 @@ public class HistoryModel {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
/**
|
||||
* A number that uses a BigDecimal to store its value,
|
||||
* leading to infinite possible precision.
|
||||
*/
|
||||
public class PreciseNumber implements NumberInterface {
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user