diff --git a/src/com/bytezone/diskbrowser/visicalc/Cell.java b/src/com/bytezone/diskbrowser/visicalc/Cell.java index 362de55..1d5f2af 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Cell.java +++ b/src/com/bytezone/diskbrowser/visicalc/Cell.java @@ -83,7 +83,7 @@ class Cell implements Comparable, Value expressionText = "15"; // CARLOAN.VC - if (false) + if (true) if (address.sortValue == 67) expressionText = "9375"; else if (address.sortValue == 131) diff --git a/src/com/bytezone/diskbrowser/visicalc/Condition.java b/src/com/bytezone/diskbrowser/visicalc/Condition.java new file mode 100644 index 0000000..7bb2697 --- /dev/null +++ b/src/com/bytezone/diskbrowser/visicalc/Condition.java @@ -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); + } +} \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/visicalc/If.java b/src/com/bytezone/diskbrowser/visicalc/If.java index a729ab1..145d6df 100644 --- a/src/com/bytezone/diskbrowser/visicalc/If.java +++ b/src/com/bytezone/diskbrowser/visicalc/If.java @@ -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);