@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)
{
// cell may have been created when formatted but no type set
// if (type == null)
// return justify ("", colWidth);
switch (type)
{
case LABEL:
@ -192,14 +188,14 @@ class Cell implements Comparable<Cell>, Value
@Override
public String getText ()
{
assert type == CellType.VALUE : "Cell type: " + type;
assert isValue () : "Cell type: " + type;
return value.getText ();
}
@Override
public boolean isError ()
{
assert type == CellType.VALUE : "Cell type: " + type;
assert isValue () : "Cell type: " + type;
return value.isError ();
}
@ -235,20 +231,20 @@ class Cell implements Comparable<Cell>, Value
@Override
public String toString ()
{
String contents = "<empty>";
if (type != null)
switch (type)
{
case LABEL:
contents = "Labl: " + label;
break;
case REPEATING_CHARACTER:
contents = "Rept: " + repeatingChar;
break;
case VALUE:
contents = "Exp : " + expressionText;
break;
}
String contents = "";
switch (type)
{
case LABEL:
contents = "Labl: " + label;
break;
case REPEATING_CHARACTER:
contents = "Rept: " + repeatingChar;
break;
case VALUE:
contents = "Exp : " + expressionText;
break;
}
return String.format ("[Cell:%5s %s]", address, contents);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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