예제 #1
0
        /// <summary>
        ///This will take in one line, the two matrices to enter the values in, the variable characters and if it is the first line or not.
        ///it will parse it to get the one or two coefficients and the answer from the input
        /// </summary>
        /// <param name="input">This is the string that will be parsed to get</param>
        /// <param name="coefficients">this matrix holds the values of the coefficients for the variables from both equations</param>
        /// <param name="answers">this matrix holds the answers from both equations</param>
        /// <param name="var1">this is the value of the first variable (it is ' ' if it has not been assigned)</param>
        /// <param name="var2">this is the value of the second variable (it is ' ' if it has not been assigned)</param>
        /// <param name="firstLine">this will determine if it is the first line to be parsed</param>
        /// <returns>This only passes values by reference, there is no return</returns>
        public static void parseEquation(string input, Matrix coefficients, Matrix answers, ref char var1, ref char var2, bool firstLine)
        {
            int    index = 0;
            double temp;
            int    line = (firstLine)? LINE1: LINE2;                                                            //this will determine which lines values are being parsed
            char   nextVar;                                                                                     //this will hold the other variable that has not been assigned on
            int    nextVarIndex;

            temp = Parsing.parseCoefficient(input, ref index);                                              //parse the string for the first coefficient

            if (firstLine)                                                                                  //this means that var1 has not been assigned yet
            {
                #region Save first variable and coefficient
                coefficients.SetCell(line, VAR1, temp);                                                     //assign the value for the first coefficient
                var1 = Char.ToLower(input[index]);                                                          //get the variable at the index after the coefficient
                #endregion

                index++;                                                                                    //increment to check next character

                #region Check second part of the equation for variable or answer
                switch (input[index])                                                                       //is it the end of the equation or is there a second variable
                {
                    #region go to answer
                case '=':                                                                                   //no second coefficient, go straight to answer
                    coefficients.SetCell(line, VAR2, 0);                                                    //assign 0 to second variable
                    index++;                                                                                //start the index after the equals sign for parsing
                    answers.SetCell(line, 0, Parsing.parseDouble(input, ref index));                        //get the answer to the equation
                    break;

                    #endregion
                    #region save second coefficient and variable and answer
                case '+':                                                                                   //there is a second variable
                case '-':
                    #region save second coefficient and variable
                    temp = Parsing.parseCoefficient(input, ref index);                                      //get second variable coefficient

                    if (input[index] == var1)                                                               //if the second variable is the same as the first throw an exception
                    {
                        throw new ParseException("Invalid characters detected, double use of the same variable!");
                    }

                    coefficients.SetCell(line, VAR2, temp);

                    var2 = Char.ToLower(input[index]);                                                      //store the second variable
                    index++;                                                                                //increment to check next character

                    if (input[index] != '=')
                    {
                        throw new ParseException("Invalid characters detected at index " + index + ". Must be \"=\"");
                    }
                    else
                    {
                        index++;                                                                            //increment to check next character
                    }
                    #endregion
                    #region save answer
                    answers.SetCell(line, 0, Parsing.parseDouble(input, ref index));                        //get answer from the end of the input
                    #endregion
                    break;

                default:
                    throw new ParseException("Invalid characters detected at index " + index);
                    #endregion
                }
                #endregion
            }
            else                                                                                            //this is the second line
            {
                #region check variable to assign coefficient
                if (input[index] == var1)                                                                   //assign to the coefficient corresponsing to the variable
                {
                    coefficients.SetCell(line, VAR1, temp);                                                 //assign the value for the first coefficient
                    nextVar      = Char.ToLower(var2);
                    nextVarIndex = VAR2;
                }
                else if (input[index] == var2)
                {
                    coefficients.SetCell(line, VAR2, temp);                                                 //assign the value for the second coefficient
                    nextVar      = Char.ToLower(var1);
                    nextVarIndex = VAR1;
                }
                else if (var2 == UNASSIGNED)
                {
                    var2 = input[index];                                                                    //assign the previously empty var2
                    coefficients.SetCell(line, VAR2, temp);
                    nextVar      = Char.ToLower(var1);
                    nextVarIndex = VAR1;
                }
                else
                {
                    throw new ParseException("Invalid variable detected at line: " + line + ", index: " + index);
                }
                #endregion

                index++;                                                                                    //increment to check next character

                #region Check second part of the equation
                switch (input[index])                                                                       //is it the end of the equation or is there a second variable
                {
                    #region go to answer
                case '=':                                                                                   //no second coefficient, go straight to answer
                    coefficients.SetCell(line, nextVarIndex, 0);                                            //assign 0 to second variable
                    index++;                                                                                //start the index after the equals sign for parsing
                    answers.SetCell(line, 0, Parsing.parseDouble(input, ref index));                        //get the answer to the equation
                    break;

                    #endregion
                    #region save second coefficient and variable and answer
                case '+':                                                                                   //there is a second variable
                case '-':
                    #region save second coefficient and variable
                    coefficients.SetCell(line, nextVarIndex, Parsing.parseCoefficient(input, ref index));   //get second variable coefficient

                    if (input[index] != nextVar)                                                            //if the second variable is the same as the first throw an exception
                    {
                        throw new ParseException("Invalid characters detected, invalid second variable");
                    }

                    index++;                                                                                //increment to check next character

                    if (input[index] != '=')                                                                //it needs to be an equal sign or is does not work
                    {
                        throw new ParseException("Invalid characters detected at index " + index + ". Must be \"=\"");
                    }
                    else
                    {
                        index++;                                                                            //increment to check next character
                    }
                    #endregion
                    #region save answer
                    answers.SetCell(line, 0, Parsing.parseDouble(input, ref index));
                    #endregion
                    break;

                default:
                    throw new ParseException("Invalid characters detected at index " + index);
                    #endregion
                }
                #endregion
            }
        }