public Leaf(eOperatorType eOperator, eLeafType eLeaf, string rawPart) { this.eOperator = eOperator; this.eLeaf = eLeaf; this.rawPart = rawPart; Parse(); }
private void UpdateValue() { eLeaf = eLeafType.VALUE; leafResult.number *= children[0].leafResult.number; leafResult.prefix *= children[0].leafResult.prefix; leafResult.power *= children[1].leafResult.power; if (null != children[2].derivedUnit) { leafResult.add = children[2].derivedUnit.add; } value = Math.Pow(leafResult.number * leafResult.prefix * children[2].value, leafResult.power); //value = leafResult.number * leafResult.prefix * Math.Pow(children[2].value, leafResult.power); foreach (KeyValuePair <string, double> kvp in children[2].leafResult.dimensions) { leafResult.dimensions[kvp.Key] = kvp.Value * leafResult.power; } }
private void ParseUnit <T>(List <T> units, eLeafType eChildLeaf) { string name = ""; foreach (T unit in units) { if (eChildLeaf == eLeafType.UNIT) { BaseUnit baseUnit = (BaseUnit)Convert.ChangeType(unit, typeof(BaseUnit)); name = baseUnit.name; } else { DerivedUnit derivedUnit = (DerivedUnit)Convert.ChangeType(unit, typeof(DerivedUnit)); name = derivedUnit.name; } int pos = part.LastIndexOf(name); if (pos < 0) { continue; } children.Add(new Leaf(eOperatorType.NONE, eLeafType.PREFIX, part.Substring(0, pos))); if (ErrorCheck(true, 1)) { continue; } children.Add(new Leaf(eOperatorType.NONE, eLeafType.POWER, part.Substring(pos + name.Length))); if (ErrorCheck(true, 2)) { continue; } children.Add(new Leaf(eOperatorType.MULTIPLY, eChildLeaf, name)); if (ErrorCheck(true, 3)) { continue; } if (children.Count == 6) { if (pos == 0) { for (int i = 0; i < 3; i++) { children.RemoveAt(0); } break; } else { for (int i = 0; i < 3; i++) { children.RemoveAt(children.Count - 1); } } } else if (pos == 0) { break; } } }