Added @SQRT

This commit is contained in:
Denis Molony 2017-03-05 21:42:27 +11:00
parent 2bbe143296
commit 65aa71254c
6 changed files with 102 additions and 66 deletions

View File

@ -24,12 +24,6 @@ public class VisicalcFile extends AbstractFile
text.append ("Cells : " + sheet.size () + "\n\n"); text.append ("Cells : " + sheet.size () + "\n\n");
text.append (sheet.getTextDisplay (debug)); text.append (sheet.getTextDisplay (debug));
if (debug)
{
text.append ("\n");
text.append (sheet.getLines ());
}
return text.toString (); return text.toString ();
} }

View File

@ -146,7 +146,8 @@ class Cell extends AbstractValue implements Comparable<Cell>
case VALUE: case VALUE:
if (!isValueType (ValueType.VALUE)) if (!isValueType (ValueType.VALUE))
{ {
char fmt = cellFormat != ' ' ? cellFormat : globalFormat; char fmt =
cellFormat != ' ' ? cellFormat : globalFormat != ' ' ? globalFormat : 'R';
return Format.justify (value.getText (), colWidth, fmt); return Format.justify (value.getText (), colWidth, fmt);
} }

View File

@ -19,4 +19,11 @@ public class Choose extends Function
values.add (source); values.add (source);
} }
@Override
public void calculate ()
{
source.calculate ();
System.out.println ("@CHOOSE not written yet");
}
} }

View File

@ -92,6 +92,9 @@ abstract class Function extends AbstractValue implements Iterable<Value>
if (text.startsWith ("@SUM(")) if (text.startsWith ("@SUM("))
return new Sum (parent, text); return new Sum (parent, text);
if (text.startsWith ("@SQRT("))
return new Sqrt (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");
} }

View File

