コード例 #1
0
        static void Main(string[] args)
        {
            // Check performance

            string s1            = "pi+5*5+5*3-5*5-5*3+1E1";
            string s2            = "sin(cos(tg(sh(ch(th(100))))))";
            int    numberOfTests = 100000;
            long   sum           = 0;

            MathParser parser = new MathParser();
            var        rpn    = parser.ConvertToRPN(s2.ToLower());

            Console.WriteLine(rpn);
            return;


            for (int i = 0; i < numberOfTests; i++)
            {
                sum += Parse(s1, s2);
            }
            Console.WriteLine("Average performance of math parser is {0} ticks",
                              sum / numberOfTests);

            Console.ReadKey(true);
        }
コード例 #2
0
        static long Parse(string expr1, string expr2)
        {
            MathParser parser    = new MathParser();
            bool       isRadians = false;

            stopWatch.Restart();
            double d1 = parser.Parse(expr1, isRadians);
            double d2 = parser.Parse(expr2, isRadians);

            stopWatch.Stop();

            return(stopWatch.ElapsedTicks);
        }
コード例 #3
0
 public List<GlycanCompositionTable> calnumbers(List<GlycanCompositionTable> Table)
 {
     List<GlycanCompositionTable> CoTa0 = new List<GlycanCompositionTable>();
     String oneBound = "";
     foreach (GlycanCompositionTable j in Table)
     {
         List<String> CoTatwo = new List<String>();
         foreach (String bound in j.Bound)
         {
             oneBound = "";
             MathParser Pa = new MathParser();
             oneBound = Convert.ToString(Pa.Parse(bound, false));
             CoTatwo.Add(oneBound);
         }
         j.Bound.Clear();
         //While we're at it, we'll remove the duplicate bounds also.
         j.Bound.AddRange(CoTatwo.Distinct().ToList());
     }
     CoTa0 = Table;
     return CoTa0;
 }
