private SplitElementType currentTypeDetection(char character) { SplitElementType result = SplitElementType.Null; if (operators.Contains(character)) { result = SplitElementType.Operator; } else if (character == '(') { result = SplitElementType.BracketsOpen; beforePreviousType = previousType; } else if (character == ')') { result = SplitElementType.BracketsClose; } else if (character == '-') { result = SplitElementType.Negative; } else if (character >= '0' && character <= '9' || character == '.') { result = SplitElementType.Number; if (character == '.' && (isDot || currentString.Length == 0)) { throw new InvalidEquationException(); } } else if (character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z') { result = SplitElementType.Word; } else if (character == '=' || character == '<' || character == '>') { result = SplitElementType.Equality; if (previousType == SplitElementType.Factorial) { beforePreviousType = SplitElementType.Factorial; } } else if (character == '!') { result = SplitElementType.Factorial; } else if (character == ',') { result = SplitElementType.Comma; } else { throw new InvalidEquationException(); } return(result); }
private static string[] SplitLegacyCode(string input) { List <string> result = new List <string>(); string currentString = string.Empty; bool isDot = false; SplitElementType previousType = SplitElementType.Null; SplitElementType beforePreviousType = SplitElementType.Null; foreach (var item in input) { SplitElementType currentType = SplitElementType.Null; if (operators.Contains(item)) { currentType = SplitElementType.Operator; } else if (item == '(') { currentType = SplitElementType.BracketsOpen; beforePreviousType = previousType; } else if (item == ')') { currentType = SplitElementType.BracketsClose; } else if (item == '-') { currentType = SplitElementType.Negative; } else if (item >= '0' && item <= '9' || item == '.') { currentType = SplitElementType.Number; if (item == '.' && (isDot || currentString.Length == 0)) { throw new InvalidEquationException(); } } else if (item >= 'a' && item <= 'z' || item >= 'A' && item <= 'Z') { currentType = SplitElementType.Word; } else if (item == '=' || item == '<' || item == '>') { currentType = SplitElementType.Equality; if (previousType == SplitElementType.Factorial) { beforePreviousType = SplitElementType.Factorial; } } else if (item == '!') { currentType = SplitElementType.Factorial; } else if (item == ',') { currentType = SplitElementType.Comma; } else if (item == ' ') { continue; } else { throw new InvalidEquationException(); } if (previousType == SplitElementType.Null) // Null { currentString = string.Empty + item; if (currentType == SplitElementType.BracketsClose) { currentString = string.Empty; } else if (new SplitElementType[] { SplitElementType.Operator, SplitElementType.Comma, SplitElementType.Equality, SplitElementType.Factorial }.Contains(currentType)) { throw new InvalidEquationException(); } } else if (previousType == SplitElementType.BracketsOpen) // Brackets Open { if (currentType == SplitElementType.Operator || currentType == SplitElementType.Comma || currentType == SplitElementType.Factorial) { throw new InvalidEquationException(); } if (currentType == SplitElementType.BracketsClose) { currentString = string.Empty; previousType = beforePreviousType; continue; } else if (currentType == SplitElementType.Equality) { currentString = string.Empty + item; } else { result.Add(currentString); currentString = string.Empty + item; } } else if (previousType == SplitElementType.Number) // Number { if (currentType == previousType) { currentString += item; } else { result.Add(currentString); if (currentType == SplitElementType.BracketsOpen || currentType == SplitElementType.Word) { result.Add("*"); } if (currentType == SplitElementType.Negative) { result.Add("+"); currentType = SplitElementType.Number; } currentString = string.Empty + item; isDot = false; } } else if (previousType == SplitElementType.Word) // Word { if (currentType == previousType) { currentString += item; } else { result.Add(currentString); currentString = string.Empty + item; } } else if (previousType == SplitElementType.Operator) // Operator { if (new SplitElementType[] { SplitElementType.Operator, SplitElementType.BracketsClose, SplitElementType.Comma, SplitElementType.Equality, SplitElementType.Factorial }.Contains(currentType)) { throw new InvalidEquationException(); } else { result.Add(currentString); currentString = string.Empty + item; if (currentType == SplitElementType.Negative) { currentType = SplitElementType.Number; } } } else if (previousType == SplitElementType.BracketsClose) // Brackets Close { result.Add(currentString); if (new SplitElementType[] { SplitElementType.Number, SplitElementType.BracketsOpen, SplitElementType.Word }.Contains(currentType)) { result.Add("*"); } if (currentType == SplitElementType.Negative) { result.Add("+"); } currentString = string.Empty + item; } else if (previousType == SplitElementType.Factorial) // Factorial { if (currentType == SplitElementType.Equality) { currentString += item; } else { result.Add(currentString); if (currentType == SplitElementType.Negative) { result.Add("+"); } else if (new SplitElementType[] { SplitElementType.BracketsOpen, SplitElementType.Number, SplitElementType.Word }.Contains(currentType)) { } currentString = string.Empty + item; } } else if (previousType == SplitElementType.Equality) // Equality { if (new SplitElementType[] { SplitElementType.Operator, SplitElementType.Comma, SplitElementType.Factorial }.Contains(currentType)) { throw new InvalidEquationException(); } else if (currentType == SplitElementType.BracketsClose) { currentType = previousType; beforePreviousType = SplitElementType.Equality; // stupid way to indicate that this happened } else if (currentType == SplitElementType.Equality) { if (beforePreviousType == SplitElementType.Factorial) { result.Add("!"); currentString = "=" + item; } else if (beforePreviousType == SplitElementType.Equality) // catches if Equality is > 2 chars long { throw new InvalidEquationException(); } else { beforePreviousType = SplitElementType.Equality; currentString += item; } } else { result.Add(currentString); if (currentType == SplitElementType.BracketsOpen) { beforePreviousType = SplitElementType.Equality; } if (currentType == SplitElementType.Negative) { currentType = SplitElementType.Number; } currentString = string.Empty + item; } } else if (previousType == SplitElementType.Comma) // Comma { if (new SplitElementType[] { SplitElementType.Operator, SplitElementType.BracketsClose, SplitElementType.Equality, SplitElementType.Factorial, SplitElementType.Comma }.Contains(currentType)) { throw new InvalidEquationException(); } result.Add(currentString); if (currentType == SplitElementType.Negative) { currentType = SplitElementType.Number; } currentString = string.Empty + item; } else if (previousType == SplitElementType.Negative) { if (currentType == SplitElementType.Number) { currentString += item; } else if (currentType == SplitElementType.BracketsOpen || currentType == SplitElementType.Word) { result.Add(currentString + "1"); result.Add("*"); currentString = string.Empty + item; } else { throw new InvalidEquationException(); } } previousType = currentType; } if (new SplitElementType[] { SplitElementType.Operator, SplitElementType.Equality, SplitElementType.Comma, SplitElementType.Null }.Contains(previousType)) { throw new InvalidEquationException(); } else if (previousType != SplitElementType.BracketsOpen) { result.Add(currentString); } return(result.ToArray()); }
public string[] Split() { foreach (var character in input) { SplitElementType currentType = currentTypeDetection(character); if (previousType == SplitElementType.Null) // Null { currentString = string.Empty + character; if (currentType == SplitElementType.BracketsClose) { currentString = string.Empty; } else if (new SplitElementType[] { SplitElementType.Operator, SplitElementType.Comma, SplitElementType.Equality, SplitElementType.Factorial }.Contains(currentType)) { throw new InvalidEquationException(); } } else if (previousType == SplitElementType.BracketsOpen) // Brackets Open { if (currentType == SplitElementType.Operator || currentType == SplitElementType.Comma || currentType == SplitElementType.Factorial) { throw new InvalidEquationException(); } if (currentType == SplitElementType.BracketsClose) { currentString = string.Empty; previousType = beforePreviousType; continue; } else if (currentType == SplitElementType.Equality) { currentString = string.Empty + character; } else { splitedString.Add(currentString); currentString = string.Empty + character; } } else if (previousType == SplitElementType.Number) // Number { if (currentType == previousType) { currentString += character; } else { splitedString.Add(currentString); if (currentType == SplitElementType.BracketsOpen || currentType == SplitElementType.Word) { splitedString.Add("*"); } if (currentType == SplitElementType.Negative) { splitedString.Add("+"); currentType = SplitElementType.Number; } currentString = string.Empty + character; isDot = false; } } else if (previousType == SplitElementType.Word) // Word { if (currentType == previousType) { currentString += character; } else { splitedString.Add(currentString); currentString = string.Empty + character; } } else if (previousType == SplitElementType.Operator) // Operator { if (new SplitElementType[] { SplitElementType.Operator, SplitElementType.BracketsClose, SplitElementType.Comma, SplitElementType.Equality, SplitElementType.Factorial }.Contains(currentType)) { throw new InvalidEquationException(); } else { splitedString.Add(currentString); currentString = string.Empty + character; if (currentType == SplitElementType.Negative) { currentType = SplitElementType.Number; } } } else if (previousType == SplitElementType.BracketsClose) // Brackets Close { splitedString.Add(currentString); if (new SplitElementType[] { SplitElementType.Number, SplitElementType.BracketsOpen, SplitElementType.Word }.Contains(currentType)) { splitedString.Add("*"); } if (currentType == SplitElementType.Negative) { splitedString.Add("+"); } currentString = string.Empty + character; } else if (previousType == SplitElementType.Factorial) // Factorial { if (currentType == SplitElementType.Equality) { currentString += character; } else { splitedString.Add(currentString); if (currentType == SplitElementType.Negative) { splitedString.Add("+"); } else if (new SplitElementType[] { SplitElementType.BracketsOpen, SplitElementType.Number, SplitElementType.Word }.Contains(currentType)) { } currentString = string.Empty + character; } } else if (previousType == SplitElementType.Equality) // Equality { if (new SplitElementType[] { SplitElementType.Operator, SplitElementType.Comma, SplitElementType.Factorial }.Contains(currentType)) { throw new InvalidEquationException(); } else if (currentType == SplitElementType.BracketsClose) { currentType = previousType; beforePreviousType = SplitElementType.Equality; // stupid way to indicate that this happened } else if (currentType == SplitElementType.Equality) { if (beforePreviousType == SplitElementType.Factorial) { splitedString.Add("!"); currentString = "=" + character; } else if (beforePreviousType == SplitElementType.Equality) // catches if Equality is >2 chars long { throw new InvalidEquationException(); } else { beforePreviousType = SplitElementType.Equality; currentString += character; } } else { splitedString.Add(currentString); if (currentType == SplitElementType.BracketsOpen) { beforePreviousType = SplitElementType.Equality; } if (currentType == SplitElementType.Negative) { currentType = SplitElementType.Number; } currentString = string.Empty + character; } } else if (previousType == SplitElementType.Comma) // Comma { if (new SplitElementType[] { SplitElementType.Operator, SplitElementType.BracketsClose, SplitElementType.Equality, SplitElementType.Factorial, SplitElementType.Comma }.Contains(currentType)) { throw new InvalidEquationException(); } splitedString.Add(currentString); if (currentType == SplitElementType.Negative) { currentType = SplitElementType.Number; } currentString = string.Empty + character; } else if (previousType == SplitElementType.Negative) { if (currentType == SplitElementType.Number) { currentString += character; } else if (currentType == SplitElementType.BracketsOpen || currentType == SplitElementType.Word) { splitedString.Add(currentString + "1"); splitedString.Add("*"); currentString = string.Empty + character; } else { throw new InvalidEquationException(); } } previousType = currentType; } if (new SplitElementType[] { SplitElementType.Operator, SplitElementType.Equality, SplitElementType.Comma, SplitElementType.Null }.Contains(previousType)) { throw new InvalidEquationException(); } else if (previousType != SplitElementType.BracketsOpen) { splitedString.Add(currentString); } return(splitedString.ToArray()); }