public static Equation Build(string e) { e = e.Trim(); Equation n = new Equation(); Equation p = new Equation(); int a = 0; int z = 0; string s = ""; foreach (char c in e) { if (c.EqualsAny('(', '[')) { a += 1; } else if (c.EqualsAny(')', ']')) { z += 1; } } if (a.Equals(z)) { foreach (char c in e) { if (c.IsSolvable()) { if (s != "") { if (s.EndsWith("0")) { if (s.Length == 1) { if (c.IsDigit()) { s = "" + c; continue; } } else { if (s[s.Length - 2].IsOperator()) { if (c.IsDigit()) { s = s.ReplaceEnd(c); continue; } } } } if (s.EndsWith("/0")) { if (c.IsOperator()) { s = ""; n = n.Throw("divide_by_zero"); break; } } if (new char[] { c, s[s.Length - 1] }.AreOperators()) { s = ""; n = n.Throw("invalid_operator_usage"); break; } } s += c; continue; } else if (c.EqualsAny('(', '[')) { if (s != "") { if (s[s.LengthAsIndex()].IsAlphanumeric()) { s += "*"; } if (s.EndsWith("/0")) { s = ""; n = n.Throw("divide_by_zero"); break; } } s += $"[{n.Layer + 1}, {n.Children.Count}]"; s.Debug($"[Opening a new equation in {n.Index()}...]"); n.Conjoin(s); p = n; n = new Equation(p); p.Adopt(n); s.Debug($"[Set {n.Index()} in layer {p.Layer}.]"); s = ""; continue; } else if (c.EqualsAny(')', ']')) { if (s.EndsWith("/0")) { s = ""; n = n.Throw("divide_by_zero"); break; } n.Value += s; if (n.HasValue()) { Debugger.Write("[No property was written. Setting to null...]"); n.Assign("null"); } n.Value.Debug($"[Closing {n.Index()}...]"); n = n.Parent; s = ""; continue; } } if (s != "") { if (s.EndsWith("/0")) { s = ""; n = n.Throw("divide_by_zero"); } else { n.Conjoin(s); n.Value.Debug($"[Extending {n.Index()}...]"); } } } else { Debugger.Write("[Incorrect equation structure. Now cancelling...]"); n.Throw("invalid_equation_structure"); } n = n.GetRootParent(); return(n); }
public void Abandon(Equation child) => Children.Remove(child);
public Equation(Equation parent) { Parent = parent; Layer = Parent.Layer + 1; Position = Parent.Children.Count; }
public void Adopt(Equation child) => Children.Add(child);