This commit is contained in:
Denis Molony 2016-03-10 13:39:23 +11:00
parent 239820c41e
commit cd9d16f0c4
6 changed files with 114 additions and 79 deletions

View File

@ -37,6 +37,14 @@ class Address implements Comparable<Address>
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)
{
if (sCol.length () == 1)
@ -55,6 +63,8 @@ class Address implements Comparable<Address>
catch (NumberFormatException e)
{
System.out.printf ("NFE: %s%n", sRow);
// for (StackTraceElement ste : Thread.currentThread ().getStackTrace ())
// System.out.println (ste);
}
}

View File

@ -34,7 +34,8 @@ class Cell implements Comparable<Cell>, Value
// /F$ - dollars and cents
// /FL - left justified
// /FR - right justified
// /F* - graph
// /F* - graph (histogram)
if (format.startsWith ("/F"))
this.format = format.charAt (2);
else if (format.startsWith ("/-"))
@ -61,13 +62,13 @@ class Cell implements Comparable<Cell>, Value
// FUTURE.VC
if (address.sortValue == 67)
expressionText = "50";
expressionText = "1000";
if (address.sortValue == 131)
expressionText = ".04";
expressionText = "10.5";
if (address.sortValue == 195)
expressionText = "12";
if (address.sortValue == 259)
expressionText = "5";
expressionText = "8";
}
boolean hasValue ()
@ -93,21 +94,12 @@ class Cell implements Comparable<Cell>, Value
public double getValue ()
{
if (expression == null)
{
System.out.printf ("%s Instantiating [%s]%n", address, expressionText);
expression = new Expression (parent, expressionText);
}
return expression.getValue ();
// if (valid || formulaText == null)
// return value;
//
// double result = 0.0;
// double interim = 0.0;
//
// if (formulaText.startsWith ("@LOOKUP("))
// {
// Lookup lookup = new Lookup (parent, formulaText);
// return lookup.getValue ();
// }
//
// System.out.printf ("Matching:[%s]%n", formulaText);
// [@IF(@ISERROR(BK24),0,BK24)]
// [@IF(D4=0,0,1)]
// [@IF(D4=0,0,B32+1)]
@ -117,65 +109,11 @@ class Cell implements Comparable<Cell>, Value
// [+D5/100/12]
// [.3*(B4+B7+B8+B9)]
// [+N12+(P12*(.2*K12+K9-O12))]
// Matcher m = cellContents.matcher (formulaText);
// while (m.find ())
// {
// valid = true;
// char operator = m.group (1).isEmpty () ? '+' : m.group (1).charAt (0);
//
// if (m.group (3) != null) // address
// {
// Address address = new Address (m.group (3));
// Cell cell = parent.getCell (address);
// if (cell != null)
// interim = cell.getValue ();
// }
// else if (m.group (4) != null) // constant
// try
// {
// interim = Double.parseDouble (m.group (4));
// }
// catch (NumberFormatException e)
// {
// System.out.printf ("NFE: %s [%s]%n", m.group (4), formulaText);
// }
// else
// {
// // interim = parent.evaluateFunction (m.group (5)); // function
// Function function = Function.getInstance (parent, m.group (5));
// if (function != null)
// interim = function.getValue ();
// }
//
// if (operator == '+')
// result += interim;
// else if (operator == '-')
// result -= interim;
// else if (operator == '*')
// result *= interim;
// else if (operator == '/')
// result = interim == 0.0 ? 0 : result / interim;
// }
//
// if (valid)
// {
// value = result;
// return result;
// }
//
// System.out.println ("?? " + formulaText);
//
// return value;
}
@Override
public String toString ()
{
// String value = repeatingChar == 0
// ? label == null ? formulaText == null ? ", Value : " + this.value
// : ", Formula: " + formulaText : ", Label : " + label
// : ", Repeat : " + repeatingChar;
String contents = "";
if (label != null)
contents = "Labl: " + label;

View File

@ -37,7 +37,7 @@ public abstract class Function implements Value
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
Sheet parent;
String text;
String functionText;
static Function getInstance (Sheet parent, String text)
{
@ -59,6 +59,9 @@ public abstract class Function implements Value
if (text.startsWith ("@IF("))
return new If (parent, text);
if (text.startsWith ("@ISERROR("))
return new IsError (parent, text);
System.out.printf ("Unknown function: %s%n", text);
return new Error (parent, "@ERROR()");
}
@ -66,7 +69,8 @@ public abstract class Function implements Value
public Function (Sheet parent, String text)
{
this.parent = parent;
this.text = text;
int pos = text.indexOf ('(');
this.functionText = text.substring (pos + 1, text.length () - 1);
}
Range getRange (String text)
@ -113,6 +117,6 @@ public abstract class Function implements Value
@Override
public String toString ()
{
return String.format ("Function: %s", text);
return String.format ("Function: %s", functionText);
}
}

