public void Divide(Term divisor) { if (topLayer is MultiplyDivideLayer) ((MultiplyDivideLayer)topLayer).Divisors.Add(divisor.TopLayer); else { MultiplyDivideLayer newTopLayer = new MultiplyDivideLayer(); newTopLayer.Factors.Add(topLayer); newTopLayer.Divisors.Add(divisor.TopLayer); topLayer = Layer.GetBetterChild(newTopLayer); } }
private void AddFirstToLayerAndDeleteFromList(MultiplyDivideLayer layer, List<IElement> els) { bool isFactor = true; if (els[0] is DivideElement) isFactor = false; if (els[0] is MultiplyElement || els[0] is DivideElement) els.RemoveAt(0); int length = GetFirstIndexOrCount(els, typeof(MultiplyElement), typeof(DivideElement)); ElementsToLayersParser parser = new ElementsToLayersParser(els.GetRange(0, length)); parser.Parse(); if (isFactor) layer.Factors.Add(parser.TopLayer); if (!isFactor) layer.Divisors.Add(parser.TopLayer); els.RemoveRange(0, length); }
private MultiplyDivideLayer ParseMultiplyDivideLayerFromElements() { MultiplyDivideLayer layer = new MultiplyDivideLayer(); if (ContainsType(elements, typeof(MultiplyElement), typeof(DivideElement))) { List<IElement> els = new List<IElement>(elements); while (els.Count > 0) AddFirstToLayerAndDeleteFromList(layer, els); return layer; } if(ContainsType(elements, typeof(ExpElement))) { ILayer factor, exponent; ParseMiddleOperatorType(new List<IElement>(elements), typeof(ExpElement), out factor, out exponent); PowerLayer powerLayer = new PowerLayer(); powerLayer.BaseOfPower = new NumberLayer(10); powerLayer.Exponent = exponent; layer.Factors.Add(factor); layer.Factors.Add(powerLayer); return layer; } return layer; }
private AddSubtractLayer CreateNonCombinedLayerPart(ILayer[] pair, MultiplyDivideLayer[] parents) { AddSubtractLayer layerPart = new AddSubtractLayer(); MultiplyDivideLayer layer0 = Layer.GetMultiplyDivideLayerWithoutOne(parents[0], pair[0]); if (additions.Contains(parents[0])) layerPart.Additions.Add(layer0); else layerPart.Subtractions.Add(layer0); MultiplyDivideLayer layer1 = Layer.GetMultiplyDivideLayerWithoutOne(parents[1], pair[1]); if (additions.Contains(parents[1])) layerPart.Additions.Add(layer1); else layerPart.Subtractions.Add(layer1); return layerPart; }
private void CombinePair(ILayer[] pair, List<MultiplyDivideLayer> all) { MultiplyDivideLayer[] parents = new MultiplyDivideLayer[2]; List<ILayer> tmp = new List<ILayer>(all); parents[0] = (MultiplyDivideLayer)Layer.FindParentLayer(pair[0], tmp); parents[1] = (MultiplyDivideLayer)Layer.FindParentLayer(pair[1], tmp); MultiplyDivideLayer newLayer = new MultiplyDivideLayer(); newLayer.Factors.Add(CreateNonCombinedLayerPart(pair, parents)); newLayer.Factors.Add(pair[0]); additions.Add(newLayer); RemoveFromLayers(parents[0]); RemoveFromLayers(parents[1]); }
public void Invert() { if(topLayer is AddSubtractLayer) { AddSubtractLayer layer = (AddSubtractLayer)topLayer; List<ILayer> additions = new List<ILayer>(layer.Additions); List<ILayer> subtractions = new List<ILayer>(layer.Subtractions); layer.Additions.Clear(); layer.Additions.AddRange(subtractions); layer.Subtractions.Clear(); layer.Subtractions.AddRange(additions); } else if(topLayer is MultiplyDivideLayer) { ((MultiplyDivideLayer)topLayer).Factors.Add(new NumberLayer(-1)); } else { MultiplyDivideLayer newTopLayer = new MultiplyDivideLayer(); newTopLayer.Factors.Add(topLayer); newTopLayer.Factors.Add(new NumberLayer(-1)); topLayer = newTopLayer; } }
public void Reciproke() { if (topLayer is MultiplyDivideLayer) { MultiplyDivideLayer layer = (MultiplyDivideLayer)topLayer; List<ILayer> factors = new List<ILayer>(layer.Factors); List<ILayer> divisors = new List<ILayer>(layer.Divisors); layer.Factors.Clear(); layer.Factors.AddRange(divisors); layer.Divisors.Clear(); layer.Divisors.AddRange(factors); } else { MultiplyDivideLayer newTopLayer = new MultiplyDivideLayer(); newTopLayer.Factors.Add(new NumberLayer(1)); newTopLayer.Divisors.Add(topLayer); topLayer = newTopLayer; } }