Esempio n. 1
0
        private void Calculate_button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
                customCulture.NumberFormat.NumberDecimalSeparator = ".";

                System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

                double result = 0.0;
                var    rpn    = new ReversePolishNotation();
                rpn.Parse(Expression_textBox.Text.Replace(',', '.'));
                result = rpn.Evaluate();

                var output = new StringBuilder();

                output.AppendFormat("Original:   {0}", rpn.OriginalExpression + Environment.NewLine);
                output.AppendFormat("Transition: {0}", rpn.TransitionExpression + Environment.NewLine);
                output.AppendFormat("Postfix:    {0}", rpn.PostfixExpression + Environment.NewLine);
                output.AppendFormat("Result:     {0}", result);

                Results_textBox.Text = output.ToString();
            }
            catch (Exception ex)
            {
                Results_textBox.Text = ex.Message;
            }
        }
Esempio n. 2
0
        public void TestMethod36()
        {
            var expression = "\"0E984725-C51C-4BF4-9960-E1C80E27ABA0\" as guid =  \"0E984725-C51C-4BF4-9960-E1C80E27ABA0\" as guid";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens);

            Assert.AreEqual(t, true);
        }
Esempio n. 3
0
        public void TestMethod35()
        {
            var expression = "\"4 / 3 / 2007 2:23:57 AM\" as datetimeoffset =  \"4 / 3 / 2007 2:23:57 AM\" as datetimeoffset'";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens);

            Assert.AreEqual(t, true);
        }
Esempio n. 4
0
        public void TestMethod30()
        {
            var expression = "\"12:12:12\" as timespan > \"12:12:10\" as timespan";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens);

            Assert.AreEqual(t, true);
        }
Esempio n. 5
0
        public void TestMethod22()
        {
            var expression = "11 + 10 > 12 and \"3.5\" as double > 0.5";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens);

            Assert.AreEqual(t, true);
        }
Esempio n. 6
0
        public void TestMethod17()
        {
            var expression = "( 5 + 2 ) * 3 + 2 * ( 1 + 3 ) = 29";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens);

            Assert.AreEqual(t, true);
        }
Esempio n. 7
0
        public void TestMethod12()
        {
            var expression = "(1 * (-3)) + (5 - 8) * 3 + 0/2 = -12";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens);

            Assert.AreEqual(t, true);
        }
Esempio n. 8
0
        public void TestMethod10()
        {
            var expression = "-1 - 4 as int = 0 as int - 5 as int";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens);

            Assert.AreEqual(t, true);
        }
Esempio n. 9
0
        public void TestMethod9()
        {
            var expression = "-3 + 5 = 2";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens);

            Assert.AreEqual(t, true);
        }
Esempio n. 10
0
        public void TestMethod7()
        {
            var expression = "1 + f = 3";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens, new Dictionary <string, object>()
            {
                { "f", 2 },
            });

            Assert.AreEqual(t, true);
        }
Esempio n. 11
0
        public void TestMethod129()
        {
            var expression = "\"12.12:12:12\" as timespan = f";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens, new Dictionary <string, object>()
            {
                { "f", new TimeSpan(12, 12, 12, 12) }
            });

            Assert.AreEqual(t, true);
        }
Esempio n. 12
0
        public void TestMethod23()
        {
            var expression = "f = z";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens, new Dictionary <string, object>()
            {
                { "f", new byte[] { 1, 2 } },
                { "z", new byte[] { 1, 2 } }
            });

            Assert.AreEqual(t, true);
        }
Esempio n. 13
0
        public void TestMethod26()
        {
            var expression = "f > z";
            var tokens     = ReversePolishNotation.GetTokens(expression);
            var t          = ReversePolishNotation.Evaluate(tokens, new Dictionary <string, object>()
            {
                { "f", new DateTime(2, 1, 1) },
                { "z", new DateTime(1, 1, 1) }
            });

            Assert.AreEqual(t, true);
        }
Esempio n. 14
0
    public static void Main(string[] args)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

        double result             = 0.0;
        ReversePolishNotation rpn = new ReversePolishNotation();

        // rpn.Parse("pow(2, 3.14) * (3 - (3 * sqrt(2) - 3.2) + 1.5*0.3)");
        rpn.Parse("(3+5.3) * 2.7 - ln(22) / pow(2.2, -1.7)");
        result = rpn.Evaluate();
        Console.WriteLine("orig:   {0}", rpn.OriginalExpression);
        Console.WriteLine("tran:   {0}", rpn.TransitionExpression);
        Console.WriteLine("post:   {0}", rpn.PostfixExpression);
        Console.WriteLine("result: {0}", result);
    }
        static void Main(string[] args)
        {
            Console.Write("Enter an expression: ");
            string expression = Console.ReadLine();

            try
            {
                ReversePolishNotation rpn = new ReversePolishNotation();
                rpn.Parse(expression);
                Console.WriteLine("Result: {0}", rpn.Evaluate());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Esempio n. 16
0
        static void Main(string[] args)
        {
            while (true)
            {
                string input = PromptGetResponse(Messages.Prompt_EnterExpression);
                if (input.ToLower() == QuitKeyword.ToLower())
                {
                    break;
                }

                string postfix;
                int    result;

                try
                {
                    postfix = ShuntingYardAlgorithm.Convert(input);
                }
                catch (Exception)
                {
                    DisplayMessage(Messages.ErrorMessage_PostfixParseFailed, isError: true);
                    continue;
                }

                try
                {
                    result = ReversePolishNotation.Evaluate(postfix);
                }
                catch (Exception ex)
                {
                    // special case in that it's very clear what the problem is
                    if (ex is DivideByZeroException)
                    {
                        DisplayMessage(Messages.ErrorMessage_EvaluationGeneratesDivideByZero, isError: true);
                    }
                    else
                    {
                        DisplayMessage(Messages.ErrorMessage_EvaluationFailed, isError: true);
                    }

                    continue;
                }

                DisplayMessage($"{Messages.Message_Result} {result}");
            }
        }
Esempio n. 17
0
 public void WhenEvaluate_InvalidExpression_ThrowsException(string input)
 {
     Assert.ThrowsException <FormatException>(() => ReversePolishNotation.Evaluate(input));
 }
Esempio n. 18
0
        public void WhenEvaluate_ValidExpression_Returns_CorrectResult(string input, int expectedOutput)
        {
            int output = ReversePolishNotation.Evaluate(input);

            Assert.AreEqual(expectedOutput, output);
        }