isNegativeUnaryOperator function
Implementation
bool isNegativeUnaryOperator(String expression, int index) {
// If the index is invalid, the character cannot be a negative unary operator.
if (index >= expression.length) {
return false;
}
// Extract the character at the given index.
final String operator = expression.substring(index, index + 1);
// If the character is not a minus sign,
// it cannot be a negative unary operator.
if (operator != '-') {
return false;
}
// If the character is a minus sign, check if it's a negative unary operator
// by examining the previous character in the expression.
if (index == 0) {
// If the minus sign is the first character in the expression, it is a
// unary operator.
return true;
}
// Extract the previous character in the expression.
final String prevChar = expression.substring(index - 1, index);
// Check if the previous character is an operator, left parenthesis.
// If it is any of these, then the minus sign is a unary operator.
if (isOperator(prevChar) || prevChar == '(') {
return true;
}
// If the previous character is not an operator, left parenthesis, or
// space, the minus sign is not a unary operator.
return false;
}