public static EquationSegment SplitEquation(string equation, int splitIndex, SplitType sideToGet) { var retStruct = new EquationSegment(); char beginningBracket; char endingBracket; int increment; int limitValue; if (splitIndex == 0 && (sideToGet == SplitType.LeftSide || sideToGet == SplitType.ExponentLeft)) { retStruct.EndIndex = 0; retStruct.StartIndex = 0; retStruct.Segment = ""; return(retStruct); } if (sideToGet == SplitType.LeftSide || sideToGet == SplitType.ExponentLeft) { beginningBracket = ')'; endingBracket = '('; increment = -1; limitValue = -1; } else { beginningBracket = '('; endingBracket = ')'; increment = 1; limitValue = equation.Count(); } //the left side is enclosed with brackets, so the only way to terminate the left side is with an opening bracket if (equation[splitIndex + increment] == beginningBracket) { int totalBrackets = 1; int index = splitIndex + increment; while (totalBrackets != 0) { index += increment; if (index == limitValue) { throw new Exception("formatting error, brackets not set up properly"); } if (equation[index] == beginningBracket) { totalBrackets++; } if (equation[index] == endingBracket) { totalBrackets--; } } if (sideToGet == SplitType.LeftSide || sideToGet == SplitType.ExponentLeft) { retStruct.EndIndex = splitIndex; retStruct.StartIndex = index; retStruct.Segment = equation.Substring(retStruct.StartIndex, retStruct.EndIndex - retStruct.StartIndex); } else { retStruct.EndIndex = index + 1; retStruct.StartIndex = splitIndex + 1; retStruct.Segment = equation.Substring(retStruct.StartIndex, retStruct.EndIndex - retStruct.StartIndex); } return(retStruct); } else { //for this case, order of operations must be respected because of lack of brackets //there are two ways for a termination to occur, either one of the terminating characters is reached //or an unexpected endingbracket is reached, such as in the case (x/y) int index = splitIndex + increment; char[] terminatingChars = new char[0]; bool ignoreCharacterTermination = false; bool ignoreBracketTermination = false; if (sideToGet == SplitType.LeftSide) { terminatingChars = new[] { '=', '+', '-' }; } if (sideToGet == SplitType.RightSide) { terminatingChars = new[] { '=', '+', '-', '*', '/' }; } if (sideToGet == SplitType.ExponentRight) { terminatingChars = new[] { '=', '+', '-', '*', '/' }; if (equation[index] == beginningBracket) //this expression may be always false { ignoreCharacterTermination = true; } else { index++; //we want to completely ignore the first character because of negatives ignoreBracketTermination = true; } } if (sideToGet == SplitType.ExponentLeft) { terminatingChars = new[] { '=', '+', '-', '*', '/' }; } int totalBrackets = 0; if (index < equation.Count()) { while (true) { //first try character based termination if (terminatingChars.Contains(equation[index])) { if (!ignoreCharacterTermination) { break; } } //now try bracket based termination if (equation[index] == beginningBracket) { if (!ignoreBracketTermination) { totalBrackets++; } } if (equation[index] == endingBracket) { if (!ignoreBracketTermination) { totalBrackets--; } } if (totalBrackets < 0) { //index -= increment; break; } //nothing interesting, loop again if (index == limitValue - increment) { index += increment; break; } //firstCharacterException = false; index += increment; } } if (sideToGet == SplitType.LeftSide || sideToGet == SplitType.ExponentLeft) { retStruct.EndIndex = splitIndex; retStruct.StartIndex = index + 1; retStruct.Segment = equation.Substring(retStruct.StartIndex, retStruct.EndIndex - retStruct.StartIndex); } else { retStruct.EndIndex = index; retStruct.StartIndex = splitIndex + 1; retStruct.Segment = equation.Substring(retStruct.StartIndex, retStruct.EndIndex - retStruct.StartIndex); } return(retStruct); } }
public static EquationSegment SplitEquation(string equation, int splitIndex, SplitType sideToGet) { var retStruct = new EquationSegment(); char beginningBracket; char endingBracket; int increment; int limitValue; if (splitIndex == 0 && (sideToGet == SplitType.LeftSide || sideToGet == SplitType.ExponentLeft)){ retStruct.EndIndex = 0; retStruct.StartIndex = 0; retStruct.Segment = ""; return retStruct; } if (sideToGet == SplitType.LeftSide || sideToGet == SplitType.ExponentLeft){ beginningBracket = ')'; endingBracket = '('; increment = -1; limitValue = -1; } else{ beginningBracket = '('; endingBracket = ')'; increment = 1; limitValue = equation.Count(); } //the left side is enclosed with brackets, so the only way to terminate the left side is with an opening bracket if (equation[splitIndex + increment] == beginningBracket){ int totalBrackets = 1; int index = splitIndex + increment; while (totalBrackets != 0){ index += increment; if (index == limitValue) throw new Exception("formatting error, brackets not set up properly"); if (equation[index] == beginningBracket){ totalBrackets++; } if (equation[index] == endingBracket){ totalBrackets--; } } if (sideToGet == SplitType.LeftSide || sideToGet == SplitType.ExponentLeft){ retStruct.EndIndex = splitIndex; retStruct.StartIndex = index; retStruct.Segment = equation.Substring(retStruct.StartIndex, retStruct.EndIndex - retStruct.StartIndex); } else{ retStruct.EndIndex = index + 1; retStruct.StartIndex = splitIndex + 1; retStruct.Segment = equation.Substring(retStruct.StartIndex, retStruct.EndIndex - retStruct.StartIndex); } return retStruct; } else{ //for this case, order of operations must be respected because of lack of brackets //there are two ways for a termination to occur, either one of the terminating characters is reached //or an unexpected endingbracket is reached, such as in the case (x/y) int index = splitIndex + increment; char[] terminatingChars = new char[0]; bool ignoreCharacterTermination = false; bool ignoreBracketTermination = false; if (sideToGet == SplitType.LeftSide){ terminatingChars = new[]{'=', '+', '-'}; } if (sideToGet == SplitType.RightSide){ terminatingChars = new[]{'=', '+', '-', '*', '/'}; } if (sideToGet == SplitType.ExponentRight){ terminatingChars = new[]{'=', '+', '-', '*', '/'}; if (equation[index] == beginningBracket){ //this expression may be always false ignoreCharacterTermination = true; } else{ index++; //we want to completely ignore the first character because of negatives ignoreBracketTermination = true; } } if (sideToGet == SplitType.ExponentLeft){ terminatingChars = new[]{'=', '+', '-', '*', '/'}; } int totalBrackets = 0; if (index < equation.Count()){ while (true){ //first try character based termination if (terminatingChars.Contains(equation[index])){ if (!ignoreCharacterTermination){ break; } } //now try bracket based termination if (equation[index] == beginningBracket){ if (!ignoreBracketTermination){ totalBrackets++; } } if (equation[index] == endingBracket){ if (!ignoreBracketTermination){ totalBrackets--; } } if (totalBrackets < 0){ //index -= increment; break; } //nothing interesting, loop again if (index == limitValue - increment){ index += increment; break; } //firstCharacterException = false; index += increment; } } if (sideToGet == SplitType.LeftSide || sideToGet == SplitType.ExponentLeft){ retStruct.EndIndex = splitIndex; retStruct.StartIndex = index + 1; retStruct.Segment = equation.Substring(retStruct.StartIndex, retStruct.EndIndex - retStruct.StartIndex); } else{ retStruct.EndIndex = index; retStruct.StartIndex = splitIndex + 1; retStruct.Segment = equation.Substring(retStruct.StartIndex, retStruct.EndIndex - retStruct.StartIndex); } return retStruct; } }