renamed to ValueList, added @CHOOSE

This commit is contained in:
Denis Molony 2017-03-16 11:27:45 +11:00
parent 0d193d8da9
commit 155d2bfd39
11 changed files with 59 additions and 37 deletions

View File

@ -21,6 +21,7 @@ class Address implements Comparable<Address>
{
assert column <= MAX_COLUMNS;
assert row <= MAX_ROWS;
this.row = row;
this.column = column;
rowKey = row * MAX_COLUMNS + column;

View File

@ -1,16 +1,18 @@
package com.bytezone.diskbrowser.visicalc;
import com.bytezone.diskbrowser.visicalc.Cell.CellType;
public class Average extends Function
{
// private final Range range;
private final ExpressionList list;
private final ValueList list;
private final boolean isRange; // may affect how the count is done
public Average (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);
// range = new Range (parent, cell, text); // use list instead
list = new ExpressionList (parent, cell, functionText);
list = new ValueList (parent, cell, functionText);
isRange = functionText.indexOf ("...") > 0;
}
@Override
@ -21,8 +23,10 @@ public class Average extends Function
for (Value v : list)
{
if (v instanceof Cell && ((Cell) v).isCellType (CellType.EMPTY))
continue;
v.calculate ();
// Cell cell = parent.getCell (address);
if (v.isValueType (ValueType.NA))
continue;

View File

@ -5,16 +5,16 @@ public class Choose extends Function
private final Range range;
private final String sourceText;
private final String rangeText;
private final Number source;
private final Value source;
Choose (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);
int pos = text.indexOf (',');
sourceText = text.substring (8, pos);
source = new Number (sourceText);
rangeText = text.substring (pos + 1, text.length () - 1);
int pos = functionText.indexOf (',');
sourceText = functionText.substring (0, pos);
source = new Expression (parent, cell, sourceText).reduce ();
rangeText = functionText.substring (pos + 1);
range = new Range (parent, cell, rangeText);
values.add (source);
@ -24,6 +24,14 @@ public class Choose extends Function
public void calculate ()
{
source.calculate ();
System.out.println ("@CHOOSE not written yet");
Address address = range.get ((int) source.getValue () - 1);
if (address == null)
valueType = ValueType.NA;
else
{
Cell cell = parent.getCell (address);
valueType = cell.getValueType ();
value = cell.getValue ();
}
}
}

View File

@ -4,14 +4,14 @@ import com.bytezone.diskbrowser.visicalc.Cell.CellType;
class Count extends Function
{
private final ExpressionList list;
private final ValueList list;
private final boolean isRange;
public Count (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);
list = new ExpressionList (parent, cell, functionText);
list = new ValueList (parent, cell, functionText);
isRange = functionText.indexOf ("...") > 0;
}

View File

@ -2,13 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
class Max extends Function
{
private final ExpressionList list;
private final ValueList list;
public Max (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);
list = new ExpressionList (parent, cell, functionText);
list = new ValueList (parent, cell, functionText);
}
@Override

View File

@ -2,13 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
class Min extends Function
{
private final ExpressionList list;
private final ValueList list;
public Min (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);
list = new ExpressionList (parent, cell, functionText);
list = new ValueList (parent, cell, functionText);
}
@Override

View File

@ -1,5 +1,7 @@
package com.bytezone.diskbrowser.visicalc;
import com.bytezone.diskbrowser.visicalc.Cell.CellType;
public class Npv extends Function
{
private final String valueText;
@ -43,7 +45,7 @@ public class Npv extends Function
++period;
Cell cell = parent.getCell (address);
if (cell.isValueType (ValueType.NA))
if (cell.isCellType (CellType.EMPTY))
continue;
if (!cell.isValueType (ValueType.VALUE))

View File

@ -93,6 +93,11 @@ class Range implements Iterable<Address>
return range.size ();
}
public Address get (int index)
{
return range.get (index);
}
@Override
public String toString ()
{

View File

@ -56,10 +56,10 @@ public class Sheet
// /GF Global Format (DGILR$*)
// /GFI Global Format Integer
// /GF$ Global Format Currency
// /GC Global Column <width>
// /GR Global
// /GC Global Column <width> 3-37
// /GR Global Recalculation A/M
// /GRA Recalculation Auto
// /GO Global
// /GO Global Calculation Order C/R
// /GOC Calculation Order - Columns first
// /GOR Calculation Order - Rows first
@ -69,9 +69,12 @@ public class Sheet
// /TB fix Both Titles
// /TN fix Neither
// /W Window (HV1SU)
// /W Window split (HV1SU)
// /WV Window Vertical (split on cursor column)
// /WH Window Horizontal (split on cursor row)
// /W1 Window (return to single window)
// /WS Window (synchronised)
// /WU Window (unsynchronised)
/*
from: Apple II TextFiles

View File

@ -2,13 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
class Sum extends Function
{
private final ExpressionList list;
private final ValueList list;
public Sum (Sheet parent, Cell cell, String text)
{
super (parent, cell, text);
list = new ExpressionList (parent, cell, functionText);
list = new ValueList (parent, cell, functionText);
}
@Override

View File

@ -1,24 +1,26 @@
package com.bytezone.diskbrowser.visicalc;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
public class ExpressionList extends AbstractValue implements Iterable<Value>
public class ValueList implements Iterable<Value>
{
private static final Pattern cellAddress = Pattern.compile ("[A-B]?[A-Z][0-9]{1,3}");
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
private final Sheet parent;
protected List<Value> values = new ArrayList<Value> ();
public ExpressionList (Sheet parent, Cell cell, String text)
public ValueList (Sheet parent, Cell cell, String text)
{
super ("expL");
this.parent = parent;
int ptr = 0;
while (ptr < text.length ())
{
if (text.charAt (ptr) == '@')
if (text.charAt (ptr) == '@') // function
{
String functionText = Expression.getBalancedText (text.substring (ptr));
Value v = new Expression (parent, cell, functionText).reduce ();
@ -29,20 +31,16 @@ public class ExpressionList extends AbstractValue implements Iterable<Value>
{
String item = getNextItem (text, ptr);
int pos = item.indexOf ("...");
if (pos > 0) // range
if (pos > 0) // range
{
String fromAddress = item.substring (0, pos);
String toAddress = item.substring (pos + 3);
Address from = new Address (fromAddress);
Address to = new Address (toAddress);
Address from = new Address (item.substring (0, pos));
Address to = new Address (item.substring (pos + 3));
Range range = new Range (parent, from, to);
for (Address address : range)
values.add (parent.getCell (address));
}
else
else // cell/number/expression
{
Value v = new Expression (parent, cell, item).reduce ();
values.add (v);
@ -53,10 +51,11 @@ public class ExpressionList extends AbstractValue implements Iterable<Value>
if (ptr < text.length () && text.charAt (ptr) == ',')
ptr++;
if (ptr < text.length () && text.charAt (ptr) == ')')
ptr++;
break;
}
}
// return substring of text from ptr up to the next comma
private String getNextItem (String text, int ptr)
{
int p = ptr;