/// <summary> /// This function resets the parser to process a new equation. /// </summary> private void ResetForNewEquation() { m_startPosition = 0; m_negativeOperatorFlag = false; m_equalSignInEquationFlag = false; m_atLeastOneVariableInEquationFlag = false; m_termBeforeEqualSignExistsFlag = false; m_termAfterEqualSignExistsFlag = false; m_parserState = LinearEquationParserState.ParseTerm; m_equationIndex++; }
/// <summary> /// This function resets the parser to its initial state for parsing /// a new set of simultaneous linear equations. /// </summary> void Reset() { m_startPosition = 0; ErrorPosition = 0; LastStatusValue = LinearEquationParserStatus.Success; m_negativeOperatorFlag = false; m_equalSignInEquationFlag = false; m_atLeastOneVariableInEquationFlag = false; m_termBeforeEqualSignExistsFlag = false; m_termAfterEqualSignExistsFlag = false; m_parserState = LinearEquationParserState.ParseTerm; m_equationIndex = 0; }
public LinearEquationParserStatus Parse(string inputLine, Sparse2DMatrix <int, int, double> aMatrix, SparseArray <int, double> bVector, SparseArray <string, int> variableNameIndexMap, ref int numberOfEquations) { //------------------------------------------------------------------ // Trim any space characters from the end of the line. //------------------------------------------------------------------ inputLine.TrimEnd(null); //------------------------------------------------------------------ // Assume success status. //------------------------------------------------------------------ int positionIndex = 0; SetLastStatusValue(LinearEquationParserStatus.Success, positionIndex); //------------------------------------------------------------------ // Skip whitespace characters //------------------------------------------------------------------ SkipSpaces(inputLine, ref positionIndex); m_startPosition = positionIndex; bool operatorFoundLast = false; while (positionIndex < inputLine.Length) { if (m_parserState == LinearEquationParserState.ParseTerm) { //------------------------------------------------------ // Skip whitespace characters //------------------------------------------------------ SkipSpaces(inputLine, ref positionIndex); if (positionIndex < inputLine.Length) { if (GetTerm(inputLine, ref positionIndex, aMatrix, bVector, variableNameIndexMap)) { m_parserState = LinearEquationParserState.ParseOperator; operatorFoundLast = false; } else { if (LastStatusValue == LinearEquationParserStatus.Success) { SetLastStatusValue(LinearEquationParserStatus.ErrorIllegalEquation, positionIndex); } break; } } else { break; } } else if (m_parserState == LinearEquationParserState.ParseOperator) { //------------------------------------------------------ // Skip whitespace characters //------------------------------------------------------ SkipSpaces(inputLine, ref positionIndex); if (positionIndex < inputLine.Length) { //------------------------------------------------------ // Check for plus sign, minus sign, or an equal sign. //------------------------------------------------------ if (GetOperator(inputLine, ref positionIndex)) { m_parserState = LinearEquationParserState.ParseTerm; operatorFoundLast = true; } else { if (LastStatusValue != LinearEquationParserStatus.Success) { if (positionIndex == m_startPosition) { SetLastStatusValue(LinearEquationParserStatus.ErrorIllegalEquation, positionIndex); } } break; } } } } // If an operator was found at if ((positionIndex >= inputLine.Length) && (positionIndex > 0) && (!operatorFoundLast)) { ResetForNewEquation(); numberOfEquations = m_equationIndex; } return(LastStatusValue); }
/// <summary> /// This function parses line that contains all or part of a simple /// linear equation. The equation contains terms separated by operators. /// The term can be a number, a variable, or a number and a variable. /// A term cannot be split between lines input to the parser method. /// The operators are either the plus character '+', the minus /// character '-', or the equal sign character '='. Numbers can have /// up to 15 digits, a decimal point, and an exponent of a power /// of 10 that follows the '^' character. /// </summary> /// <param name="inputLine">The input line of text to be parsed</param> /// <param name="aMatrix">The A matrix for the simultaneous equations. /// This is updated as each line of input is parsed. /// </param> /// <param name="bVector">The B vector for the simultaneous equations. /// This is updated as each line of input is parsed.</param> /// <param name="variableNameIndexMap">A map that stores the integer /// index for a variable using the variable name as a key.</param> /// <param name="numberOfEquations">A reference to an integer that /// will contain the number of equations when this method exits. /// </param> /// <returns>An enum of type LinearEquationParserStatus</returns> public LinearEquationParserStatus Parse(string inputLine, Sparse2DMatrix <int, int, double> aMatrix, SparseArray <int, double> bVector, SparseArray <string, int> variableNameIndexMap, ref int numberOfEquations) { //------------------------------------------------------------------ // Trim any space characters from the end of the line. //------------------------------------------------------------------ inputLine.TrimEnd(null); //------------------------------------------------------------------ // Assume success status. //------------------------------------------------------------------ int positionIndex = 0; SetLastStatusValue(LinearEquationParserStatus.Success, positionIndex); //------------------------------------------------------------------ // Skip whitespace characters //------------------------------------------------------------------ SkipSpaces(inputLine, ref positionIndex); //------------------------------------------------------------------ // Save the position of the first non-whitespace character. If // the first term is not found at this position then set the // error status to report that it is likely that the last // equation was not properly terminated. //------------------------------------------------------------------ m_startPosition = positionIndex; //------------------------------------------------------------------ // Separate the input string into tokens. // // Variables contains the letters A through Z and the underscore // '_' character. // // Operators include plus '+', minus '-', and times '*'. // // Delimiters include the equals sign '='. // // Numbers include the digits 0 through 9, the decimal point // (period character) '.', an optional exponent character which // is the letter '^', and up to two digits for the optional exponent. // // Check for sequences of terms and operators. // // Term: // <Space> <Sign> Number <Space> // <Space> <Sign> Variable <Space> // <Space> <Sign> Number Variable <Space> // // Operator: // <Space> Plus <Space> // <Space> Minus <Space> // <Space> Equals <Space> // //-------------------------------------------------------------- bool operatorFoundLast = false; while (positionIndex < inputLine.Length) { if (m_parserState == LinearEquationParserState.ParseTerm) { //------------------------------------------------------ // Skip whitespace characters //------------------------------------------------------ SkipSpaces(inputLine, ref positionIndex); if (positionIndex < inputLine.Length) { if (GetTerm(inputLine, ref positionIndex, aMatrix, bVector, variableNameIndexMap)) { m_parserState = LinearEquationParserState.ParseOperator; operatorFoundLast = false; } else { if (LastStatusValue == LinearEquationParserStatus.Success) { SetLastStatusValue(LinearEquationParserStatus.ErrorIllegalEquation, positionIndex); } break; } } else { break; } } else if (m_parserState == LinearEquationParserState.ParseOperator) { //------------------------------------------------------ // Skip whitespace characters //------------------------------------------------------ SkipSpaces(inputLine, ref positionIndex); if (positionIndex < inputLine.Length) { //------------------------------------------------------ // Check for plus sign, minus sign, or an equal sign. //------------------------------------------------------ if (GetOperator(inputLine, ref positionIndex)) { m_parserState = LinearEquationParserState.ParseTerm; operatorFoundLast = true; } else { if (LastStatusValue != LinearEquationParserStatus.Success) { if (positionIndex == m_startPosition) { SetLastStatusValue(LinearEquationParserStatus.ErrorIllegalEquation, positionIndex); } } break; } } } } // If an operator was found at if ((positionIndex >= inputLine.Length) && (positionIndex > 0) && (!operatorFoundLast)) { ResetForNewEquation(); numberOfEquations = m_equationIndex; } return(LastStatusValue); }