public void EvaluatePostFixString()
        {
            // Arrange
            string postFixString = " 6 m 1 m 5 - m / 1 m +  "; // == -6/-(-1-5)+-1 (infix)
            PostfixEvaluator pe = new PostfixEvaluator();

            // Act
            decimal result = pe.Evaluate(postFixString);

            // Assert
            Assert.AreEqual(-2m, result, string.Format("Evaluation of \"{0}\" failed!", postFixString));

            // Arrange
            postFixString = " 6 m 1 m 5 - m / 1 p +  "; // == -6/-(-1-5)++1 (infix)

            // Act
            result = pe.Evaluate(postFixString);

            // Assert
            Assert.AreEqual(0m, result, string.Format("Evaluation of \"{0}\" failed!", postFixString));

            // Arrange
            postFixString = "3 4 2 * 1 5 - 2 3 ^ ^ / +"; // 3 + 4 * 2 / (1 - 5) ^ 2 ^ 3 (infix)

            // Act
            result = pe.Evaluate(postFixString);

            // Assert
            Assert.AreEqual(3.0001220703125m, result, string.Format("Evaluation of \"{0}\" failed!", postFixString));

            // Arrange
            postFixString = "1 1 + 1 + 1 +"; // 1+(1)+(1)+1 (infix)

            // Act
            result = pe.Evaluate(postFixString);

            // Assert
            Assert.AreEqual(4m, result, string.Format("Evaluation of \"{0}\" failed!", postFixString));

            // Arrange
            postFixString = "1 1 m + 1 m + 1 +"; // 1+(-1)+(-1)+1 (infix)

            // Act
            result = pe.Evaluate(postFixString);

            // Assert
            Assert.AreEqual(0m, result, string.Format("Evaluation of \"{0}\" failed!", postFixString));

            // Arrange
            postFixString = "1 1 m -"; // (1)-(-1) (infix)

            // Act
            result = pe.Evaluate(postFixString);

            // Assert
            Assert.AreEqual(2m, result, string.Format("Evaluation of \"{0}\" failed!", postFixString));

            //
        }
Пример #2
0
        private void button2_Click(object sender, EventArgs e)
        {
            tbTokenOutput.AppendText(string.Format("PostfixEvaluation:{0}", Environment.NewLine));

            try
            {
                PostfixEvaluator eva = new PostfixEvaluator();
                var result = eva.Evaluate(tbPostfix.Text);
                tbResult.Text = result.ToString();
            }
            catch (Exception ex)
            {
                tbResult.Text = string.Format("Error: {0}{1}", ex.Message, Environment.NewLine);
                tbTokenOutput.AppendText(tbResult.Text);
            }
        }
Пример #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ShowTokens();

                InfixToPostfixConverter conv = new InfixToPostfixConverter(new Tokenizer());
                var postfix = conv.Convert(tbSource.Text);
                tbPostfix.Text = postfix;

                PostfixEvaluator eva = new PostfixEvaluator();
                var result = eva.Evaluate(postfix);
                tbResult.Text = result.ToString();
            }
            catch (Exception ex)
            {
                tbResult.Text = string.Format("Error: {0}{1}", ex.Message, Environment.NewLine);
                tbTokenOutput.AppendText(tbResult.Text);
            }
        }