@NA returns false for isError()

This commit is contained in:
Denis Molony 2016-03-19 16:31:30 +11:00
parent e3625c4e0f
commit d1ed74d449
10 changed files with 92 additions and 58 deletions

View File

@ -112,10 +112,6 @@ class Cell implements Comparable<Cell>, Value
String getText (int colWidth, char defaultFormat) String getText (int colWidth, char defaultFormat)
{ {
// cell may have been created when formatted but no type set
// if (type == null)
// return justify ("", colWidth);
switch (type) switch (type)
{ {
case LABEL: case LABEL:
@ -192,14 +188,14 @@ class Cell implements Comparable<Cell>, Value
@Override @Override
public String getText () public String getText ()
{ {
assert type == CellType.VALUE : "Cell type: " + type; assert isValue () : "Cell type: " + type;
return value.getText (); return value.getText ();
} }
@Override @Override
public boolean isError () public boolean isError ()
{ {
assert type == CellType.VALUE : "Cell type: " + type; assert isValue () : "Cell type: " + type;
return value.isError (); return value.isError ();
} }
@ -235,8 +231,8 @@ class Cell implements Comparable<Cell>, Value
@Override @Override
public String toString () public String toString ()
{ {
String contents = "<empty>"; String contents = "";
if (type != null)
switch (type) switch (type)
{ {
case LABEL: case LABEL:

View File

@ -45,14 +45,20 @@ class Condition
public boolean getResult () public boolean getResult ()
{ {
// System.out.println (conditionText);
if (conditionExpression == null) if (conditionExpression == null)
{ {
// System.out.printf ("creating %s%n", conditionText);
conditionExpression = new Expression (parent, conditionText); conditionExpression = new Expression (parent, conditionText);
// System.out.printf ("creating %s%n", valueText);
valueExpression = new Expression (parent, valueText); valueExpression = new Expression (parent, valueText);
// System.out.printf ("calculating %s%n", conditionExpression);
conditionExpression.calculate (); conditionExpression.calculate ();
// System.out.printf ("calculating %s%n", valueExpression);
valueExpression.calculate (); valueExpression.calculate ();
} }
// System.out.println ("after calculation");
if (conditionExpression.isError () || valueExpression.isError ()) if (conditionExpression.isError () || valueExpression.isError ())
return false; return false;

View File

@ -32,12 +32,13 @@ class Expression implements Value
private final List<String> operators = new ArrayList<String> (); private final List<String> operators = new ArrayList<String> ();
private final List<String> signs = new ArrayList<String> (); private final List<String> signs = new ArrayList<String> ();
// protected boolean isError;
private ValueType valueType; private ValueType valueType;
private double value; private double value;
private String text;
public Expression (Sheet parent, String text) public Expression (Sheet parent, String text)
{ {
this.text = text;
String line = checkBrackets (text); String line = checkBrackets (text);
// System.out.printf ("Exp[%s]%n", line); // System.out.printf ("Exp[%s]%n", line);
@ -121,7 +122,10 @@ class Expression implements Value
@Override @Override
public Value calculate () public Value calculate ()
{ {
// System.out.println (this); // System.out.printf ("Calculating: %s%n", text);
// if (text.equals ("@NA"))
// Utility.printStackTrace ();
try try
{ {
Value thisValue = values.get (0); Value thisValue = values.get (0);
@ -129,6 +133,7 @@ class Expression implements Value
if (thisValue.isError ()) if (thisValue.isError ())
{ {
valueType = thisValue.getValueType (); valueType = thisValue.getValueType ();
System.out.println ("error");
return this; return this;
} }
value = thisValue.isNotAvailable () ? 0 : thisValue.getValue (); value = thisValue.isNotAvailable () ? 0 : thisValue.getValue ();
@ -144,6 +149,7 @@ class Expression implements Value
if (thisValue.isError ()) if (thisValue.isError ())
{ {
valueType = thisValue.getValueType (); valueType = thisValue.getValueType ();
System.out.println ("error");
return this; return this;
} }
@ -171,6 +177,8 @@ class Expression implements Value
{ {
valueType = ValueType.ERROR; valueType = ValueType.ERROR;
} }
// System.out.printf ("Result: %f%n", value);
return this; return this;
} }
@ -292,25 +300,26 @@ class Expression implements Value
@Override @Override
public String toString () public String toString ()
{ {
StringBuilder text = new StringBuilder (); // StringBuilder text = new StringBuilder ();
//
int ptr = 0; // int ptr = 0;
for (Value value : values) // for (Value value : values)
{ // {
assert value != null; // assert value != null;
text.append (signs.get (ptr)); // text.append (signs.get (ptr));
// value.calculate (); // // value.calculate ();
// if (value.isValue ()) // // if (value.isValue ())
// text.append (value.getValue ()); // // text.append (value.getValue ());
if (ptr < operators.size ()) // if (ptr < operators.size ())
{ // {
// System.out.println (operators.get (ptr)); // // System.out.println (operators.get (ptr));
text.append (operators.get (ptr++)); // text.append (operators.get (ptr++));
} // }
} // }
// System.out.println ("finished building"); // // System.out.println ("finished building");
//
return text.toString (); // return text.toString ();
return "Expression: " + text;
} }
public static void main (String[] args) public static void main (String[] args)

View File

@ -85,7 +85,9 @@ abstract class Function implements Value
return new Error (parent, text); return new Error (parent, text);
if (text.equals ("@NA")) if (text.equals ("@NA"))
{
return new Na (parent, text); return new Na (parent, text);
}
System.out.printf ("Unknown function: [%s]%n", text); System.out.printf ("Unknown function: [%s]%n", text);
return new Error (parent, "@ERROR"); return new Error (parent, "@ERROR");
@ -136,6 +138,7 @@ abstract class Function implements Value
@Override @Override
public double getValue () public double getValue ()
{ {
// System.out.printf ("Getting value of : %s %s%n", functionName, functionText);
assert valueType == ValueType.VALUE : "Function ValueType = " + valueType; assert valueType == ValueType.VALUE : "Function ValueType = " + valueType;
return value; return value;
} }

View File

@ -29,11 +29,12 @@ class If extends Function
if (condition.getResult ()) if (condition.getResult ())
{ {
// System.out.println ("true");
if (expTrue == null) if (expTrue == null)
{
expTrue = new Expression (parent, textTrue); expTrue = new Expression (parent, textTrue);
expTrue.calculate (); expTrue.calculate ();
}
if (expTrue.isError () || expTrue.isNotAvailable ()) if (expTrue.isError () || expTrue.isNotAvailable ())
valueType = expTrue.getValueType (); valueType = expTrue.getValueType ();
else else
@ -41,16 +42,18 @@ class If extends Function
} }
else else
{ {
// System.out.println ("false");
if (expFalse == null) if (expFalse == null)
{
expFalse = new Expression (parent, textFalse); expFalse = new Expression (parent, textFalse);
expFalse.calculate (); expFalse.calculate ();
}
if (expFalse.isError () || expFalse.isNotAvailable ()) if (expFalse.isError () || expFalse.isNotAvailable ())
valueType = expFalse.getValueType (); valueType = expFalse.getValueType ();
else else
value = expFalse.getValue (); value = expFalse.getValue ();
} }
return this; return this;
} }

View File

@ -2,21 +2,22 @@ package com.bytezone.diskbrowser.visicalc;
class IsError extends Function class IsError extends Function
{ {
Expression expression; Cell cell;
public IsError (Sheet parent, String text) public IsError (Sheet parent, String text)
{ {
super (parent, text); super (parent, text);
expression = new Expression (parent, functionText);
} }
@Override @Override
public Value calculate () public Value calculate ()
{ {
expression.calculate (); if (cell == null)
// value = expression.getValue (); cell = parent.getCell (functionText);
valueType = expression.getValueType ();
value = isError () ? 1 : 0; value = cell == null ? 1 : 0;
valueType = ValueType.VALUE;
return this; return this;
} }
} }

View File

@ -7,12 +7,15 @@ public class IsNa extends Function
IsNa (Sheet parent, String text) IsNa (Sheet parent, String text)
{ {
super (parent, text); super (parent, text);
expression = new Expression (parent, functionText);
} }
@Override @Override
public Value calculate () public Value calculate ()
{ {
if (expression == null)
expression = new Expression (parent, functionText);
expression.calculate ();
value = expression.getValue (); value = expression.getValue ();
valueType = expression.getValueType (); valueType = expression.getValueType ();
return this; return this;

View File

@ -14,20 +14,25 @@ class Lookup extends Function
int pos = text.indexOf (','); int pos = text.indexOf (',');
sourceText = text.substring (8, pos); sourceText = text.substring (8, pos);
rangeText = text.substring (pos + 1, text.length () - 1); rangeText = text.substring (pos + 1, text.length () - 1);
source = new Expression (parent, sourceText);
range = getRange (rangeText);
} }
@Override @Override
public Value calculate () public Value calculate ()
{ {
if (source == null)
{
source = new Expression (parent, sourceText);
range = getRange (rangeText);
}
source.calculate (); source.calculate ();
// System.out.println ("calculated source");
if (source.isError () || source.isNotAvailable ()) if (source.isError () || source.isNotAvailable ())
{ {
valueType = source.getValueType (); valueType = source.getValueType ();
return this; return this;
} }
double sourceValue = source.getValue (); double sourceValue = source.getValue ();
Address target = null; Address target = null;
@ -40,10 +45,17 @@ class Lookup extends Function
} }
if (target != null) if (target != null)
{
if (range.isVertical ()) if (range.isVertical ())
value = parent.getCell (target.nextColumn ()).getValue (); value = parent.getCell (target.nextColumn ()).getValue ();
else else
value = parent.getCell (target.nextRow ()).getValue (); value = parent.getCell (target.nextRow ()).getValue ();
valueType = ValueType.VALUE;
}
else
{
System.out.println ("Target is null!");
}
return this; return this;
} }

View File

@ -10,7 +10,7 @@ public class Na extends Function
@Override @Override
public boolean isError () public boolean isError ()
{ {
return true; return false;
} }
@Override @Override

View File

@ -178,6 +178,7 @@ public class Sheet
private void processLine (String line) private void processLine (String line)
{ {
assert !line.isEmpty (); assert !line.isEmpty ();
// System.out.println (line);
if (line.startsWith ("/")) if (line.startsWith ("/"))
{ {