removed Sheet from Function constructor

This commit is contained in:
Denis Molony 2017-03-18 19:33:40 +11:00
parent c532516f23
commit 76a220acd3
34 changed files with 247 additions and 223 deletions

View File

@ -4,9 +4,9 @@ public class Abs extends Function
{
private final Expression source;
Abs (Sheet parent, Cell cell, String text)
Abs (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
source = new Expression (parent, cell, functionText);
values.add (source);

View File

@ -4,9 +4,9 @@ public class Acos extends Function
{
Value v;
Acos (Sheet parent, Cell cell, String text)
Acos (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;

View File

@ -7,9 +7,9 @@ class And extends Function
{
private final List<Condition> conditions = new ArrayList<Condition> ();
public And (Sheet parent, Cell cell, String text)
public And (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
String remainder = functionText;
while (true)

View File

@ -4,9 +4,9 @@ public class Asin extends Function
{
Value v;
Asin (Sheet parent, Cell cell, String text)
Asin (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;

View File

@ -4,9 +4,9 @@ public class Atan extends Function
{
Value v;
Atan (Sheet parent, Cell cell, String text)
Atan (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;

View File

@ -7,9 +7,9 @@ public class Average extends Function
private final ValueList list;
private final boolean isRange; // may affect how the count is done
public Average (Sheet parent, Cell cell, String text)
public Average (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
list = new ValueList (parent, cell, functionText);
isRange = functionText.indexOf ("...") > 0;

View File

@ -38,6 +38,11 @@ class Cell extends AbstractValue implements Comparable<Cell>
return this.cellType == cellType;
}
Sheet getParent ()
{
return parent;
}
@Override
public boolean isVolatile ()
{

View File

@ -7,9 +7,9 @@ public class Choose extends Function
private final String rangeText;
private final Value source;
Choose (Sheet parent, Cell cell, String text)
Choose (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
// int pos = functionText.indexOf (',');
// sourceText = functionText.substring (0, pos);

View File

@ -4,9 +4,9 @@ public class Cos extends Function
{
Value v;
Cos (Sheet parent, Cell cell, String text)
Cos (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;

View File

@ -7,9 +7,9 @@ class Count extends Function
private final ValueList list;
private final boolean isRange;
public Count (Sheet parent, Cell cell, String text)
public Count (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
list = new ValueList (parent, cell, functionText);
isRange = functionText.indexOf ("...") > 0;

View File

@ -2,9 +2,9 @@ package com.bytezone.diskbrowser.visicalc;
class Error extends Function
{
public Error (Sheet parent, Cell cell, String text)
public Error (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
valueType = ValueType.ERROR;
}
}

View File

@ -4,9 +4,9 @@ public class Exp extends Function
{
Value v;
Exp (Sheet parent, Cell cell, String text)
Exp (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;

View File

@ -41,7 +41,7 @@ class Expression extends AbstractValue implements Iterable<Value>
this.cell = cell;
this.text = text;
String line = balanceBrackets (text); // add trailing right brackets if required
String line = balanceBrackets (text); // add trailing right brackets if necessary
int ptr = 0;
while (ptr < line.length ())
@ -66,7 +66,7 @@ class Expression extends AbstractValue implements Iterable<Value>
case '@': // function
String functionText = getFunctionCall (line.substring (ptr));
ptr += functionText.length ();
values.add (Function.getInstance (parent, cell, functionText));
values.add (parent.getFunction (cell, functionText));
break;
case '(': // parentheses block
@ -116,8 +116,6 @@ class Expression extends AbstractValue implements Iterable<Value>
}
assert values.size () > 0;
// if (values.size () == 0)
// System.out.printf ("Nothing[%s]%n", text);
}
Value reduce ()
@ -146,7 +144,6 @@ class Expression extends AbstractValue implements Iterable<Value>
return;
}
// System.out.printf (" calc %-6s %s%n", cell.getAddressText (), text);
if (!isVolatile)
return;
@ -331,7 +328,7 @@ class Expression extends AbstractValue implements Iterable<Value>
return text.substring (0, ptr);
}
// receives a string starting with the function call
// receives a string starting with the function name
private String getFunctionCall (String text)
{
if (text.charAt (0) != '@')
@ -341,7 +338,7 @@ class Expression extends AbstractValue implements Iterable<Value>
if (text.startsWith (functionName))
{
if (functionName.endsWith ("(")) // if function has parameters
return getBalancedText (text); // return full function call
return getBalancedText (text); // return full function call
return functionName; // return function name only
}
@ -387,20 +384,6 @@ class Expression extends AbstractValue implements Iterable<Value>
}
return text.toString ();
}
@Override
public String toString ()
{
return "Expression : " + text;
}
public static void main (String[] args)
{
Expression ex = new Expression (null, null, "-5+((-4-(20-(2^3))+6/3))*-2");
System.out.println (ex.getValue ());
System.out.println (ex);
}
@Override
@ -408,4 +391,10 @@ class Expression extends AbstractValue implements Iterable<Value>
{
return values.iterator ();
}
@Override
public String toString ()
{
return "Expression : " + text;
}
}

View File

@ -2,9 +2,9 @@ package com.bytezone.diskbrowser.visicalc;
public class False extends Function
{
False (Sheet parent, Cell cell, String text)
False (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
value = 0;
valueType = ValueType.VALUE;

View File

@ -16,137 +16,10 @@ abstract class Function extends AbstractValue implements Iterable<Value>
protected String functionText;
protected String fullText;
static Function getInstance (Sheet parent, Cell cell, String text)
{
if (text.charAt (0) != '@')
{
System.out.printf ("Unknown function: [%s]%n", text);
return new Error (parent, cell, "@ERROR");
}
if (text.charAt (1) == 'A')
{
if (text.startsWith ("@ABS("))
return new Abs (parent, cell, text);
if (text.startsWith ("@ACOS("))
return new Acos (parent, cell, text);
if (text.startsWith ("@AND("))
return new And (parent, cell, text);
if (text.startsWith ("@ASIN("))
return new Asin (parent, cell, text);
if (text.startsWith ("@ATAN("))
return new Atan (parent, cell, text);
if (text.startsWith ("@AVERAGE("))
return new Average (parent, cell, text);
}
else if (text.charAt (1) == 'C')
{
if (text.startsWith ("@COUNT("))
return new Count (parent, cell, text);
if (text.startsWith ("@CHOOSE("))
return new Choose (parent, cell, text);
if (text.startsWith ("@COS("))
return new Cos (parent, cell, text);
}
else if (text.charAt (1) == 'E')
{
if (text.startsWith ("@ERROR"))
return new Error (parent, cell, text);
if (text.startsWith ("@EXP("))
return new Exp (parent, cell, text);
}
else if (text.charAt (1) == 'F')
{
if (text.startsWith ("@FALSE"))
return new False (parent, cell, text);
}
else if (text.charAt (1) == 'I')
{
if (text.startsWith ("@IF("))
return new If (parent, cell, text);
if (text.startsWith ("@INT("))
return new Int (parent, cell, text);
if (text.startsWith ("@ISERROR("))
return new IsError (parent, cell, text);
if (text.startsWith ("@ISNA("))
return new IsNa (parent, cell, text);
}
else if (text.charAt (1) == 'L')
{
if (text.startsWith ("@LOG10("))
return new Log10 (parent, cell, text);
if (text.startsWith ("@LOOKUP("))
return new Lookup (parent, cell, text);
if (text.startsWith ("@LN("))
return new Ln (parent, cell, text);
}
else if (text.charAt (1) == 'M')
{
if (text.startsWith ("@MIN("))
return new Min (parent, cell, text);
if (text.startsWith ("@MAX("))
return new Max (parent, cell, text);
}
else if (text.charAt (1) == 'N')
{
if (text.equals ("@NA"))
return new Na (parent, cell, text);
if (text.startsWith ("@NPV("))
return new Npv (parent, cell, text);
}
else if (text.charAt (1) == 'O')
{
if (text.startsWith ("@OR("))
return new Or (parent, cell, text);
}
else if (text.charAt (1) == 'P')
{
if (text.startsWith ("@PI"))
return new Pi (parent, cell, text);
}
else if (text.charAt (1) == 'S')
{
if (text.startsWith ("@SIN("))
return new Sin (parent, cell, text);
if (text.startsWith ("@SUM("))
return new Sum (parent, cell, text);
if (text.startsWith ("@SQRT("))
return new Sqrt (parent, cell, text);
}
else if (text.charAt (1) == 'T')
{
if (text.startsWith ("@TAN("))
return new Tan (parent, cell, text);
if (text.startsWith ("@TRUE"))
return new True (parent, cell, text);
}
System.out.printf ("Unknown function: [%s]%n", text);
return new Error (parent, cell, "@ERROR");
}
Function (Sheet parent, Cell cell, String text)
Function (Cell cell, String text)
{
super ("Function");
this.parent = parent;
this.parent = cell.getParent ();
this.cell = cell;
fullText = text;

View File

@ -10,9 +10,9 @@ class If extends Function
private final Expression expTrue;
private final Expression expFalse;
public If (Sheet parent, Cell cell, String text)
public If (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
conditionText = Expression.getParameter (functionText);
textTrue =

View File

@ -4,9 +4,9 @@ public class Int extends Function
{
Expression source;
Int (Sheet parent, Cell cell, String text)
Int (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
source = new Expression (parent, cell, functionText);
values.add (source);

View File

@ -4,9 +4,9 @@ class IsError extends Function
{
Value expression;
public IsError (Sheet parent, Cell cell, String text)
public IsError (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
expression = new Expression (parent, cell, functionText).reduce ();
}

View File

@ -4,9 +4,9 @@ public class IsNa extends Function
{
Value expression;
IsNa (Sheet parent, Cell cell, String text)
IsNa (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
expression = new Expression (parent, cell, functionText).reduce ();
}

View File

@ -4,9 +4,9 @@ public class Ln extends Function
{
Value v;
Ln (Sheet parent, Cell cell, String text)
Ln (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;

View File

@ -4,9 +4,9 @@ public class Log10 extends Function
{
Value v;
Log10 (Sheet parent, Cell cell, String text)
Log10 (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;

View File

@ -7,16 +7,18 @@ class Lookup extends Function
private final Expression source;
private final Range range;
public Lookup (Sheet parent, Cell cell, String text)
public Lookup (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
int pos = text.indexOf (',');
// int pos = text.indexOf (',');
sourceText = Expression.getParameter (functionText);
sourceText = text.substring (8, pos);
// sourceText = text.substring (8, pos);
source = new Expression (parent, cell, sourceText);
rangeText = text.substring (pos + 1, text.length () - 1);
// rangeText = text.substring (pos + 1, text.length () - 1);
rangeText = functionText.substring (sourceText.length () + 1);
range = new Range (parent, cell, rangeText);
values.add (source);

View File

@ -4,9 +4,9 @@ class Max extends Function
{
private final ValueList list;
public Max (Sheet parent, Cell cell, String text)
public Max (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
list = new ValueList (parent, cell, functionText);
}

View File

@ -4,9 +4,9 @@ class Min extends Function
{
private final ValueList list;
public Min (Sheet parent, Cell cell, String text)
public Min (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
list = new ValueList (parent, cell, functionText);
}

View File

@ -2,9 +2,9 @@ package com.bytezone.diskbrowser.visicalc;
public class Na extends Function
{
public Na (Sheet parent, Cell cell, String text)
public Na (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
valueType = ValueType.NA;
}
}

View File

@ -10,9 +10,9 @@ public class Npv extends Function
private final Expression rateExp;
private final Range range;
Npv (Sheet parent, Cell cell, String text)
Npv (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
int pos = text.indexOf (',');
valueText = text.substring (5, pos);

View File

@ -7,9 +7,9 @@ class Or extends Function
{
private final List<Condition> conditions = new ArrayList<Condition> ();
public Or (Sheet parent, Cell cell, String text)
public Or (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
String remainder = functionText;
while (true)

View File

@ -2,9 +2,9 @@ package com.bytezone.diskbrowser.visicalc;
class Pi extends Function
{
Pi (Sheet parent, Cell cell, String text)
Pi (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
value = Math.PI;
valueType = ValueType.VALUE;

View File

@ -33,6 +33,8 @@ public class Sheet
private int minRow = 9999;
private int maxRow;
int[] functionTotals = new int[Function.functionList.length];
// Maximum cell = BK254
//Commands:
@ -384,18 +386,37 @@ public class Sheet
}
}
text.append (String.format ("Global format : %s%n", globalFormat));
text.append (String.format ("Column width : %d%n", columnWidth));
text.append (String.format ("Recalc order : %s%n",
recalculationOrder == 'R' ? "Row" : "Column"));
text.append (String.format ("Recalculation : %s%n",
recalculation == 'A' ? "Automatic" : "Manual"));
text.append (String.format ("Cells : %d%n", size ()));
List<String> counts = new ArrayList<String> ();
for (int i = 0; i < functionTotals.length; i++)
if (functionTotals[i] > 0)
{
String name = Function.functionList[i];
if (name.endsWith ("("))
name = name.substring (0, name.length () - 1);
counts.add (String.format ("%-10s %d", name, functionTotals[i]));
}
while (counts.size () < 18)
counts.add ("");
text.append (String.format ("Global format : %-18s %-18s %-18s %s%n", globalFormat,
counts.get (0), counts.get (6), counts.get (12)));
text.append (String.format ("Column width : %-2d %-15s %-18s %-18s %s%n",
columnWidth, "", counts.get (1), counts.get (7), counts.get (13)));
text.append (String.format ("Recalc order : %-18s %-18s %-18s %s%n",
recalculationOrder == 'R' ? "Row" : "Column", counts.get (2), counts.get (8),
counts.get (14)));
text.append (String.format ("Recalculation : %-18s %-18s %-18s %s%n",
recalculation == 'A' ? "Automatic" : "Manual", counts.get (3), counts.get (9),
counts.get (15)));
text.append (String.format ("Cells : %-5d %-11s %-18s %-18s %s%n", size (),
"", counts.get (4), counts.get (10), counts.get (16)));
if (size () > 0)
text.append (String.format ("Range : %s:%s%n%n",
Address.getCellName (minRow + 1, minColumn),
Address.getCellName (maxRow + 1, maxColumn)));
text.append (String.format ("Range : %-18s %-18s %-18s %s%n%n",
Address.getCellName (minRow + 1, minColumn) + ":"
+ Address.getCellName (maxRow + 1, maxColumn),
counts.get (5), counts.get (11), counts.get (17)));
else
text.append ("\n\n");
@ -470,4 +491,138 @@ public class Sheet
return text.toString ();
}
Function getFunction (Cell cell, String text)
{
if (text.charAt (0) != '@')
{
System.out.printf ("Unknown function: [%s]%n", text);
return new Error (cell, "@ERROR");
}
for (int i = 0; i < Function.functionList.length; i++)
if (text.startsWith (Function.functionList[i]))
{
functionTotals[i]++;
break;
}
if (text.charAt (1) == 'A')
{
if (text.startsWith ("@ABS("))
return new Abs (cell, text);
if (text.startsWith ("@ACOS("))
return new Acos (cell, text);
if (text.startsWith ("@AND("))
return new And (cell, text);
if (text.startsWith ("@ASIN("))
return new Asin (cell, text);
if (text.startsWith ("@ATAN("))
return new Atan (cell, text);
if (text.startsWith ("@AVERAGE("))
return new Average (cell, text);
}
else if (text.charAt (1) == 'C')
{
if (text.startsWith ("@COUNT("))
return new Count (cell, text);
if (text.startsWith ("@CHOOSE("))
return new Choose (cell, text);
if (text.startsWith ("@COS("))
return new Cos (cell, text);
}
else if (text.charAt (1) == 'E')
{
if (text.startsWith ("@ERROR"))
return new Error (cell, text);
if (text.startsWith ("@EXP("))
return new Exp (cell, text);
}
else if (text.charAt (1) == 'F')
{
if (text.startsWith ("@FALSE"))
return new False (cell, text);
}
else if (text.charAt (1) == 'I')
{
if (text.startsWith ("@IF("))
return new If (cell, text);
if (text.startsWith ("@INT("))
return new Int (cell, text);
if (text.startsWith ("@ISERROR("))
return new IsError (cell, text);
if (text.startsWith ("@ISNA("))
return new IsNa (cell, text);
}
else if (text.charAt (1) == 'L')
{
if (text.startsWith ("@LOG10("))
return new Log10 (cell, text);
if (text.startsWith ("@LOOKUP("))
return new Lookup (cell, text);
if (text.startsWith ("@LN("))
return new Ln (cell, text);
}
else if (text.charAt (1) == 'M')
{
if (text.startsWith ("@MIN("))
return new Min (cell, text);
if (text.startsWith ("@MAX("))
return new Max (cell, text);
}
else if (text.charAt (1) == 'N')
{
if (text.equals ("@NA"))
return new Na (cell, text);
if (text.startsWith ("@NPV("))
return new Npv (cell, text);
}
else if (text.charAt (1) == 'O')
{
if (text.startsWith ("@OR("))
return new Or (cell, text);
}
else if (text.charAt (1) == 'P')
{
if (text.startsWith ("@PI"))
return new Pi (cell, text);
}
else if (text.charAt (1) == 'S')
{
if (text.startsWith ("@SIN("))
return new Sin (cell, text);
if (text.startsWith ("@SUM("))
return new Sum (cell, text);
if (text.startsWith ("@SQRT("))
return new Sqrt (cell, text);
}
else if (text.charAt (1) == 'T')
{
if (text.startsWith ("@TAN("))
return new Tan (cell, text);
if (text.startsWith ("@TRUE"))
return new True (cell, text);
}
System.out.printf ("Unknown function: [%s]%n", text);
return new Error (cell, "@ERROR");
}
}

View File

@ -4,9 +4,9 @@ public class Sin extends Function
{
Value v;
Sin (Sheet parent, Cell cell, String text)
Sin (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;

View File

@ -4,9 +4,9 @@ public class Sqrt extends Function
{
private final Expression source;
Sqrt (Sheet parent, Cell cell, String text)
Sqrt (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
source = new Expression (parent, cell, text.substring (5, text.length () - 1));
values.add (source);
}

View File

@ -4,9 +4,9 @@ class Sum extends Function
{
private final ValueList list;
public Sum (Sheet parent, Cell cell, String text)
public Sum (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
list = new ValueList (parent, cell, functionText);
}

View File

@ -4,9 +4,9 @@ public class Tan extends Function
{
Value v;
Tan (Sheet parent, Cell cell, String text)
Tan (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
v = new Expression (parent, cell, functionText).reduce ();
valueType = ValueType.VALUE;

View File

@ -2,9 +2,9 @@ package com.bytezone.diskbrowser.visicalc;
public class True extends Function
{
True (Sheet parent, Cell cell, String text)
True (Cell cell, String text)
{
super (parent, cell, text);
super (cell, text);
value = 1;
valueType = ValueType.VALUE;