@ -21,16 +21,17 @@ public class Sheet
private final Map<Integer, Cell> columnOrderCells = new TreeMap<Integer, Cell> (); private final Map<Integer, Cell> columnOrderCells = new TreeMap<Integer, Cell> ();
private final List<String> lines = new ArrayList<String> (); private final List<String> lines = new ArrayList<String> ();
private char globalFormat = ' ';
private final Map<Integer, Integer> columnWidths = new TreeMap<Integer, Integer> (); private final Map<Integer, Integer> columnWidths = new TreeMap<Integer, Integer> ();
private int columnWidth = 12; private int columnWidth = 9;
private char globalFormat = ' ';
private char recalculation = 'A'; // auto/manual private char recalculation = 'A'; // auto/manual
private char recalculationOrder = 'C'; // row/column private char recalculationOrder = 'C'; // row/column
private int highestColumn; private int minColumn = 9999;
private int highestRow; private int maxColumn;
private int minRow = 9999;
private int maxRow;
// Maximum cell = BK254 // Maximum cell = BK254
@ -164,12 +165,9 @@ public class Sheet
ptr += length + 1; // +1 for end-of-line token ptr += length + 1; // +1 for end-of-line token
} }
System.out.println ("** Start of calculation **");
// might have to keep recalculating until nothing changes?? // might have to keep recalculating until nothing changes??
if (recalculation == 'A') // auto if (recalculation == 'A') // auto
{ {
// recalculationOrder = 'R';
// System.out.printf ("Calculation order: %s%n", recalculationOrder);
calculate (recalculationOrder); calculate (recalculationOrder);
calculate (recalculationOrder); calculate (recalculationOrder);
} }
@ -233,40 +231,35 @@ public class Sheet
} }
// check for formatting commands // check for formatting commands
String format = "";
while (line.startsWith ("/")) while (line.startsWith ("/"))
{ {
if (line.charAt (1) == '-') // repeating label if (line.charAt (1) == '-') // repeating label
{ {
currentCell.setFormat (line); currentCell.setFormat (line);
line = ""; line = "";
format += line;
} }
else else
{ {
String fmt = line.substring (0, FORMAT_LENGTH); currentCell.setFormat (line.substring (0, FORMAT_LENGTH));
line = line.substring (FORMAT_LENGTH); line = line.substring (FORMAT_LENGTH);
currentCell.setFormat (fmt); // formatting command
format += fmt;
} }
} }
// if there is anything left it must be an expression // if there is anything left it must be an expression
if (!line.isEmpty ()) if (!line.isEmpty ())
currentCell.setValue (line); // expression currentCell.setValue (line); // expression
if (false)
System.out.printf ("[%s][%-3s][%s]%n", currentCell.getAddress (), format, line);
} }
private void addCell (Cell cell) private void addCell (Cell cell)
{ {
// System.out.printf ("Adding: %s%n", cell);
rowOrderCells.put (cell.getAddress ().getRowKey (), cell); rowOrderCells.put (cell.getAddress ().getRowKey (), cell);
columnOrderCells.put (cell.getAddress ().getColumnKey (), cell); columnOrderCells.put (cell.getAddress ().getColumnKey (), cell);
highestRow = Math.max (highestRow, cell.getAddress ().getRow ()); minRow = Math.min (minRow, cell.getAddress ().getRow ());
highestColumn = Math.max (highestColumn, cell.getAddress ().getColumn ()); minColumn = Math.min (minColumn, cell.getAddress ().getColumn ());
maxRow = Math.max (maxRow, cell.getAddress ().getRow ());
maxColumn = Math.max (maxColumn, cell.getAddress ().getColumn ());
} }
Cell getCell (String addressText) Cell getCell (String addressText)
@ -279,7 +272,6 @@ public class Sheet
Cell cell = rowOrderCells.get (address.getRowKey ()); Cell cell = rowOrderCells.get (address.getRowKey ());
if (cell == null) if (cell == null)
{ {
// System.out.printf ("cell not found, creating: %s%n", address);
cell = new Cell (this, address); cell = new Cell (this, address);
addCell (cell); addCell (cell);
} }
@ -300,54 +292,41 @@ public class Sheet
{ {
switch (line.charAt (1)) switch (line.charAt (1))
{ {
case 'W':
// System.out.printf ("Skipping [%s]%n", line);
break;
case 'G': case 'G':
switch (line.charAt (2)) setGlobal (line);
{ break;
case 'R': case 'W':
recalculation = line.charAt (3);
break;
case 'O':
recalculationOrder = line.charAt (3);
break;
case 'P':
// System.out.printf ("Skipping [%s]%n", line);
break;
case 'C':
columnWidth = Integer.parseInt (line.substring (3));
break;
case 'F':
globalFormat = line.charAt (3);
break;
default:
System.out.printf ("Unknown global format [%s]%n", line);
break;
}
break; break;
case 'X': case 'X':
// System.out.printf ("Skipping [%s]%n", line);
break; break;
default: default:
System.out.printf ("Skipping [%s]%n", line); System.out.printf ("Skipping [%s]%n", line);
} }
} }
public String getLines () private void setGlobal (String line)
{ {
StringBuilder text = new StringBuilder (); switch (line.charAt (2))
for (String line : lines)
{ {
text.append (line); case 'C':
text.append ("\n"); columnWidth = Integer.parseInt (line.substring (3));
break;
case 'O':
recalculationOrder = line.charAt (3);
break;
case 'R':
recalculation = line.charAt (3);
break;
case 'F':
globalFormat = line.charAt (3);
break;
case 'P':
// System.out.printf ("Skipping [%s]%n", line);
break;
default:
System.out.printf ("Unknown global format [%s]%n", line);
break;
} }
if (text.length () > 0)
text.deleteCharAt (text.length () - 1);
return text.toString ();
} }
public String getTextDisplay (boolean debug) public String getTextDisplay (boolean debug)
@ -367,7 +346,7 @@ public class Sheet
int lastColumn = 0; int lastColumn = 0;
StringBuilder heading = new StringBuilder (" "); StringBuilder heading = new StringBuilder (" ");
for (int column = 0; column <= highestColumn; column++) for (int column = 0; column <= maxColumn; column++)
{ {
int width = columnWidth; int width = columnWidth;
if (columnWidths.containsKey (column)) if (columnWidths.containsKey (column))
@ -386,6 +365,20 @@ public class Sheet
heading.append (String.format (fmt, underline)); heading.append (String.format (fmt, underline));
} }
text.append (String.format ("Global format : %s%n", globalFormat));
text.append (String.format ("Column width : %d%n", columnWidth));
text.append (String.format ("Recalculation order : %s%n",
recalculationOrder == 'R' ? "Row" : "Column"));
text.append (String.format ("Recalculation : %s%n",
recalculation == 'A' ? "Automatic" : "Manual"));
if (rowOrderCells.size () > 0)
text.append (String.format ("Range : %s:%s%n%n",
Address.getCellName (minRow + 1, minColumn),
Address.getCellName (maxRow + 1, maxColumn)));
else
text.append ("\n\n");
if (debug) if (debug)
{ {
text.append (heading); text.append (heading);
@ -443,6 +436,16 @@ public class Sheet
text.append (cell.getDebugText ()); text.append (cell.getDebugText ());
text.append ("\n"); text.append ("\n");
} }
text.append ("\n");
for (String line : lines)
{
text.append (line);
text.append ("\n");
}
if (text.length () > 0)
text.deleteCharAt (text.length () - 1);
} }
return text.toString (); return text.toString ();

View File

@ -0,0 +1,28 @@
package com.bytezone.diskbrowser.visicalc;
public class Sqrt extends Function
{
private final Expression source;
Sqrt (Sheet parent, String text)
{
super (parent, text);
source = new Expression (parent, text.substring (5, text.length () - 1));
values.add (source);
}
@Override
public void calculate ()
{
source.calculate ();
if (!source.isValueType (ValueType.VALUE))
{
valueType = source.getValueType ();
return;
}
value = Math.sqrt (source.getValue ());
valueType = ValueType.VALUE;
}
}