expressing

This commit is contained in:
Denis Molony 2016-03-11 12:52:22 +11:00
parent 7a7825d470
commit 86605c7ecb
5 changed files with 92 additions and 48 deletions

View File

@ -22,6 +22,7 @@ public class VisicalcFile extends AbstractFile
text.append ("Visicalc : " + name + "\n");
text.append ("Cells : " + sheet.size () + "\n\n");
// sheet.getCells ();
text.append (sheet.getCells ());
return text.toString ();

View File

@ -83,6 +83,20 @@ class Cell implements Comparable<Cell>, Value
expressionText = "1000";
else if (address.sortValue == 386)
expressionText = "15";
// CARLOAN.VC
if (false)
if (address.sortValue == 67)
expressionText = "9375";
else if (address.sortValue == 131)
expressionText = "4500";
else if (address.sortValue == 195)
expressionText = "24";
else if (address.sortValue == 259)
expressionText = "11.9";
else if (address.sortValue == 579)
expressionText = "D9*G5/(1-((1+G5)^-D4))";
}
boolean hasValue ()
@ -101,7 +115,7 @@ class Cell implements Comparable<Cell>, Value
return label;
if (repeatingChar > 0)
return repeat;
return "bollocks";
return "?";
}
@Override
@ -109,6 +123,11 @@ class Cell implements Comparable<Cell>, Value
{
if (expression == null)
{
if (expressionText == null)
{
System.out.println ("null expression text");
return 0;
}
System.out.printf ("%s Instantiating [%s]%n", address, expressionText);
expression = new Expression (parent, expressionText);
}

View File

@ -19,10 +19,9 @@ public class Expression implements Value
// parentheses. You must start an expression with a +, a digit (0-9), or one of
// the symbols @-(. or #.
// @IF(D5=0,0,D9*(G5/(1-((1+G5)^-D4))
private final List<Value> values = new ArrayList<Value> ();
private final List<String> operators = new ArrayList<String> ();
private final List<String> signs = new ArrayList<String> ();
public Expression (Sheet parent, String input)
{
@ -30,35 +29,40 @@ public class Expression implements Value
System.out.printf ("New expression [%s]%n", input);
if (true)
int leftBracket = 0;
int rightBracket = 0;
for (char c : input.toCharArray ())
if (c == '(')
leftBracket++;
else if (c == ')')
rightBracket++;
if (leftBracket != rightBracket)
{
int leftBracket = 0;
int rightBracket = 0;
for (char c : input.toCharArray ())
{
if (c == '(')
leftBracket++;
if (c == ')')
rightBracket++;
}
if (leftBracket != rightBracket)
{
System.out.printf ("Unbalanced brackets: left:%d, right:%d%n", leftBracket,
rightBracket);
line = "@ERROR()";
}
System.out.printf ("**** Unbalanced brackets: left:%d, right:%d ****%n",
leftBracket, rightBracket);
line = "@ERROR()";
}
if (line.startsWith ("-"))
line = "0" + line;
else if (line.startsWith ("+"))
line = line.substring (1);
// System.out.printf ("Exp [%s]%n", line);
int ptr = 0;
while (ptr < line.length ())
{
char ch = line.charAt (ptr);
if (ch == '-')
{
signs.add ("(-)");
ch = line.charAt (++ptr);
}
else
{
signs.add ("(+)");
if (ch == '+')
ch = line.charAt (++ptr);
}
switch (ch)
{
case '@': // function
@ -74,6 +78,10 @@ public class Expression implements Value
values.add (new Expression (parent, bracketText));
break;
case '#':
System.out.printf ("Hash character [%s] in [%s]%n", ch, line);
break;
default:
if (ch == '.' || (ch >= '0' && ch <= '9')) // number
{
@ -114,7 +122,11 @@ public class Expression implements Value
ptr = 0;
for (Value val : values)
{
System.out.println (val.getValue ());
System.out.println (signs.get (ptr));
if (val == null)
System.out.println ("null");
else
System.out.println (val.getValue ());
if (ptr < operators.size ())
System.out.println (operators.get (ptr++));
}
@ -124,10 +136,22 @@ public class Expression implements Value
@Override
public double getValue ()
{
double value = values.get (0).getValue ();
Value thisValue = values.get (0);
double value = thisValue == null ? 0 : values.get (0).getValue ();
String sign = signs.get (0);
if (sign.equals ("(-)"))
value *= -1;
for (int i = 1; i < values.size (); i++)
{
double nextValue = values.get (i).getValue ();
thisValue = values.get (i);
double nextValue = thisValue == null ? 0 : thisValue.getValue ();
sign = signs.get (i);
if (sign.equals ("(-)"))
nextValue *= -1;
String operator = operators.get (i - 1);
if (operator.equals ("+"))
value += nextValue;
@ -146,6 +170,8 @@ public class Expression implements Value
private String getFunctionText (String text)
{
int ptr = text.indexOf ('('); // find first left parenthesis
if (ptr < 0)
return "";
int depth = 1;
while (++ptr < text.length ()) // find matching right parenthesis
{
@ -190,16 +216,10 @@ public class Expression implements Value
{
StringBuilder text = new StringBuilder ();
// text.append (String.format ("Has value ......... %s%n", hasValue));
// text.append (String.format ("Value ............. %f%n", value));
// text.append (String.format ("Function .......... %s%n", function));
// text.append (String.format ("Address ........... %s%n", address));
// text.append (String.format ("Operator .......... %s%n", operator));
// text.append (String.format ("Expression1 ....... %s%n", expression1));
// text.append (String.format ("Expression2 ....... %s%n", expression2));
int ptr = 0;
for (Value value : values)
{
text.append (signs.get (ptr));
text.append (value.getValue ());
if (ptr < operators.size ())
text.append (operators.get (ptr++));
@ -210,7 +230,8 @@ public class Expression implements Value
public static void main (String[] args)
{
Expression ex = new Expression (null, "5+((4-(10-2)+6/3))*2");
Expression ex = new Expression (null, "-5+((-4-(20-(2^3))+6/3))*-2");
System.out.println (ex.getValue ());
System.out.println (ex);
}
}

View File

@ -24,13 +24,9 @@ import java.util.regex.Pattern;
// @TAN
// @ATAN
// Unimplemented functions found so far:
// @OR
// @AND
public abstract class Function implements Value
abstract class Function implements Value
{
private static final Pattern functionPattern = Pattern
private static final Pattern rangePattern = Pattern
.compile ("\\(([A-B]?[A-Z])([0-9]{1,3})\\.\\.\\.([A-B]?[A-Z])([0-9]{1,3})\\)?");
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
@ -66,22 +62,27 @@ public abstract class Function implements Value
if (text.startsWith ("@ISERROR("))
return new IsError (parent, text);
if (text.startsWith ("@ERROR("))
return new Error (parent, text);
System.out.printf ("Unknown function: %s%n", text);
return new Error (parent, "@ERROR()");
}
public Function (Sheet parent, String text)
Function (Sheet parent, String text)
{
this.parent = parent;
// get function's parameter string
int pos = text.indexOf ('(');
this.functionText = text.substring (pos + 1, text.length () - 1);
}
Range getRange (String text)
protected Range getRange (String text)
{
Range range = null;
Matcher m = functionPattern.matcher (text);
while (m.find ())
Matcher m = rangePattern.matcher (text);
if (m.find ())
{
Address fromAddress = new Address (m.group (1), m.group (2));
Address toAddress = new Address (m.group (3), m.group (4));
@ -92,7 +93,7 @@ public abstract class Function implements Value
return range;
m = addressList.matcher (text);
while (m.find ())
if (m.find ())
{
String[] cells = m.group (1).split (",");
range = new Range (cells);
@ -112,7 +113,7 @@ public abstract class Function implements Value
}
if (range == null)
System.out.println ("null range : " + text);
System.out.printf ("null range [%s]%n", text);
return range;
}

View File

@ -345,6 +345,8 @@ public class Sheet implements Iterable<Cell>
else
longLine = " "
+ " ";
String underline = "---------------------------------------------------------"
+ "-----------------------------------------------------------------";
DecimalFormat nf = new DecimalFormat ("$#####0.00");
// NumberFormat nf = NumberFormat.getCurrencyInstance ();
@ -368,7 +370,7 @@ public class Sheet implements Iterable<Cell>
char letter2 = (char) ((cellNo % 26) + 'A');
String fmt =
String.format ("%s%s%%%d.%ds", letter1, letter2, (width - 2), (width - 2));
heading.append (String.format (fmt, "--------------------------------------"));
heading.append (String.format (fmt, underline));
}
}
text.append (heading);