// | + - | public static string AddOpenBracket(string expression) { int openBracketLeft = CountOpenBrackets(expression); char lastSymbol = GetLastSymbol(expression); if (lastSymbol == ' ') { return(expression + '('); } else if (lastSymbol == '.' || lastSymbol == ',') { return(expression); } else if (!Contains.ToNumbers(lastSymbol) && !Contains.ToAllOperators(lastSymbol)) { return(expression); } else if (lastSymbol == ')' || Contains.ToNumbers(lastSymbol)) { return(expression + '×' + '('); } else if (Contains.ToAllOperators(lastSymbol)) { return(expression + '('); } return(expression); }
// | + - | public static string InvertNumber(string expression) { string result; int operatorIndex = FindNearestOperator(expression); if (operatorIndex == -1 || expression.Length == 0) { result = '-' + expression; } else if (expression[operatorIndex] == '+') { result = ReplaceOperator(expression, operatorIndex, '-'); } else if (expression[operatorIndex] == '(' && expression.Length - 1 > operatorIndex && Contains.ToNumbers(expression[operatorIndex + 1])) { result = expression.Insert(operatorIndex + 1, "-"); } else if (operatorIndex == 0 && expression[operatorIndex] == '(') { result = expression + '-'; } else if (operatorIndex == 0) { result = ReplaceOperator(expression, operatorIndex, ' '); } else if (expression[operatorIndex - 1] == '(' && expression[operatorIndex] == '-') { result = ReplaceOperator(expression, operatorIndex, ' '); } else if (expression[operatorIndex - 1] == ')' && expression[operatorIndex] == '-') { result = ReplaceOperator(expression, operatorIndex, '+'); } else if (Contains.ToAllOperators(expression[operatorIndex - 1]) && expression[operatorIndex] == '-') { result = ReplaceOperator(expression, operatorIndex, ' '); } else if (Contains.ToNumbers(expression[operatorIndex - 1]) && expression[operatorIndex] == '-') { result = ReplaceOperator(expression, operatorIndex, '+'); } else { result = expression.Insert(operatorIndex + 1, "-"); } result = result.Replace(" ", ""); return(result); }
// | + - | public static string AddSeparator(string expression) { string symbol = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator; char separator = symbol[0]; string result = expression; bool correctLast = false; for (int i = expression.Length - 1; i >= 0; i--) { if (!correctLast) { if (Contains.ToNumbers(expression[expression.Length - 1])) { correctLast = true; } else if (Contains.ToMathOperators( expression[expression.Length - 1]) || expression[expression.Length - 1] == '(') { result += "0" + separator; break; } else { break; } } if (correctLast) { if (Contains.ToSeparators(expression[i])) { break; } if (Contains.ToAllOperators(expression[i])) { result += separator; break; } if (i == 0 && Contains.ToNumbers(expression[i])) { result += separator; } } } if (expression.Length == 0) { result = "0" + separator; } return(result); }
// | - - | public static string ReplaceMinusForPlus(string expression) { string currentExpression = expression; for (int i = 0; i < currentExpression.Length; i++) { if (i > 0 && currentExpression[i] == '-' && currentExpression[i - 1] == '-') { currentExpression = currentExpression.Remove(i - 1, 2); currentExpression = currentExpression.Insert(i - 1, "+"); if (currentExpression[0] == '+') { currentExpression = currentExpression.Remove(0, 1); } if (i > 1 && Contains.ToAllOperators(currentExpression[i - 2])) { currentExpression = currentExpression.Remove(i - 1, 1); } } } return(currentExpression); }
// | + - | public static string Selector(string expression, int index) { expression = Parse.RemoveSpace(expression); if (!Parse.IsCorrect(expression)) { throw new NotCorrectException("Выражение не корректно."); } if (!Contains.ToMathOperators(expression[index])) { throw new Exception("Переданный индекс не является " + "математическим оператором."); } if (Contains.ToAllOperators(expression[index - 1])) { throw new Exception("Переданный индекс оператора нельзя посчитать."); } int firstIndex = 0; int lastIndex = 0; for (int i = index - 1; i >= 0; i--) { if (i == 0 && !Contains.ToBrackets(expression[i])) { firstIndex = i; break; } else if (expression[i] == '-' && Contains.ToAllOperators( expression[i - 1])) { continue; } else if (Contains.ToAllOperators(expression[i]) && !Contains.ToSeparators(expression[i])) { firstIndex = i + 1; break; } } for (int i = index + 1; i < expression.Length; i++) { if (i == expression.Length - 1) { lastIndex = i; break; } else if (i - index + 1 == 0 && expression[i] == '-') { continue; } else if (Contains.ToAllOperators(expression[i + 1]) && !Contains.ToSeparators(expression[i + 1])) { lastIndex = i; break; } } int subStringSize = lastIndex + 1 - firstIndex; string result = expression.Substring(firstIndex, subStringSize); return(result); }