Permalink
Browse files

Initial implementation of '!=' operator

  • Loading branch information...
1 parent 9858bf1 commit 379dcbbe30d8dfa717347b5e0559640d4d7d73a3 @dharmatech committed Jan 20, 2013
Showing with 51 additions and 14 deletions.
  1. +10 −10 Physics/Program.cs
  2. +37 −4 Symbolism/Program.cs
  3. +4 −0 Tests/Program.cs
View
@@ -94,18 +94,18 @@ public static class Calc
{
public static MathObject Time(Obj a, Obj b)
{
- if (!(a.velocity.x == null) &&
- !(b.velocity.x == null) &&
- !(a.acceleration.x == null) &&
- !(a.acceleration.x == new DoubleFloat(0.0)) &&
- !(a.acceleration.x == new Integer(0)))
+ if (a.velocity.x != null &&
+ b.velocity.x != null &&
+ a.acceleration.x != null &&
+ a.acceleration.x != new DoubleFloat(0.0) &&
+ a.acceleration.x != new Integer(0))
return (b.velocity.x - a.velocity.x) / a.acceleration.x;
- if (!(a.velocity.y == null) &&
- !(b.velocity.y == null) &&
- !(a.acceleration.y == null) &&
- !(a.acceleration.y == new DoubleFloat(0.0)) &&
- !(a.acceleration.y == new Integer(0)))
+ if (a.velocity.y != null &&
+ b.velocity.y != null &&
+ a.acceleration.y != null &&
+ a.acceleration.y != new DoubleFloat(0.0) &&
+ a.acceleration.y != new Integer(0))
return (b.velocity.y - a.velocity.y) / a.acceleration.y;
throw new Exception();
View
@@ -86,8 +86,14 @@ public abstract class MathObject
public static Equation operator ==(MathObject a, MathObject b)
{ return new Equation(a, b); }
+ //public static Equation operator !=(MathObject a, MathObject b)
+ //{ return new Equation(a, b); }
+
+ //public static NotEqual operator !=(MathObject a, MathObject b)
+ //{ return new NotEqual(a, b); }
+
public static Equation operator !=(MathObject a, MathObject b)
- { return new Equation(a, b); }
+ { return new Equation(a, b, Equation.Operators.NotEqual); }
//////////////////////////////////////////////////////////////////////
public static MathObject operator +(MathObject a, MathObject b)
@@ -142,19 +148,26 @@ public int Precedence()
public class Equation
{
+ public enum Operators { Equal, NotEqual }
+
public MathObject a;
public MathObject b;
+ public Operators Operator;
+
public Equation(MathObject x, MathObject y)
- { a = x; b = y; }
+ { a = x; b = y; Operator = Operators.Equal; }
+
+ public Equation(MathObject x, MathObject y, Operators op)
+ { a = x; b = y; Operator = op; }
public String ObjectString()
{ return "Equation(" + a + ", " + b + ")"; }
public override string ToString()
{ return a + " == " + b; }
- public Boolean ToBoolean()
+ Boolean ToBoolean()
{
if (a is Integer && b is Integer) return ((Integer)a).Equals(b);
if (a is DoubleFloat && b is DoubleFloat) return ((DoubleFloat)a).Equals(b);
@@ -179,9 +192,29 @@ public Boolean ToBoolean()
}
public static implicit operator Boolean(Equation eq)
- { return (eq.a == eq.b).ToBoolean(); }
+ {
+ if (eq.Operator == Operators.Equal)
+ return (eq.a == eq.b).ToBoolean();
+
+ if (eq.Operator == Operators.NotEqual)
+ return !((eq.a == eq.b).ToBoolean());
+
+ throw new Exception();
+ }
}
+ //public class NotEqual
+ //{
+ // public MathObject a;
+ // public MathObject b;
+
+ // public NotEqual(MathObject x, MathObject y)
+ // { a = x; b = y; }
+
+ // public static implicit operator Boolean(NotEqual eq)
+ // { return !((eq.a == eq.b).ToBoolean()); }
+ //}
+
public abstract class Number : MathObject { }
public class Integer : Number
View
@@ -140,6 +140,10 @@ static void Main(string[] args)
AssertIsTrue(1 * x == x);
AssertIsTrue(x * 1 == x);
+
+ AssertIsTrue(x != y);
+
+ AssertIsTrue(x != new Integer(10));
}
{

0 comments on commit 379dcbb

Please sign in to comment.