private Bracket getLastOpenBracket() { return(input.FindLast(e => { Bracket b = e as Bracket; return (b is null) ? false : b.bracketType == BracketType.Opening; }) as Bracket); }
private void HandleFunctionConectedtobrackets(Bracket bracket, int indexOfBracket) { if (input[indexOfBracket - 1] is Function) { var list = input.GetRange(indexOfBracket, input.IndexOf(bracket.enclosureReference) - indexOfBracket + 1); var arguments = (from op in list where op is Number select(op as Number)).ToArray(); var result = (input[indexOfBracket - 1] as Function).Execute(arguments); RemoveArguments(list); input.Insert(indexOfBracket - 1, result); OutputChange(); } }
private void InsertMissingBracket(int insertionIndex) { var bracket = new Bracket(BracketType.Opening); foreach (var equationElement in decodedEquation) { if (equationElement is Prioritabel) { (equationElement as Prioritabel).priority = (int)Math.Log((equationElement as Prioritabel).priority, Operation.MAX_PRIORITY) + 1; } } decodedEquation.Insert(insertionIndex, bracket); decodedEquation.Add(new Bracket(BracketType.Closing, bracket)); }
private void OpenBracketCase() { if (previousElement is Constant || previousElement is Variable) { decodedEquation.Add(new Operation(OperationType.Multiplication, additionalPriority)); } var bracket = new Bracket(BracketType.Opening); commas.Add(0); openBrackets.Add(bracket); decodedEquation.Add(bracket); additionalPriority++; previousElement = decodedEquation.Last(); }
public Number Simplify() { Bracket bracket = getLastOpenBracket(); while (bracket != null) { int indexOfBracket = input.IndexOf(bracket); SimplifyPart(indexOfBracket, input.IndexOf(bracket.enclosureReference)); if (indexOfBracket > 0) { HandleFunctionConectedtobrackets(bracket, indexOfBracket); } input.Remove(bracket.enclosureReference); input.Remove(bracket); bracket = getLastOpenBracket(); } SimplifyPart(0, input.Count); return(input[0] as Number); }
public static List <Element> DecodeEquationLegacyCode(string input) { List <Element> result = new List <Element>(); if (input.Length < 1) { throw new InvalidEquationException(); } List <Bracket> openBrackets = new List <Bracket>(); List <Function> twoArgumentFunctions = new List <Function>(); int additionalPriority = 0; bool isEqualitySign = false; List <int> commas = new List <int>(); commas.Add(0); Element previousElement = null; string[] array = SplitLegacyCode(input); foreach (var item in array) { if (item[0] == '(') { if (previousElement != null) { if (previousElement.elementType == ElementType.Constant || previousElement.elementType == ElementType.Parameter || previousElement.elementType == ElementType.Unknown) { result.Add(new Operation(OperationType.Multiplication, additionalPriority)); } } var bracket = new Bracket(BracketType.Opening); commas.Add(0); openBrackets.Add(bracket); result.Add(bracket); additionalPriority++; previousElement = result.Last(); } else if (item[0] >= '0' && item[0] <= '9' || item[0] == '-') // SM - B.Close, Fact, Number, Const/P { if (previousElement != null) { if (previousElement.elementType == ElementType.Constant || previousElement.elementType == ElementType.Parameter || previousElement.elementType == ElementType.Unknown) { result.Add(new Operation(OperationType.Multiplication, additionalPriority)); } } result.Add(new Number(double.Parse(item, CultureInfo.InvariantCulture))); previousElement = result.Last(); } else if (item.ToLower()[0] >= 'a' && item.ToLower()[0] <= 'z') { var temp = new WordInterpreter(item, additionalPriority).InterpretWord(); if (previousElement != null) { if ((previousElement.elementType == ElementType.Number ? (previousElement as Number).value < 0 : false) && (temp.Last().elementType == ElementType.Constant || temp.Last().elementType == ElementType.Parameter || temp.Last().elementType == ElementType.Unknown)) { result.Add(new Operation(OperationType.Addition, additionalPriority)); } } foreach (var subitem in temp) { result.Add(subitem); } previousElement = result.Last(); } else if (operators.Contains(item[0])) { if (previousElement != null) { if (previousElement.elementType == ElementType.Function) { throw new InvalidEquationException(); } } result.Add(new Operation((OperationType)Operation.CharToOperationType(item[0]), additionalPriority)); previousElement = result.Last(); } else if (item[0] == ')') { if (previousElement != null) { if (previousElement.elementType == ElementType.Function) { throw new InvalidEquationException(); } } if (openBrackets.Count == 0) { if (!isEqualitySign) { var bracket = new Bracket(BracketType.Opening); foreach (var subitem in result) { if (subitem.elementType == ElementType.Operation || subitem.elementType == ElementType.Function) { (subitem as Prioritabel).priority = (int)Math.Log((subitem as Prioritabel).priority, Operation.MAX_PRIORITY) + 1; } } result.Insert(0, bracket); result.Add(new Bracket(BracketType.Closing, bracket)); } else { int equalityIndex = result.FindLastIndex(c => { return(c.elementType == ElementType.Equality); }); var bracket = new Bracket(BracketType.Opening); foreach (var subitem in result) { if ((subitem.elementType == ElementType.Operation || subitem.elementType == ElementType.Function) && result.IndexOf(subitem) > equalityIndex) { (subitem as Prioritabel).priority = (int)Math.Log((subitem as Prioritabel).priority, Operation.MAX_PRIORITY) + 1; } } result.Insert(equalityIndex + 1, bracket); result.Add(new Bracket(BracketType.Closing, bracket)); } } else { var bracket = openBrackets.Last(); openBrackets.Remove(bracket); commas.Remove(commas.Last()); result.Add(new Bracket(BracketType.Closing, bracket)); additionalPriority--; } previousElement = result.Last(); } else if (item[0] == '=' || item[0] == '<' || item[0] == '>') { if (previousElement != null) { if (previousElement.elementType == ElementType.Function) { throw new InvalidEquationException(); } } if ((result.Last().elementType == ElementType.Operation) ? (result.Last() as Operation).operationType == OperationType.Multiplication : false) { result.RemoveAt(result.Count - 1); } if (openBrackets.Count > 0) { foreach (var subitem in openBrackets) { result.Remove(subitem); } openBrackets.Clear(); additionalPriority = 0; } result.Add(new Equality((EqualityType)Equality.StringToEqualityType(item))); isEqualitySign = true; previousElement = result.Last(); } else if (item[0] == ',') { if (previousElement != null) { if (previousElement.elementType == ElementType.Function) { throw new InvalidEquationException(); } } if (commas.Last() == Comma.MAX_COMMAS) { throw new InvalidEquationException(); } result.Add(new Comma()); commas[commas.Count - 1]++; previousElement = result.Last(); } else if (item[0] == '!') { if (previousElement != null) { if (previousElement.elementType == ElementType.Function) { throw new InvalidEquationException(); } } result.Add(new Operation(OperationType.Factorial, additionalPriority)); previousElement = result.Last(); } } if (openBrackets.Count > 0) { int count = openBrackets.Count; foreach (var subitem in openBrackets) { result.Add(new Bracket(BracketType.Closing, subitem)); } openBrackets.Clear(); additionalPriority = 0; } if (isEqualitySign) // removes unnessesery brackets to avoid sth like this: (((X))) { int equalityCount = result.Count(c => { return(c.elementType == ElementType.Equality); }); int[] equalitySignIndex = new int[equalityCount]; equalitySignIndex[0] = result.FindIndex(c => { return(c.elementType == ElementType.Equality); }); for (int i = 1; i < equalitySignIndex.Length; i++) { equalitySignIndex[i] = result.FindIndex(equalitySignIndex[i - 1] + 1, c => { return(c.elementType == ElementType.Equality); }); } for (int i = 0; i < equalityCount + 1; i++) { while (result[i == 0 ? 0 : equalitySignIndex[i - 1]].elementType == ElementType.Brackets && result[i == equalityCount + 1 ? result.Count - 1 : equalitySignIndex[i]].elementType == ElementType.Brackets) { result.RemoveAt(i == 0 ? 0 : equalitySignIndex[i - 1]); result.RemoveAt(i == equalityCount + 1 ? result.Count - 1 : equalitySignIndex[i]); } } } else { while (result[0].elementType == ElementType.Brackets && result[result.Count - 1].elementType == ElementType.Brackets) { result.RemoveAt(0); result.RemoveAt(result.Count - 1); } } if (result.Last().elementType == ElementType.Function) { throw new InvalidEquationException(); } return(result); }