added Condition

This commit is contained in:
Denis Molony 2016-03-13 09:33:18 +11:00
parent 5408d01b9c
commit de682dc190
3 changed files with 87 additions and 74 deletions

View File

@ -83,7 +83,7 @@ class Cell implements Comparable<Cell>, Value
expressionText = "15";
// CARLOAN.VC
if (false)
if (true)
if (address.sortValue == 67)
expressionText = "9375";
else if (address.sortValue == 131)

View File

@ -0,0 +1,79 @@
package com.bytezone.diskbrowser.visicalc;
public class Condition
{
private static String[] comparators = { "<>", "<=", ">=", "=", "<", ">" };
private final Sheet parent;
private String comparator;
private String cond;
private String value;
private Expression expCond;
private Expression expValue;
public Condition (Sheet parent, String text)
{
this.parent = parent;
for (String comp : comparators)
{
int pos = text.indexOf (comp);
if (pos > 0)
{
cond = text.substring (0, pos);
value = text.substring (pos + comp.length ());
comparator = comp;
break;
}
}
if (comparator == null)
{
if (text.startsWith ("@"))
{
cond = text;
value = "1";
comparator = "=";
}
else
System.out.println ("No comparator and not a function");
}
}
public boolean getResult ()
{
if (expCond == null)
{
expCond = new Expression (parent, cond);
expValue = new Expression (parent, value);
}
double condValue = expCond.getValue ();
double valueValue = expValue.getValue ();
if (comparator.equals ("="))
return condValue == valueValue;
else if (comparator.equals ("<>"))
return condValue != valueValue;
else if (comparator.equals ("<"))
return condValue < valueValue;
else if (comparator.equals (">"))
return condValue > valueValue;
else if (comparator.equals ("<="))
return condValue <= valueValue;
else if (comparator.equals (">="))
return condValue >= valueValue;
else
System.out.printf ("Unexpected comparator result [%s]%n", comparator);
return false; // flag error?
}
@Override
public String toString ()
{
return String.format ("[cond=%s, op=%s, value=%s]", cond, comparator, value);
}
}

View File

@ -2,94 +2,28 @@ package com.bytezone.diskbrowser.visicalc;
public class If extends Function
{
private static String[] comparators = { "<>", "<=", ">=", "=", "<", ">" };
private final String condition;
private final Condition condition;
private final String textTrue;
private final String textFalse;
private String comparator;
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);
text = text.substring (4, text.length () - 1);
System.out.println (text);
int pos1 = text.indexOf (',');
int pos2 = text.indexOf (',', pos1 + 1);
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)
{
int pos = condition.indexOf (comp);
if (pos > 0)
{
cond = condition.substring (0, pos);
value = condition.substring (pos + comp.length ());
comparator = comp;
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);
int pos1 = functionText.indexOf (',');
int pos2 = functionText.indexOf (',', pos1 + 1);
condition = new Condition (parent, functionText.substring (0, pos1));
textTrue = functionText.substring (pos1 + 1, pos2);
textFalse = functionText.substring (pos2 + 1);
}
@Override
public double getValue ()
{
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 (condition.getResult ())
{
if (expTrue == null)
expTrue = new Expression (parent, textTrue);