View File

@ -11,6 +11,11 @@ public class If extends Function
private String cond;
private String value;
private Expression expTrue;
private Expression expFalse;
private Expression expCond;
private Expression expValue;
public If (Sheet parent, String text)
{
super (parent, text);
@ -22,6 +27,7 @@ public class If extends Function
condition = text.substring (0, pos1);
textTrue = text.substring (pos1 + 1, pos2);
textFalse = text.substring (pos2 + 1);
System.out.printf ("Cond:%s, true=%s, false=%s%n", condition, textTrue, textFalse);
for (String comp : comparators)
@ -35,12 +41,65 @@ public class If extends Function
break;
}
}
if (comparator == null)
{
if (condition.startsWith ("@"))
{
cond = condition;
value = "1";
comparator = "=";
}
else
System.out.println ("No comparator and not a function");
}
System.out.printf ("cond=%s, op=%s, value=%s%n", cond, comparator, value);
}
@Override
public double getValue ()
{
return 0;
if (expCond == null)
{
expCond = new Expression (parent, cond);
expValue = new Expression (parent, value);
}
double condValue = expCond.getValue ();
double valueValue = expValue.getValue ();
boolean result;
if (comparator.equals ("="))
result = condValue == valueValue;
else if (comparator.equals ("<>"))
result = condValue != valueValue;
else if (comparator.equals ("<"))
result = condValue < valueValue;
else if (comparator.equals (">"))
result = condValue > valueValue;
else if (comparator.equals ("<="))
result = condValue <= valueValue;
else if (comparator.equals (">="))
result = condValue >= valueValue;
else
{
System.out.printf ("Unexpected comparator result [%s]%n", comparator);
return 0;
}
if (result)
{
if (expTrue == null)
expTrue = new Expression (parent, textTrue);
return expTrue.getValue ();
}
else
{
if (expFalse == null)
expFalse = new Expression (parent, textFalse);
return expFalse.getValue ();
}
}
}

View File

@ -0,0 +1,23 @@
package com.bytezone.diskbrowser.visicalc;
public class IsError extends Function
{
boolean firstTime = true;
Cell cell;
public IsError (Sheet parent, String text)
{
super (parent, text);
}
@Override
public double getValue ()
{
if (firstTime)
{
firstTime = false;
cell = parent.getCell (new Address (functionText));
}
return cell == null ? 1 : 0;
}
}

View File

@ -317,8 +317,9 @@ public class Sheet implements Iterable<Cell>
Cell cell = sheet.get (address.sortValue);
if (cell == null)
{
cell = new Cell (this, address);
sheet.put (address.sortValue, cell);
// cell = new Cell (this, address);
// sheet.put (address.sortValue, cell);
System.out.printf ("Nonexistent cell requested [%s]%n", address);
}
return cell;
}
@ -358,9 +359,9 @@ public class Sheet implements Iterable<Cell>
width = columnWidths.get (cellNo);
if (width == 1)
{
heading.append ("=");
}
else if (width == 2)
heading.append ("==");
else
{
char letter1 = cellNo < 26 ? ' ' : cellNo < 676 ? 'A' : 'B';