mirror of
https://github.com/dmolony/DiskBrowser.git
synced 2025-02-21 11:28:58 +00:00
calculation order
This commit is contained in:
parent
112da0f4b6
commit
b9013f64dd
@ -2,15 +2,34 @@ package com.bytezone.diskbrowser.visicalc;
|
|||||||
|
|
||||||
public class Abs extends Function
|
public class Abs extends Function
|
||||||
{
|
{
|
||||||
|
private boolean hasChecked;
|
||||||
|
private double value = 0;
|
||||||
|
|
||||||
Abs (Sheet parent, String text)
|
Abs (Sheet parent, String text)
|
||||||
{
|
{
|
||||||
super (parent, text);
|
super (parent, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasValue ()
|
||||||
|
{
|
||||||
|
if (!hasChecked)
|
||||||
|
calculate ();
|
||||||
|
return hasValue;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double getValue ()
|
public double getValue ()
|
||||||
{
|
{
|
||||||
return 0;
|
return hasValue () ? value : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void calculate ()
|
||||||
|
{
|
||||||
|
hasChecked = true;
|
||||||
|
hasValue = true;
|
||||||
|
|
||||||
|
Expression exp = new Expression (parent, functionText);
|
||||||
|
value = Math.abs (exp.getValue ());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,7 +6,8 @@ class Address implements Comparable<Address>
|
|||||||
private static final int MAX_COLUMNS = 64;
|
private static final int MAX_COLUMNS = 64;
|
||||||
|
|
||||||
int row, column;
|
int row, column;
|
||||||
int sortValue;
|
int rowKey;
|
||||||
|
int columnKey;
|
||||||
String text;
|
String text;
|
||||||
|
|
||||||
public Address (String column, String row)
|
public Address (String column, String row)
|
||||||
@ -20,7 +21,8 @@ class Address implements Comparable<Address>
|
|||||||
assert row <= MAX_ROWS;
|
assert row <= MAX_ROWS;
|
||||||
this.row = row;
|
this.row = row;
|
||||||
this.column = column;
|
this.column = column;
|
||||||
sortValue = row * MAX_COLUMNS + column;
|
rowKey = row * MAX_COLUMNS + column;
|
||||||
|
columnKey = column * MAX_ROWS + row;
|
||||||
|
|
||||||
int col1 = column / 26;
|
int col1 = column / 26;
|
||||||
int col2 = column % 26;
|
int col2 = column % 26;
|
||||||
@ -37,14 +39,6 @@ class Address implements Comparable<Address>
|
|||||||
set (address.substring (0, 2), address.substring (2));
|
set (address.substring (0, 2), address.substring (2));
|
||||||
}
|
}
|
||||||
|
|
||||||
// copied from Appleworks Cell
|
|
||||||
static String getCellName (int row, int column)
|
|
||||||
{
|
|
||||||
char c1 = (char) ('A' + column / 26 - 1);
|
|
||||||
char c2 = (char) ('A' + column % 26);
|
|
||||||
return "" + (c1 == '@' ? "" : c1) + c2 + row;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void set (String sCol, String sRow)
|
private void set (String sCol, String sRow)
|
||||||
{
|
{
|
||||||
if (sCol.length () == 1)
|
if (sCol.length () == 1)
|
||||||
@ -57,7 +51,8 @@ class Address implements Comparable<Address>
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
row = Integer.parseInt (sRow) - 1;
|
row = Integer.parseInt (sRow) - 1;
|
||||||
sortValue = row * MAX_COLUMNS + column;
|
rowKey = row * MAX_COLUMNS + column;
|
||||||
|
columnKey = column * MAX_ROWS + row;
|
||||||
text = sCol + sRow;
|
text = sCol + sRow;
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
catch (NumberFormatException e)
|
||||||
@ -80,15 +75,23 @@ class Address implements Comparable<Address>
|
|||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copied from Appleworks Cell
|
||||||
|
static String getCellName (int row, int column)
|
||||||
|
{
|
||||||
|
char c1 = (char) ('A' + column / 26 - 1);
|
||||||
|
char c2 = (char) ('A' + column % 26);
|
||||||
|
return "" + (c1 == '@' ? "" : c1) + c2 + row;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString ()
|
public String toString ()
|
||||||
{
|
{
|
||||||
return String.format ("%-4s %3d %3d %4d", text, row, column, sortValue);
|
return String.format ("%-4s %3d %3d %4d", text, row, column, rowKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo (Address o)
|
public int compareTo (Address o)
|
||||||
{
|
{
|
||||||
return sortValue - o.sortValue;
|
return rowKey - o.rowKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -30,6 +30,11 @@ class Cell implements Comparable<Cell>, Value
|
|||||||
this.address = address;
|
this.address = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isValue ()
|
||||||
|
{
|
||||||
|
return type == CellType.VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
void format (String format)
|
void format (String format)
|
||||||
{
|
{
|
||||||
// /FG - general
|
// /FG - general
|
||||||
@ -68,37 +73,37 @@ class Cell implements Comparable<Cell>, Value
|
|||||||
|
|
||||||
// FUTURE.VC
|
// FUTURE.VC
|
||||||
if (false)
|
if (false)
|
||||||
if (address.sortValue == 67)
|
if (address.rowKey == 67)
|
||||||
expressionText = "1000";
|
expressionText = "1000";
|
||||||
else if (address.sortValue == 131)
|
else if (address.rowKey == 131)
|
||||||
expressionText = "10.5";
|
expressionText = "10.5";
|
||||||
else if (address.sortValue == 195)
|
else if (address.rowKey == 195)
|
||||||
expressionText = "12";
|
expressionText = "12";
|
||||||
else if (address.sortValue == 259)
|
else if (address.rowKey == 259)
|
||||||
expressionText = "8";
|
expressionText = "8";
|
||||||
|
|
||||||
// IRA.VC
|
// IRA.VC
|
||||||
if (false)
|
if (false)
|
||||||
if (address.sortValue == 66)
|
if (address.rowKey == 66)
|
||||||
expressionText = "10";
|
expressionText = "10";
|
||||||
else if (address.sortValue == 130)
|
else if (address.rowKey == 130)
|
||||||
expressionText = "30";
|
expressionText = "30";
|
||||||
else if (address.sortValue == 194)
|
else if (address.rowKey == 194)
|
||||||
expressionText = "65";
|
expressionText = "65";
|
||||||
else if (address.sortValue == 258)
|
else if (address.rowKey == 258)
|
||||||
expressionText = "1000";
|
expressionText = "1000";
|
||||||
else if (address.sortValue == 386)
|
else if (address.rowKey == 386)
|
||||||
expressionText = "15";
|
expressionText = "15";
|
||||||
|
|
||||||
// CARLOAN.VC
|
// CARLOAN.VC
|
||||||
if (false)
|
if (false)
|
||||||
if (address.sortValue == 67)
|
if (address.rowKey == 67)
|
||||||
expressionText = "9375";
|
expressionText = "9375";
|
||||||
else if (address.sortValue == 131)
|
else if (address.rowKey == 131)
|
||||||
expressionText = "4500";
|
expressionText = "4500";
|
||||||
else if (address.sortValue == 195)
|
else if (address.rowKey == 195)
|
||||||
expressionText = "24";
|
expressionText = "24";
|
||||||
else if (address.sortValue == 259)
|
else if (address.rowKey == 259)
|
||||||
expressionText = "11.9";
|
expressionText = "11.9";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,11 +181,7 @@ class Cell implements Comparable<Cell>, Value
|
|||||||
public boolean hasValue ()
|
public boolean hasValue ()
|
||||||
{
|
{
|
||||||
if (type == CellType.VALUE)
|
if (type == CellType.VALUE)
|
||||||
{
|
|
||||||
if (value == null)
|
|
||||||
createValue ();
|
|
||||||
return value.hasValue ();
|
return value.hasValue ();
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,21 +192,16 @@ class Cell implements Comparable<Cell>, Value
|
|||||||
if (type != CellType.VALUE)
|
if (type != CellType.VALUE)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (value == null)
|
|
||||||
createValue ();
|
|
||||||
|
|
||||||
return value.getValue ();
|
return value.getValue ();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getError ()
|
public String getError ()
|
||||||
{
|
{
|
||||||
if (value == null)
|
|
||||||
createValue ();
|
|
||||||
return value.getError ();
|
return value.getError ();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createValue ()
|
void calculate ()
|
||||||
{
|
{
|
||||||
if (expressionText == null)
|
if (expressionText == null)
|
||||||
{
|
{
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.bytezone.diskbrowser.visicalc;
|
package com.bytezone.diskbrowser.visicalc;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
@ -10,23 +9,24 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
import com.bytezone.diskbrowser.utilities.HexFormatter;
|
import com.bytezone.diskbrowser.utilities.HexFormatter;
|
||||||
|
|
||||||
public class Sheet implements Iterable<Cell>
|
public class Sheet
|
||||||
{
|
{
|
||||||
private static final Pattern addressPattern =
|
private static final Pattern addressPattern =
|
||||||
Pattern.compile ("([AB]?[A-Z])([0-9]{1,3}):");
|
Pattern.compile ("([AB]?[A-Z])([0-9]{1,3}):");
|
||||||
|
|
||||||
private final Map<Integer, Cell> sheet = new TreeMap<Integer, Cell> ();
|
private final Map<Integer, Cell> rowOrderCells = new TreeMap<Integer, Cell> ();
|
||||||
|
private final Map<Integer, Cell> columnOrderCells = new TreeMap<Integer, Cell> ();
|
||||||
private final List<String> lines = new ArrayList<String> ();
|
private final List<String> lines = new ArrayList<String> ();
|
||||||
|
|
||||||
Cell currentCell = null;
|
private Cell currentCell = null;
|
||||||
char defaultFormat;
|
private char defaultFormat;
|
||||||
|
|
||||||
private final Map<Integer, Integer> columnWidths = new TreeMap<Integer, Integer> ();
|
private final Map<Integer, Integer> columnWidths = new TreeMap<Integer, Integer> ();
|
||||||
private int columnWidth = 12;
|
private int columnWidth = 12;
|
||||||
private char recalculation = ' '; // auto/manual
|
private char recalculation = ' '; // auto/manual
|
||||||
private char recalculationOrder = ' '; // row/column
|
private char recalculationOrder = 'C'; // row/column
|
||||||
private int columns;
|
private int maxColumns;
|
||||||
private int rows;
|
private int maxRows;
|
||||||
|
|
||||||
// Maximum cell = BK254
|
// Maximum cell = BK254
|
||||||
|
|
||||||
@ -157,7 +157,10 @@ public class Sheet implements Iterable<Cell>
|
|||||||
|
|
||||||
private void calculate (char order)
|
private void calculate (char order)
|
||||||
{
|
{
|
||||||
|
Map<Integer, Cell> cells = order == 'R' ? rowOrderCells : columnOrderCells;
|
||||||
|
for (Cell cell : cells.values ())
|
||||||
|
if (cell.isValue ())
|
||||||
|
cell.calculate ();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getLineLength (byte[] buffer, int offset)
|
private int getLineLength (byte[] buffer, int offset)
|
||||||
@ -170,13 +173,7 @@ public class Sheet implements Iterable<Cell>
|
|||||||
|
|
||||||
private void processLine (String line)
|
private void processLine (String line)
|
||||||
{
|
{
|
||||||
// NB no closing bracket: [>K11:@SUM(J11...F11]
|
assert !line.isEmpty ();
|
||||||
|
|
||||||
if (line.isEmpty ())
|
|
||||||
{
|
|
||||||
System.out.println ("empty command");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (line.startsWith ("/"))
|
if (line.startsWith ("/"))
|
||||||
{
|
{
|
||||||
@ -229,7 +226,7 @@ public class Sheet implements Iterable<Cell>
|
|||||||
if (m.find ())
|
if (m.find ())
|
||||||
{
|
{
|
||||||
Address address = new Address (m.group (1), m.group (2));
|
Address address = new Address (m.group (1), m.group (2));
|
||||||
currentCell = sheet.get (address.sortValue);
|
currentCell = rowOrderCells.get (address.rowKey);
|
||||||
|
|
||||||
int pos = line.indexOf (':'); // end of cell address
|
int pos = line.indexOf (':'); // end of cell address
|
||||||
line = line.substring (pos + 1); // remove address from line
|
line = line.substring (pos + 1); // remove address from line
|
||||||
@ -239,11 +236,13 @@ public class Sheet implements Iterable<Cell>
|
|||||||
currentCell = new Cell (this, address);
|
currentCell = new Cell (this, address);
|
||||||
if (!line.startsWith ("/G"))
|
if (!line.startsWith ("/G"))
|
||||||
{
|
{
|
||||||
sheet.put (currentCell.address.sortValue, currentCell);
|
rowOrderCells.put (currentCell.address.rowKey, currentCell);
|
||||||
if (address.row > rows)
|
columnOrderCells.put (currentCell.address.columnKey, currentCell);
|
||||||
rows = address.row;
|
|
||||||
if (address.column > columns)
|
if (address.row > maxRows)
|
||||||
columns = address.column;
|
maxRows = address.row;
|
||||||
|
if (address.column > maxColumns)
|
||||||
|
maxColumns = address.column;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -299,18 +298,12 @@ public class Sheet implements Iterable<Cell>
|
|||||||
|
|
||||||
Cell getCell (Address address)
|
Cell getCell (Address address)
|
||||||
{
|
{
|
||||||
return sheet.get (address.sortValue);
|
return rowOrderCells.get (address.rowKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size ()
|
public int size ()
|
||||||
{
|
{
|
||||||
return sheet.size ();
|
return rowOrderCells.size ();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Iterator<Cell> iterator ()
|
|
||||||
{
|
|
||||||
return sheet.values ().iterator ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLines ()
|
public String getLines ()
|
||||||
@ -346,7 +339,7 @@ public class Sheet implements Iterable<Cell>
|
|||||||
int lastColumn = 0;
|
int lastColumn = 0;
|
||||||
|
|
||||||
StringBuilder heading = new StringBuilder (" ");
|
StringBuilder heading = new StringBuilder (" ");
|
||||||
for (int cellNo = 0; cellNo <= columns; cellNo++)
|
for (int cellNo = 0; cellNo <= maxColumns; cellNo++)
|
||||||
{
|
{
|
||||||
int width = columnWidth;
|
int width = columnWidth;
|
||||||
if (columnWidths.containsKey (cellNo))
|
if (columnWidths.containsKey (cellNo))
|
||||||
@ -370,7 +363,7 @@ public class Sheet implements Iterable<Cell>
|
|||||||
text.append ("\n001:");
|
text.append ("\n001:");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Cell cell : sheet.values ())
|
for (Cell cell : rowOrderCells.values ())
|
||||||
{
|
{
|
||||||
while (lastRow < cell.address.row)
|
while (lastRow < cell.address.row)
|
||||||
{
|
{
|
||||||
@ -411,7 +404,7 @@ public class Sheet implements Iterable<Cell>
|
|||||||
|
|
||||||
System.out.println ();
|
System.out.println ();
|
||||||
System.out.println ("Cells:");
|
System.out.println ("Cells:");
|
||||||
for (Cell cell : sheet.values ())
|
for (Cell cell : rowOrderCells.values ())
|
||||||
System.out.println (cell);
|
System.out.println (cell);
|
||||||
|
|
||||||
System.out.println ();
|
System.out.println ();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user