always add cells

This commit is contained in:
Denis Molony 2017-03-03 21:24:23 +11:00
parent 7451194aaf
commit 2b9db288d4
14 changed files with 136 additions and 57 deletions

View File

@ -16,6 +16,7 @@ public class Abs extends Function
{
source = new Expression (parent, functionText);
values.add (source);
source.calculate ();
}
value = Math.abs (source.getValue ());

View File

@ -7,7 +7,7 @@ public class Average extends Function
public Average (Sheet parent, String text)
{
super (parent, text);
range = new Range (text);
range = new Range (parent, text);
}
@Override

View File

@ -81,6 +81,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
{
expressionText = command;
cellType = CellType.VALUE;
value = new Expression (parent, expressionText).reduce ();
}
// FUTURE.VC
@ -144,7 +145,10 @@ class Cell extends AbstractValue implements Comparable<Cell>
case VALUE:
if (!isValueType (ValueType.VALUE))
return Format.justify (value.getText (), colWidth, 'R');
{
char fmt = cellFormat != ' ' ? cellFormat : globalFormat;
return Format.justify (value.getText (), colWidth, fmt);
}
char formatChar = cellFormat != ' ' ? cellFormat : globalFormat;
return " " + Format.format (value, formatChar, colWidth - 1);
@ -158,8 +162,10 @@ class Cell extends AbstractValue implements Comparable<Cell>
@Override
public double getValue ()
{
if (value == null)
calculate ();
// if (value == null)
// calculate ();
if (cellType != CellType.VALUE)
return 0;
return value.getValue ();
}
@ -167,14 +173,21 @@ class Cell extends AbstractValue implements Comparable<Cell>
@Override
public ValueType getValueType ()
{
// if (value == null)
// calculate ();
if (cellType == CellType.EMPTY)
return ValueType.NA;
return value.getValueType ();
}
@Override
public String getText ()
{
if (value == null)
calculate ();
// if (value == null)
// calculate ();
if (cellType == CellType.EMPTY)
return "";
return value.getText ();
}
@ -182,9 +195,15 @@ class Cell extends AbstractValue implements Comparable<Cell>
@Override
public boolean isValueType (ValueType type)
{
if (value == null)
calculate ();
// if (value == null || value.getValueType () != ValueType.VALUE)
// calculate ();
if (cellType == CellType.LABEL || cellType == CellType.REPEATING_CHARACTER)
return type == ValueType.VALUE;
if (cellType == CellType.EMPTY)
return type == ValueType.NA;
assert value != null : "bollocks " + address;
return value.isValueType (type);
}
@ -196,20 +215,15 @@ class Cell extends AbstractValue implements Comparable<Cell>
@Override
public void calculate ()
{
if (value != null && value.isValueType (ValueType.VALUE))
return;
// if (value != null && value.isValueType (ValueType.VALUE))
// return;
//
// if (expressionText == null)
// expressionText = "";
if (value == null)
{
if (expressionText == null)
expressionText = "";
value = new Expression (parent, expressionText).reduce ();
}
value.calculate ();
return;
// value = new Expression (parent, expressionText).reduce ();
if (value != null)
value.calculate ();
}
public String getDebugText ()

View File

@ -14,7 +14,7 @@ public class Choose extends Function
int pos = text.indexOf (',');
sourceText = text.substring (8, pos);
rangeText = text.substring (pos + 1, text.length () - 1);
range = new Range (rangeText);
range = new Range (parent, rangeText);
source = new Number (sourceText);
}
}

View File

@ -7,7 +7,7 @@ class Count extends Function
public Count (Sheet parent, String text)
{
super (parent, text);
range = new Range (text);
range = new Range (parent, text);
}
@Override

View File

