From 65aa71254cbf459614002bcb9a675487fb068592 Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Sun, 5 Mar 2017 21:42:27 +1100 Subject: [PATCH] Added @SQRT --- .../diskbrowser/applefile/VisicalcFile.java | 6 - .../bytezone/diskbrowser/visicalc/Cell.java | 3 +- .../bytezone/diskbrowser/visicalc/Choose.java | 7 + .../diskbrowser/visicalc/Function.java | 3 + .../bytezone/diskbrowser/visicalc/Sheet.java | 121 +++++++++--------- .../bytezone/diskbrowser/visicalc/Sqrt.java | 28 ++++ 6 files changed, 102 insertions(+), 66 deletions(-) create mode 100644 src/com/bytezone/diskbrowser/visicalc/Sqrt.java diff --git a/src/com/bytezone/diskbrowser/applefile/VisicalcFile.java b/src/com/bytezone/diskbrowser/applefile/VisicalcFile.java index 87447c3..21b031c 100644 --- a/src/com/bytezone/diskbrowser/applefile/VisicalcFile.java +++ b/src/com/bytezone/diskbrowser/applefile/VisicalcFile.java @@ -24,12 +24,6 @@ public class VisicalcFile extends AbstractFile text.append ("Cells : " + sheet.size () + "\n\n"); text.append (sheet.getTextDisplay (debug)); - if (debug) - { - text.append ("\n"); - text.append (sheet.getLines ()); - } - return text.toString (); } diff --git a/src/com/bytezone/diskbrowser/visicalc/Cell.java b/src/com/bytezone/diskbrowser/visicalc/Cell.java index c4ee57c..d1f7d95 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Cell.java +++ b/src/com/bytezone/diskbrowser/visicalc/Cell.java @@ -146,7 +146,8 @@ class Cell extends AbstractValue implements Comparable case VALUE: if (!isValueType (ValueType.VALUE)) { - char fmt = cellFormat != ' ' ? cellFormat : globalFormat; + char fmt = + cellFormat != ' ' ? cellFormat : globalFormat != ' ' ? globalFormat : 'R'; return Format.justify (value.getText (), colWidth, fmt); } diff --git a/src/com/bytezone/diskbrowser/visicalc/Choose.java b/src/com/bytezone/diskbrowser/visicalc/Choose.java index ec7a8e2..5327840 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Choose.java +++ b/src/com/bytezone/diskbrowser/visicalc/Choose.java @@ -19,4 +19,11 @@ public class Choose extends Function values.add (source); } + + @Override + public void calculate () + { + source.calculate (); + System.out.println ("@CHOOSE not written yet"); + } } \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/visicalc/Function.java b/src/com/bytezone/diskbrowser/visicalc/Function.java index e4aaaef..035e12b 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Function.java +++ b/src/com/bytezone/diskbrowser/visicalc/Function.java @@ -92,6 +92,9 @@ abstract class Function extends AbstractValue implements Iterable if (text.startsWith ("@SUM(")) return new Sum (parent, text); + if (text.startsWith ("@SQRT(")) + return new Sqrt (parent, text); + System.out.printf ("Unknown function: [%s]%n", text); return new Error (parent, "@ERROR"); } diff --git a/src/com/bytezone/diskbrowser/visicalc/Sheet.java b/src/com/bytezone/diskbrowser/visicalc/Sheet.java index 040369f..6ba639a 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Sheet.java +++ b/src/com/bytezone/diskbrowser/visicalc/Sheet.java @@ -21,16 +21,17 @@ public class Sheet private final Map columnOrderCells = new TreeMap (); private final List lines = new ArrayList (); - private char globalFormat = ' '; - private final Map columnWidths = new TreeMap (); - private int columnWidth = 12; + private int columnWidth = 9; + private char globalFormat = ' '; private char recalculation = 'A'; // auto/manual private char recalculationOrder = 'C'; // row/column - private int highestColumn; - private int highestRow; + private int minColumn = 9999; + private int maxColumn; + private int minRow = 9999; + private int maxRow; // Maximum cell = BK254 @@ -164,12 +165,9 @@ public class Sheet ptr += length + 1; // +1 for end-of-line token } - System.out.println ("** Start of calculation **"); // might have to keep recalculating until nothing changes?? if (recalculation == 'A') // auto { - // recalculationOrder = 'R'; - // System.out.printf ("Calculation order: %s%n", recalculationOrder); calculate (recalculationOrder); calculate (recalculationOrder); } @@ -233,40 +231,35 @@ public class Sheet } // check for formatting commands - String format = ""; while (line.startsWith ("/")) { - if (line.charAt (1) == '-') // repeating label + if (line.charAt (1) == '-') // repeating label { currentCell.setFormat (line); line = ""; - format += line; } else { - String fmt = line.substring (0, FORMAT_LENGTH); + currentCell.setFormat (line.substring (0, FORMAT_LENGTH)); line = line.substring (FORMAT_LENGTH); - currentCell.setFormat (fmt); // formatting command - format += fmt; } } // if there is anything left it must be an expression if (!line.isEmpty ()) - currentCell.setValue (line); // expression - - if (false) - System.out.printf ("[%s][%-3s][%s]%n", currentCell.getAddress (), format, line); + currentCell.setValue (line); // expression } private void addCell (Cell cell) { - // System.out.printf ("Adding: %s%n", cell); rowOrderCells.put (cell.getAddress ().getRowKey (), cell); columnOrderCells.put (cell.getAddress ().getColumnKey (), cell); - highestRow = Math.max (highestRow, cell.getAddress ().getRow ()); - highestColumn = Math.max (highestColumn, cell.getAddress ().getColumn ()); + minRow = Math.min (minRow, cell.getAddress ().getRow ()); + minColumn = Math.min (minColumn, cell.getAddress ().getColumn ()); + + maxRow = Math.max (maxRow, cell.getAddress ().getRow ()); + maxColumn = Math.max (maxColumn, cell.getAddress ().getColumn ()); } Cell getCell (String addressText) @@ -279,7 +272,6 @@ public class Sheet Cell cell = rowOrderCells.get (address.getRowKey ()); if (cell == null) { - // System.out.printf ("cell not found, creating: %s%n", address); cell = new Cell (this, address); addCell (cell); } @@ -300,54 +292,41 @@ public class Sheet { switch (line.charAt (1)) { - case 'W': - // System.out.printf ("Skipping [%s]%n", line); - break; case 'G': - switch (line.charAt (2)) - { - case 'R': - recalculation = line.charAt (3); - break; - case 'O': - recalculationOrder = line.charAt (3); - break; - case 'P': - // System.out.printf ("Skipping [%s]%n", line); - break; - case 'C': - columnWidth = Integer.parseInt (line.substring (3)); - break; - case 'F': - globalFormat = line.charAt (3); - break; - default: - System.out.printf ("Unknown global format [%s]%n", line); - break; - } + setGlobal (line); + break; + case 'W': break; case 'X': - // System.out.printf ("Skipping [%s]%n", line); break; default: System.out.printf ("Skipping [%s]%n", line); } } - public String getLines () + private void setGlobal (String line) { - StringBuilder text = new StringBuilder (); - - for (String line : lines) + switch (line.charAt (2)) { - text.append (line); - text.append ("\n"); + case 'C': + columnWidth = Integer.parseInt (line.substring (3)); + break; + case 'O': + recalculationOrder = line.charAt (3); + break; + case 'R': + recalculation = line.charAt (3); + break; + case 'F': + globalFormat = line.charAt (3); + break; + case 'P': + // System.out.printf ("Skipping [%s]%n", line); + break; + default: + System.out.printf ("Unknown global format [%s]%n", line); + break; } - - if (text.length () > 0) - text.deleteCharAt (text.length () - 1); - - return text.toString (); } public String getTextDisplay (boolean debug) @@ -367,7 +346,7 @@ public class Sheet int lastColumn = 0; StringBuilder heading = new StringBuilder (" "); - for (int column = 0; column <= highestColumn; column++) + for (int column = 0; column <= maxColumn; column++) { int width = columnWidth; if (columnWidths.containsKey (column)) @@ -386,6 +365,20 @@ public class Sheet heading.append (String.format (fmt, underline)); } + text.append (String.format ("Global format : %s%n", globalFormat)); + text.append (String.format ("Column width : %d%n", columnWidth)); + text.append (String.format ("Recalculation order : %s%n", + recalculationOrder == 'R' ? "Row" : "Column")); + text.append (String.format ("Recalculation : %s%n", + recalculation == 'A' ? "Automatic" : "Manual")); + + if (rowOrderCells.size () > 0) + text.append (String.format ("Range : %s:%s%n%n", + Address.getCellName (minRow + 1, minColumn), + Address.getCellName (maxRow + 1, maxColumn))); + else + text.append ("\n\n"); + if (debug) { text.append (heading); @@ -443,6 +436,16 @@ public class Sheet text.append (cell.getDebugText ()); text.append ("\n"); } + + text.append ("\n"); + for (String line : lines) + { + text.append (line); + text.append ("\n"); + } + + if (text.length () > 0) + text.deleteCharAt (text.length () - 1); } return text.toString (); diff --git a/src/com/bytezone/diskbrowser/visicalc/Sqrt.java b/src/com/bytezone/diskbrowser/visicalc/Sqrt.java new file mode 100644 index 0000000..682c780 --- /dev/null +++ b/src/com/bytezone/diskbrowser/visicalc/Sqrt.java @@ -0,0 +1,28 @@ +package com.bytezone.diskbrowser.visicalc; + +public class Sqrt extends Function +{ + private final Expression source; + + Sqrt (Sheet parent, String text) + { + super (parent, text); + source = new Expression (parent, text.substring (5, text.length () - 1)); + values.add (source); + } + + @Override + public void calculate () + { + source.calculate (); + + if (!source.isValueType (ValueType.VALUE)) + { + valueType = source.getValueType (); + return; + } + + value = Math.sqrt (source.getValue ()); + valueType = ValueType.VALUE; + } +} \ No newline at end of file