From 7381d8e66bebc1e3b834c20ed0a4e2085b0b6aa0 Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Mon, 13 Mar 2017 18:16:20 +1100 Subject: [PATCH] visicalc numeric formats --- .../diskbrowser/disk/DiskFactory.java | 16 ++++++----- src/com/bytezone/diskbrowser/dos/DosDisk.java | 12 ++++----- .../bytezone/diskbrowser/visicalc/Cell.java | 5 ++-- .../bytezone/diskbrowser/visicalc/Error.java | 6 ----- .../diskbrowser/visicalc/Expression.java | 8 +++--- .../bytezone/diskbrowser/visicalc/Format.java | 27 +++++++++++++++---- .../diskbrowser/visicalc/IsError.java | 10 +++---- .../bytezone/diskbrowser/visicalc/IsNa.java | 5 +--- .../bytezone/diskbrowser/visicalc/Lookup.java | 13 +++++++-- src/com/bytezone/diskbrowser/visicalc/Na.java | 6 ----- 10 files changed, 59 insertions(+), 49 deletions(-) diff --git a/src/com/bytezone/diskbrowser/disk/DiskFactory.java b/src/com/bytezone/diskbrowser/disk/DiskFactory.java index 622565b..cc7deb7 100755 --- a/src/com/bytezone/diskbrowser/disk/DiskFactory.java +++ b/src/com/bytezone/diskbrowser/disk/DiskFactory.java @@ -22,7 +22,7 @@ import com.bytezone.diskbrowser.wizardry.WizardryScenarioDisk; public class DiskFactory { - private static boolean debug = true; + private static boolean debug = false; private DiskFactory () { @@ -211,11 +211,15 @@ public class DiskFactory if (true) { - // if (debug) - // System.out.println (" trying checksums"); long checksum = appleDisk256.getBootChecksum (); - if (checksum == 3176296590L // + if (checksum == 227968344L) // empty boot sector + { + // could be wizardry data, visialc data ... + if (debug) + System.out.println (" empty sector checksum : " + checksum); + } + else if (checksum == 3176296590L // || checksum == 108825457L // || checksum == 1439356606L // || checksum == 1550012074L // @@ -258,9 +262,7 @@ public class DiskFactory else if (checksum == 2803644711L // Apple Pascal disk 0 || checksum == 3317783349L // || checksum == 1728863694L // Wizardry_I_boot.dsk - || checksum == 227968344L // wizardry_I/australia16.dsk - || checksum == 198094178L // - || checksum == 227968344L) // lauren.dsk + || checksum == 198094178L) // { if (debug) System.out.println (" known PASCAL checksum : " + checksum); diff --git a/src/com/bytezone/diskbrowser/dos/DosDisk.java b/src/com/bytezone/diskbrowser/dos/DosDisk.java index f6c5585..96bdee8 100755 --- a/src/com/bytezone/diskbrowser/dos/DosDisk.java +++ b/src/com/bytezone/diskbrowser/dos/DosDisk.java @@ -264,12 +264,12 @@ public class DosDisk extends AbstractFormattedDisk if (buffer[53] != 16 && buffer[53] != 13) // tracks per sector return 0; - if (buffer[49] < -1 || buffer[49] > 1) // direction of next file save - { - System.out.println ("Bad direction : " + buffer[49]); - // Visicalc data disk had 0xF8 - // return 0; - } + // if (buffer[49] < -1 || buffer[49] > 1) // direction of next file save + // { + // System.out.println ("Bad direction : " + buffer[49]); + // // Visicalc data disk had 0xF8 + // // return 0; + // } int version = buffer[3]; if (version < -1 || version > 4) diff --git a/src/com/bytezone/diskbrowser/visicalc/Cell.java b/src/com/bytezone/diskbrowser/visicalc/Cell.java index deac3db..7968edb 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Cell.java +++ b/src/com/bytezone/diskbrowser/visicalc/Cell.java @@ -173,9 +173,8 @@ class Cell extends AbstractValue implements Comparable @Override public ValueType getValueType () { - if (cellType == CellType.EMPTY) - return ValueType.NA; - if (cellType == CellType.LABEL || cellType == CellType.REPEATING_CHARACTER) + if (cellType == CellType.EMPTY || cellType == CellType.LABEL + || cellType == CellType.REPEATING_CHARACTER) return ValueType.VALUE; return value.getValueType (); diff --git a/src/com/bytezone/diskbrowser/visicalc/Error.java b/src/com/bytezone/diskbrowser/visicalc/Error.java index 07623e2..52b4ed2 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Error.java +++ b/src/com/bytezone/diskbrowser/visicalc/Error.java @@ -7,10 +7,4 @@ class Error extends Function super (parent, text); valueType = ValueType.ERROR; } - - @Override - public double getValue () - { - return 0; - } } \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/visicalc/Expression.java b/src/com/bytezone/diskbrowser/visicalc/Expression.java index f431471..6b58dcf 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Expression.java +++ b/src/com/bytezone/diskbrowser/visicalc/Expression.java @@ -157,13 +157,13 @@ class Expression extends AbstractValue implements Iterable thisValue.calculate (); value = 0; - if (thisValue.isValueType (ValueType.ERROR)) + if (!thisValue.isValueType (ValueType.VALUE)) { valueType = thisValue.getValueType (); return; } - value = thisValue.getValue (); // NA returns zero + value = thisValue.getValue (); String sign = signs.get (0); if (sign.equals ("(-)")) @@ -174,13 +174,13 @@ class Expression extends AbstractValue implements Iterable thisValue = values.get (i); thisValue.calculate (); - if (thisValue.isValueType (ValueType.ERROR)) + if (!thisValue.isValueType (ValueType.VALUE)) { valueType = thisValue.getValueType (); return; } - double nextValue = thisValue.getValue (); // NA returns zero + double nextValue = thisValue.getValue (); sign = signs.get (i); if (sign.equals ("(-)")) diff --git a/src/com/bytezone/diskbrowser/visicalc/Format.java b/src/com/bytezone/diskbrowser/visicalc/Format.java index b5ef253..9ad333b 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Format.java +++ b/src/com/bytezone/diskbrowser/visicalc/Format.java @@ -17,18 +17,33 @@ public class Format if (actualValue == -0.0) actualValue = 0; + String valueText = String.valueOf ((int) actualValue); + if (valueText.startsWith ("0")) + valueText = valueText.substring (1); + int digits = valueText.length (); + if (digits > colWidth) + return OVERFLOW.substring (0, colWidth); + switch (formatChar) { case 'L': case 'R': case 'G': case ' ': - String numberFormat = String.format ("%%%d.7f", colWidth + 8); + int precision = colWidth - (digits + 1); + if (digits == 0) + precision = colWidth - 1; + if (precision < 0) + precision = 0; + String numberFormat = String.format ("%%%d.%df", colWidth, precision); String val = String.format (numberFormat, actualValue); + // System.out.printf ("%s %2d %2d %s %15.8f %s : ", formatChar, colWidth, + // digits, numberFormat, actualValue, val); val = val.trim (); - while (val.endsWith ("0")) - val = val.substring (0, val.length () - 1); + if (val.indexOf ('.') >= 0) + while (val.endsWith ("0")) + val = val.substring (0, val.length () - 1); if (val.endsWith (".")) val = val.substring (0, val.length () - 1); if (val.startsWith ("0.")) @@ -48,14 +63,16 @@ public class Format val = String.format (rightFormat, val); } + // System.out.printf ("[%s]%n", val); + if (val.length () > colWidth) return OVERFLOW.substring (0, colWidth); return val; case 'I': - String integerFormat = String.format ("%%%dd", colWidth); - String result = String.format (integerFormat, (int) actualValue); + String integerFormat = String.format ("%%%d.0f", colWidth); + String result = String.format (integerFormat, actualValue); if (result.length () > colWidth) return OVERFLOW.substring (0, colWidth); return result; diff --git a/src/com/bytezone/diskbrowser/visicalc/IsError.java b/src/com/bytezone/diskbrowser/visicalc/IsError.java index 93645b6..f08cd1a 100644 --- a/src/com/bytezone/diskbrowser/visicalc/IsError.java +++ b/src/com/bytezone/diskbrowser/visicalc/IsError.java @@ -2,22 +2,20 @@ package com.bytezone.diskbrowser.visicalc; class IsError extends Function { - Cell cell; + Value expression; public IsError (Sheet parent, String text) { super (parent, text); - cell = parent.getCell (functionText); + expression = new Expression (parent, functionText).reduce (); } @Override public void calculate () { - // if (cell == null) - // cell = parent.getCell (functionText); - - value = cell == null ? 1 : cell.isValueType (ValueType.ERROR) ? 1 : 0; + expression.calculate (); + value = expression.isValueType (ValueType.ERROR) ? 1 : 0; valueType = ValueType.VALUE; } } \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/visicalc/IsNa.java b/src/com/bytezone/diskbrowser/visicalc/IsNa.java index ad292ee..18b1c12 100644 --- a/src/com/bytezone/diskbrowser/visicalc/IsNa.java +++ b/src/com/bytezone/diskbrowser/visicalc/IsNa.java @@ -14,11 +14,8 @@ public class IsNa extends Function @Override public void calculate () { - // if (expression == null) - // expression = new Expression (parent, functionText); - expression.calculate (); - value = expression.getValue (); + value = expression.isValueType (ValueType.NA) ? 1 : 0; valueType = expression.getValueType (); } } \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/visicalc/Lookup.java b/src/com/bytezone/diskbrowser/visicalc/Lookup.java index 916e794..9cae7eb 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Lookup.java +++ b/src/com/bytezone/diskbrowser/visicalc/Lookup.java @@ -30,6 +30,7 @@ class Lookup extends Function if (!source.isValueType (ValueType.VALUE)) { valueType = source.getValueType (); + // valueType = ValueType.NA; return; } @@ -45,15 +46,23 @@ class Lookup extends Function for (Address address : range) { Cell cell = parent.getCell (address); - if (cell.isValueType (ValueType.NA)) - continue; + // if (cell.isValueType (ValueType.NA)) + // { + // // System.out.println ("NA1"); + // break; + // // continue; + // } if (cell.getValue () > sourceValue) // past the value break; target = address; } if (target == null) + { valueType = ValueType.NA; + // System.out.println ("NA2"); + value = 0; + } else { Address adjacentAddress = diff --git a/src/com/bytezone/diskbrowser/visicalc/Na.java b/src/com/bytezone/diskbrowser/visicalc/Na.java index a01a737..ac2fc6a 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Na.java +++ b/src/com/bytezone/diskbrowser/visicalc/Na.java @@ -7,10 +7,4 @@ public class Na extends Function super (parent, text); valueType = ValueType.NA; } - - @Override - public double getValue () - { - return 0; - } } \ No newline at end of file