コード例 #4
0
        private List<CompositionHypothesisEntry> checkAddRules(List<CompositionHypothesisEntry> Ans, GlycanHypothesisCombinatorialGenerator GD)
        {
            List<CompositionHypothesisEntry> CHy = new List<CompositionHypothesisEntry>();
            List<string> elementIDs = new List<string>();
            for (int j = 0; j < Ans.Count(); j++)
            {
                if (Ans[j].ElementNames.Count > 0)
                {
                    for (int i = 0; i < Ans[j].ElementNames.Count(); i++)
                    {
                        elementIDs.Add(Ans[j].ElementNames[i]);
                    }
                    break;
                }
            }

            for (int i = 0; i < CHy.Count(); i++)
            {
                CHy[i].ElementNames.Clear();
                CHy[i].MoleculeNames.Clear();

                if (i == CHy.Count() - 1)
                {
                    CHy[0].ElementNames = elementIDs;
                }
            }
            Boolean RowGood = true;
            try
            {
                if (GD.aTable.Count != 0)
                {
                    for (int j = 0; j < Ans.Count; j++)
                    {
                        for (int i = 0; i < GD.aTable.Count(); i++)
                        {
                            //MessageBox.Show(Convert.ToString(c));
                            String equation = translet(Ans[j], GD.aTable[i]);
                            String Relationship = GD.aTable[i].Relationship;
                            String Constraint = GD.aTable[i].Constraint;
                            Constraint = converConstraints(Ans[j],Constraint);
                            //Five relationships
                            //First one
                            if (Relationship == "=")
                            {
                                MathParser parser = new MathParser();
                                Double solution = parser.Parse(equation, false);
                                //only 3 situation exists
                                //Equals to Even
                                if (Constraint == "even")
                                {
                                    if (solution % 2 != 0)
                                    {
                                        RowGood = false;
                                    }
                                    continue;
                                }
                                //Equals to Odd
                                if (Constraint == "odd")
                                {
                                    if (solution % 2 != 1)
                                    {
                                        RowGood = false;
                                    }
                                    continue;
                                }
                                //Equals to a number
                                MathParser parser2 = new MathParser();
                                Double solution2 = parser2.Parse(Constraint, false);
                                if (solution != solution2)
                                {
                                    RowGood = false;
                                }
                                continue;
                            }
                            //Second one
                            else if (Relationship == ">")
                            {
                                MathParser parser = new MathParser();
                                Double solution = parser.Parse(equation, false);
                                //only 1 situation exists
                                //Equals to a number
                                MathParser parser2 = new MathParser();
                                Double solution2 = parser2.Parse(Constraint, false);
                                if (solution <= solution2)
                                {
                                    RowGood = false;
                                }
                                continue;
                            }
                            //Third one
                            else if (Relationship == "<")
                            {
                                MathParser parser = new MathParser();
                                Double solution = parser.Parse(equation, false);
                                //only 1 situation exists
                                //Equals to a number
                                MathParser parser2 = new MathParser();
                                Double solution2 = parser2.Parse(Constraint, false);
                                if (solution >= solution2)
                                {
                                    RowGood = false;
                                }
                                continue;
                            }
                            //Forth one
                            else if (Relationship == "≥")
                            {
                                MathParser parser = new MathParser();
                                Double solution = parser.Parse(equation, false);
                                //only 1 situation exists
                                //Equals to a number
                                MathParser parser2 = new MathParser();
                                Double solution2 = parser2.Parse(Constraint, false);
                                if (solution < solution2)
                                {
                                    RowGood = false;
                                }
                                continue;
                            }
                            //Fifth one
                            else if (Relationship == "≤")
                            {
                                MathParser parser = new MathParser();
                                Double solution = parser.Parse(equation, false);
                                //only 1 situation exists
                                //Equals to a number
                                MathParser parser2 = new MathParser();
                                Double solution2 = parser2.Parse(Constraint, false);
                                if (solution > solution2)
                                {
                                    RowGood = false;
                                }
                                continue;
                            }
                            //Last one
                            else if (Relationship == "≠")
                            {
                                MathParser parser = new MathParser();
                                Double solution = parser.Parse(equation, false);
                                //only 1 situation exists
                                //Equals to a number
                                MathParser parser2 = new MathParser();
                                Double solution2 = parser2.Parse(Constraint, false);
                                if (solution == solution2)
                                {
                                    RowGood = false;
                                }
                                continue;
                            }
                        }
                        if (RowGood)
                            CHy.Add(Ans[j]);
                        else
                            RowGood = true;
                    }
                }
                else
                {
                    CHy.AddRange(Ans);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in Additional Rule Table. Please refer to the help section and check your input. Error Code:" + ex);
            }

            for (int i = 0; i < CHy.Count(); i++)
            {
                CHy[i].ElementNames.Clear();
                CHy[i].MoleculeNames.Clear();

                if (i == CHy.Count() - 1)
                {
                    CHy[0].ElementNames = elementIDs;
                }
            }
            return CHy;
        }
コード例 #5
0
 private Double getFragmentMass(String Fra)
 {
     String translate = "0";
     aminoAcidMass AAMass = new aminoAcidMass();
     foreach (char i in Fra)
     {
         translate = translate + "+" + Convert.ToString(AAMass.getMass(Convert.ToString(i)));
     }
     MathParser MTK = new MathParser();
     Double mass = MTK.Parse(translate, false);
     return mass;
 }
コード例 #6
0
        /// <summary>
        /// Evaluates the string in the textbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEquals_Click(object sender, EventArgs e)
        {
            MathParser parser = new MathParser();
            string equation = mathText.Text;
            bool isRadians = false;

            try
            {
                result = parser.Parse(equation, isRadians);
                mathText.Text = result.ToString();
                specialEvent = true;
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("Invalid Operation!");
            }
        }
コード例 #7
0
        /// <summary>
        /// Store the evaluated equation in the textbox and store it into a variable
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMS_Click(object sender, EventArgs e)
        {
            MathParser parser = new MathParser();
            string equation = mathText.Text;
            bool isRadians = false;

            try
            {
                storedNumber = parser.Parse(equation, isRadians);
                lblStoreNumber.Text = "M";
                specialEvent = true;
            }
            catch (InvalidOperationException)
            {

            }
        }
コード例 #8
0
    public static void TestReplaceVariablesWithValues(Thing thing, ThingPart thingPart)
    {
        #if !UNITY_EDITOR
        return;
        #endif

        if (!didExpressionTest)
        {
            didExpressionTest = true;
            bool incorrectFound = false;

            thing.behaviorScriptVariables = new Dictionary <string, float>();
            Managers.areaManager.behaviorScriptVariables = new Dictionary <string, float>();

            thing.behaviorScriptVariables.Add("foo", 257.6f);
            Managers.areaManager.behaviorScriptVariables.Add("area.gold", 6f);

            Dictionary <string, float> tests = new Dictionary <string, float> {
                { "18", 18.00f },
                { "foo", 257.60f },
                { "bar", 0.00f },
                { "foo / 10", 25.76f },
                { "foo/10", 25.76f },
                { "foo / 10 + foo * 2", 540.96f },
                { "foo/10+foo*2", 540.96f },
                { "floor(6.6)", 6.00f },
                { "floor(foo)", 257.00f },
                { "floor(foo / 10)", 25.00f },
                { " floor  (  foo  / 10  ) ", 25.00f },
                { "ceil( floor(foo / 10) * 2.5)", 63.00f },
                { "area.gold + area.bar", 6.00f },
                { "area.gold + area.gold", 12.00f },
                { "smaller(23 17)", 17.00f },
                { "smaller(foo 17)", 17.00f },
                { "smaller(17 foo)", 17.00f },
                { "smaller(17 foo 18) * [foo (bar smaller **", -1f },
            };

            MathParserTK.MathParser parser = new MathParserTK.MathParser();

            foreach (KeyValuePair <string, float> test in tests)
            {
                string expression    = test.Key;
                float  correctResult = test.Value;

                Debug.Log("Before: " + expression);
                expression = ReplaceVariablesWithValues(thing, thingPart, expression, doDebug: true);
                Debug.Log("After:  " + expression);

                float result = -1f;
                try
                {
                    result = (float)parser.Parse(expression);
                }
                catch
                {
                }
                Debug.Log("Result: " + result);

                bool isCorrect = result == correctResult;
                if (!isCorrect)
                {
                    incorrectFound = true;
                }
                Debug.Log(isCorrect ?
                          "Correct" :
                          ">>> Incorrect (expected " + correctResult.ToString() + ")"
                          );

                Debug.Log("------------------");
            }

            Debug.Log(!incorrectFound ? "- All Correct -" : "- Some incorrect -");
        }
    }