Example #1
0
        static void ParseEquations(string input1 = "", string line2 = "")
        {
            bool   valid = true;
            char   var1 = UNASSIGNED, var2 = UNASSIGNED;                                                                //blank space will be the default when it is unassigned
            Matrix coefficients;                                                                                        //this will hold the coefficients for the variables
            Matrix answers;                                                                                             //this will hold the answers for both equations
            Matrix inverse;                                                                                             //this will hold the inverse of the coefficients matrix
            Matrix varValues;                                                                                           //this will hold the values of the variables after solving for them
            string input2 = line2;                                                                                      //second line user input

            if (input1 != "")
            {
                WriteLine("Equation 1: " + input1 + "\n");
            }

            coefficients = new Matrix(2, 2);                                                                            //initialize three matrices
            coefficients = new Matrix(2, 2);                                                                            //initialize three matrices
            answers      = new Matrix(2, 1);                                                                            //initialize three matrices

            try{                                                                                                        //parse first equation
                Parsing.parseEquation(input1, coefficients, answers, ref var1, ref var2, true);
            }
            catch (ParseException e) {
                WriteLine(e + "\n");
                valid = false;
            }

            if (valid)
            {
                //Get second line and parse the variables
                if (input2 == "")
                {
                    Write("\nEnter the second equation \t-->  ");
                    input2 = ReadLine();
                }
                else
                {
                    WriteLine("Equation 2: " + input2 + "\n");
                }

                try{                                                                                                    //parse second equation
                    Parsing.parseEquation(input2, coefficients, answers, ref var1, ref var2, false);
                }
                catch (ParseException e) {
                    WriteLine(e + "\n");
                    valid = false;
                }

                if (valid)
                {
                    WriteLine("\nThis is the coefficients matrix\n" + coefficients.ToString());                         //output the values of the coefficients

                    try{
                        inverse = coefficients.Inverse();                                                               //calculate the inverse of the coefficient matrix
                        WriteLine("This is the inverse of the coefficients matrix\n" + inverse.ToString());

                        WriteLine("This is the answers matrix\n" + answers.ToString());

                        varValues = inverse.Mult(answers);                                                              //find matrix X by doing coefficients.inverse * answers matrices
                        WriteLine("This is the solution of the multiplication of the inverse coefficients matrix and the answers matrix\n" + varValues.ToString());

                        WriteLine(var1 + " = " + varValues.GetCell(VAR1, 0));
                        WriteLine(var2 + " = " + varValues.GetCell(VAR2, 0));
                    }
                    catch (Exception e) {
                        WriteLine(e + "\nThe equations could not be calculated\n");
                    }
                }
            }

            WriteLine("\nPress Enter to continue...");
            ReadLine();

            Clear();
        }
Example #2
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
            }
        }