@ -74,9 +74,9 @@ class Expression extends AbstractValue implements Iterable<Value>
bracketText.substring (1, bracketText.length () - 1)));
break;
case '#':
case '#': // no idea
System.out.printf ("Hash character [%s] in [%s]%n", ch, line);
ptr++; // no idea
ptr++;
break;
default:
@ -93,8 +93,9 @@ class Expression extends AbstractValue implements Iterable<Value>
Cell cell = parent.getCell (addressText);
if (cell == null)
{
System.out.println ("adding NA");
values.add (Function.getInstance (parent, "@NA"));
// should this (or parent) create a new empty cell?
// cell = parent.addCell (addressText);
values.add (new Number ("0"));
}
else
values.add (parent.getCell (addressText));
@ -154,17 +155,18 @@ class Expression extends AbstractValue implements Iterable<Value>
try
{
Value thisValue = values.get (0);
thisValue.calculate ();
if (thisValue != null) // || thisValue.getValueType () != ValueType.VALUE)
thisValue.calculate ();
value = 0;
if (thisValue.isValueType (ValueType.VALUE))
value = thisValue.getValue ();
else
if (!thisValue.isValueType (ValueType.VALUE))
{
valueType = thisValue.getValueType ();
return;
}
value = thisValue.getValue ();
String sign = signs.get (0);
if (sign.equals ("(-)"))
value *= -1;
@ -172,17 +174,18 @@ class Expression extends AbstractValue implements Iterable<Value>
for (int i = 1; i < values.size (); i++)
{
thisValue = values.get (i);
thisValue.calculate ();
if (thisValue != null) // || thisValue.getValueType () != ValueType.VALUE)
thisValue.calculate ();
double nextValue = 0;
if (thisValue.isValueType (ValueType.VALUE))
nextValue = thisValue.getValue ();
else
if (!thisValue.isValueType (ValueType.VALUE))
// if (thisValue.isValueType (ValueType.ERROR))
{
valueType = thisValue.getValueType ();
return;
}
double nextValue = thisValue.getValue ();
sign = signs.get (i);
if (sign.equals ("(-)"))
nextValue *= -1;

View File

@ -2,17 +2,21 @@ package com.bytezone.diskbrowser.visicalc;
public class Int extends Function
{
Expression source;
Int (Sheet parent, String text)
{
super (parent, text);
source = new Expression (parent, functionText);
values.add (source);
}
@Override
public void calculate ()
{
Expression exp = new Expression (parent, functionText);
value = (int) exp.getValue ();
valueType = exp.getValueType ();
source.calculate ();
value = (int) source.getValue ();
valueType = source.getValueType ();
}
}

View File

@ -13,26 +13,29 @@ class Lookup extends Function
int pos = text.indexOf (',');
sourceText = text.substring (8, pos);
source = new Expression (parent, sourceText);
values.add (source);
rangeText = text.substring (pos + 1, text.length () - 1);
range = new Range (rangeText);
range = new Range (parent, rangeText);
}
@Override
public void calculate ()
{
if (source == null)
{
source = new Expression (parent, sourceText);
values.add (source);
}
source.calculate ();
if (!source.isValueType (ValueType.VALUE))
{
valueType = source.getValueType ();
return;
}
if (range.size () == 0)
{
valueType = ValueType.NA;
return;
}
double sourceValue = source.getValue ();
Address target = null;
@ -44,15 +47,26 @@ class Lookup extends Function
target = address;
}
// System.out.printf ("*****-----**** %s%n", target);
if (target == null)
valueType = ValueType.NA;
else
{
Address adjacentAddress =
range.isVertical () ? target.nextColumn () : target.nextRow ();
Cell adjacentCell = parent.getCell (adjacentAddress);
value = adjacentCell.getValue ();
valueType = ValueType.VALUE;
if (parent.cellExists (adjacentAddress))
{
Cell adjacentCell = parent.getCell (adjacentAddress);
if (adjacentCell != null)
value = adjacentCell.getValue ();
valueType = ValueType.VALUE;
}
else
{
value = 0;
valueType = ValueType.VALUE;
}
}
}

View File

@ -7,7 +7,7 @@ class Max extends Function
public Max (Sheet parent, String text)
{
super (parent, text);
range = new Range (text);
range = new Range (parent, text);
}
@Override

View File

@ -7,7 +7,7 @@ class Min extends Function
public Min (Sheet parent, String text)
{
super (parent, text);
range = new Range (text);
range = new Range (parent, text);
}
@Override

View File

@ -11,7 +11,7 @@ public class Npv extends Function
Npv (Sheet parent, String text)
{
super (parent, text);
range = new Range (text);
range = new Range (parent, text);
// int pos = text.indexOf (',');
// valueText = text.substring (8, pos);

View File

@ -15,24 +15,41 @@ class Range implements Iterable<Address>
Address from, to;
List<Address> range = new ArrayList<Address> ();
Sheet parent;
public Range (String rangeText)
public Range (Sheet parent, String rangeText)
{
this.parent = parent;
setRange (rangeText);
createCells ();
}
public Range (Address from, Address to)
public Range (Sheet parent, Address from, Address to)
{
this.from = from;
this.to = to;
this.parent = parent;
addRange ();
createCells ();
}
public Range (String[] cells)
public Range (Sheet parent, String[] cells)
{
this.parent = parent;
for (String s : cells)
range.add (new Address (s));
createCells ();
}
private void createCells ()
{
for (Address address : range)
parent.getCell (address);
}
private void addRange ()
@ -54,6 +71,7 @@ class Range implements Iterable<Address>
}
else
throw new InvalidParameterException ();
from = tempFrom;
}
@ -77,6 +95,17 @@ class Range implements Iterable<Address>
return range.iterator ();
}
public int size ()
{
int total = 0;
for (Address address : range)
if (parent.getCell (address) != null)
++total;
return total;
}
private void setRange (String text)
{
Matcher m = rangePattern.matcher (text);

View File

@ -164,6 +164,7 @@ 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
{
@ -260,6 +261,7 @@ public class Sheet
private void addCell (Cell cell)
{
// System.out.printf ("Adding: %s%n", cell);
rowOrderCells.put (cell.getAddress ().rowKey, cell);
columnOrderCells.put (cell.getAddress ().columnKey, cell);
@ -274,7 +276,19 @@ public class Sheet
Cell getCell (Address address)
{
return rowOrderCells.get (address.rowKey);
Cell cell = rowOrderCells.get (address.rowKey);
if (cell == null)
{
// System.out.printf ("cell not found, creating: %s%n", address);
cell = new Cell (this, address);
addCell (cell);
}
return cell;
}
boolean cellExists (Address address)
{
return rowOrderCells.get (address.rowKey) != null;
}
public int size ()

View File

@ -7,7 +7,7 @@ class Sum extends Function
public Sum (Sheet parent, String text)
{
super (parent, text);
range = new Range (text);
range = new Range (parent, text);
}
@Override