示例#1
0
        public static string Evaluate(IEnumerable <Token> expression)
        {
            var stack = new Stack <Token>();

            foreach (var token in expression)
            {
                if (token.Type == TokenType.Variable)
                {
                    stack.Push(token);
                }
                var ft = new FormatTranslator();
                if (token.Type != TokenType.Operator)
                {
                    continue;
                }
                {
                    var    op2 = stack.Pop().ToString();
                    string smallRes;
                    if (token.Value == "!")
                    {
                        smallRes = BaseMath.DoMath(ft.Factory(op2), token.Value);
                    }
                    else
                    {
                        var op1 = stack.Pop().ToString();
                        smallRes = BaseMath.DoMath(ft.Factory(op1), ft.Factory(op2), token.Value);
                    }
                    var v = new Token(TokenType.Variable, smallRes);
                    stack.Push(v);
                }
            }
            return(stack.Pop().ToString());
        }
示例#2
0
        public void TwoIntSum()
        {
            var ft = new FormatTranslator();
            var a  = "2";
            var b  = "2";

            Assert.AreEqual("4", BaseMath.DoMath(ft.Factory(a), ft.Factory(b), "+"));
        }
示例#3
0
        private List <GOAPAction> GetBestPlan(TreeDecision <GOAPAction> plans)
        {
            var bestCost = _comparer.BadCost;
            var bestPlan = new List <GOAPAction>();
            var stack    = new Stack <GOAPAction>();

            foreach (var child in plans.Root.Children)
            {
                Iterate(_comparer.ZeroCost, child);
            }

            void Iterate(IGOAPCost previusCost, TreeElement <GOAPAction> element)
            {
                var newCost = previusCost.GetSumWith(element.Content.Cost);

                stack.Push(element.Content);

                if (!element.HasChildren)
                {
                    int value = _comparer.Compare(bestCost, newCost);

                    if (value < 0 || (value == 0 && BaseMath.Probability(0.5f)))
                    {
                        bestCost = newCost;
                        bestPlan = stack.Where(x => !x.IsConnector).ToList();
                    }

                    return;
                }

                foreach (var child in element.Children)
                {
                    Iterate(newCost, child);
                }

                while (stack.Pop() != element.Content)
                {
                    ;
                }
            }

            return(bestPlan);
        }
示例#4
0
        public void SubstractDoubleFromString()
        {
            var ft = new FormatTranslator();

            try
            {
                var rr = BaseMath.DoMath(ft.Factory("4"), ft.Factory("4.4"), "*");
                Debug.WriteLine($"4*4.4={rr}");
            }
            catch (InvalidOperationException ex)
            {
                Assert.AreEqual(ex.Message, "Operator '-' cannot be applied to operands of type 'System.String' and 'System.Double'.");
            }
            try
            {
                BaseMath.DoMath(ft.Factory("asd"), ft.Factory("asd"), "*");
            }
            catch (InvalidOperationException ex)
            {
                Debug.WriteLine(ex.Message);
                Assert.AreEqual(ex.Message, "Operator '*' cannot be applied to operands of type 'System.String' and 'System.String'.");
            }
        }