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();
        }