Example #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());
        }
Example #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), "+"));
        }
Example #3
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'.");
            }
        }