Exemple #1
0
        }                   // end Operator syncor functions.

        public void Solve() // the method deals with the prelimanry making of the Expression Understading.
        {
            OperatorSync(); // Syncs the operators to be considered.
            SyncKeyWord();
            ConstantSync();
            if (givenExpression.Contains("(") || givenExpression.Contains(")"))
            {
                Only_Number_And_Operator_List_Maker();
                if (!(ExpressionElements[ExpressionElements.Count - 1] == ")") && theBasicOperatorsString.Contains(ExpressionElements[ExpressionElements.Count - 1]))
                {
                    Processed = false;
                    throw new MathParserException($"" +
                                                  "Invalid operator entry. No basic operator ({theBasicOperatorList.ToString()}) can be at the end of the expression.");
                }
                the_Data_List_Maker();
                OperatorHandler
                    oh = new OperatorHandler(ref ExpressionElements);
                oh.BasicOperatorsString = theBasicOperatorsString;
                oh.Process();
                if (!oh.isCorrectOperatorSequence())
                {
                    Processed = false;
                    throw new MathParserException("Bad operator sequence.");
                }

                // Balancing brakkets.
                if (ExpressionElements.Contains("(") || ExpressionElements.Contains("-(") || ExpressionElements.Contains(")"))
                {
                    int nsb = 0;
                    int neb = 0;
                    foreach (string s in ExpressionElements)
                    {
                        if (s.Contains("("))
                        {
                            nsb++;
                        }
                        if (s.Contains(")"))
                        {
                            neb++;
                        }
                    }
                    if (nsb > neb)
                    {
                        for (int c = 0; c < (nsb - neb); c++)
                        {
                            ExpressionElements.Add(")");
                        }
                    }
                    if (neb > nsb)
                    {
                        List <string> dumy = new List <string>();
                        for (int c = 0; c < (-nsb + neb); c++)
                        {
                            dumy.Add("(");
                        }
                        foreach (string x in ExpressionElements)
                        {
                            dumy.Add(x);
                        }
                        ExpressionElements = dumy;
                    }
                }
                // end balancing brakkets


                MathParser.BraketSolver sol = new BraketSolver(ExpressionElements, theData);
                if (sol.isProcessed())
                {
                    Solution = sol.getSolution();
                }
                else
                {
                    Processed = false;
                    throw new MathParserException("Error");
                }
            }
            else if ((givenExpression.Contains("[") && !givenExpression.Contains("]")) || (!givenExpression.Contains("[") && givenExpression.Contains(("]"))))               // if the expession string contans an invalid matrix formate.
            {
                throw (new MathParserException("Invalid Matrix Formate. Entered."));
            }
            else               // if the expression string contain only operators and numbers.
            {
                Only_Number_And_Operator_List_Maker();
                if (theBasicOperatorsString.Contains(ExpressionElements[ExpressionElements.Count - 1]))
                {
                    Processed = false;

                    throw new MathParserException($"Invalid operator entry. No basic operator ({theBasicOperatorList.ToString()}) can be at the end of the expression.");
                }
                else
                {
                    the_Data_List_Maker();
                    OperatorHandler oh = new OperatorHandler(ref ExpressionElements);
                    oh.BasicOperatorsString = theBasicOperatorsString;
                    oh.Process();

                    if (!oh.isCorrectOperatorSequence())
                    {
                        Processed = false;
                        throw new MathParserException("Bad operator sequence.");
                    }
                    else
                    {
                        DMASSolver DS = new DMASSolver(ExpressionElements, theData);
                        DS.Solve();
                        if (DS.isProcessed())
                        {
                            Solution = new MathParserExpression(DS.getSolution());
                        }
                        else
                        {
                            Processed = false;
                        }
                    }
                }    // end
            }        // end
        }            // end solve
Exemple #2
0
 void Solve()
 {
     if (!(ExpressionElements.Contains("(") || ExpressionElements.Contains(")")))
     {
         DMASSolver sol = new DMASSolver(ExpressionElements, theData);
         sol.Solve();
         if (sol.isProcessed())
         {
             solution = sol.getSolution();
         }
         else
         {
             Processed = false;
             throw new MathParserException("The expression could not be solved");
         }
     }
     else
     {
         bool          intake        = false;
         int           bstart        = 0;
         int           belements     = 1;
         List <string> theBraketList = new List <string>();
         for (int c = 0; c < ExpressionElements.Count; c++)
         {
             string Element = ExpressionElements [c];
             if (!ExpressionElements.Contains("(") && !ExpressionElements.Contains("-("))
             {
                 break;
             }
             if (Element == ")")
             {
                 intake = false;
                 DMASSolver sol = new DMASSolver(theBraketList, theData);
                 sol.Solve();
                 if (sol.isProcessed())
                 {
                     MathParserExpression ANS = sol.getSolution();
                     if (ExpressionElements [bstart].Contains("-"))
                     {
                         if (ANS.Type.Contains("Number"))
                         {
                             Number g = ANS.Data;
                             g   = g * new Number((double)-1);
                             ANS = new MathParserExpression(g);
                         }
                         else if (ANS.Type.Contains("Matrix"))
                         {
                             Matrix g = ANS.Data;
                             g   = g * -1;
                             ANS = new MathParserExpression(g);
                         }
                     }
                     string name = autoNamer();
                     theData.Add(name, ANS);
                     ExpressionElements [bstart] = name;
                     ExpressionElements.RemoveRange(bstart + 1, belements + 1);
                     c = -1;
                 }
                 else
                 {
                     Processed = false;
                     throw new MathParserException("Error");
                 }
                 belements = 0;
             }
             if (intake)
             {
                 theBraketList.Add(Element);
                 belements++;
             }
             if (Element.Contains("("))
             {
                 theBraketList = new List <string> ();
                 intake        = true;
                 bstart        = c;
                 belements     = 0;
             }
         }
         BraketSolver bsol = new BraketSolver(ExpressionElements, theData);
         if (bsol.isProcessed())
         {
             solution = bsol.getSolution();
         }
         else
         {
             Processed = false;
             throw new MathParserException("Error");
         }
     }
 }