Пример #1
0
 public void calculateTest()
 {
     RpnExpression re = new RpnExpression();
     Assert.AreEqual(300, re.calculate(100, 200, "+"));
     Assert.AreEqual(-100, re.calculate(100, 200, "-"));
     Assert.AreEqual(0.0693, re.calculate(0.33, 0.21, "*"));
     Assert.AreEqual(0.4, re.calculate(2, 5, "/"));
 }
Пример #2
0
 public void isOperatorTest()
 {
     RpnExpression re = new RpnExpression();
     // Only +, -, *, / available
     Assert.IsTrue(re.isOperator("+"));
     Assert.IsTrue(re.isOperator("-"));
     Assert.IsTrue(re.isOperator("*"));
     Assert.IsTrue(re.isOperator("/"));
     // Any others unavailable
     Assert.IsFalse(re.isOperator("("));
     Assert.IsFalse(re.isOperator(")"));
 }
Пример #3
0
        static void Main(string[] args)
        {
            String handle = "1";
            do
            {
                try
                {
                    Console.Write("Please select the decimal delimiter type, [1]comma, [2]point\r\n\r\n");

                    char dl = '1';
                    while (dl.Equals('1'))
                    {
                        String spliter = Console.ReadLine();
                        if (spliter.Equals("1"))
                        {
                            dl = ',';
                        }
                        else if (spliter.Equals("2"))
                        {
                            dl = '.';
                        }
                        else
                        {
                            Console.Write("Please input correct option, [1]comma, [2]point\r\n\r\n");
                        }
                    }

                    Console.Write("Please input the RPN array, use space to split the digital & operator arrays ...\r\n\r\n");
                    String rpnArray = Console.ReadLine();
                    Console.WriteLine("Your proposed Reverse Polish Notation is: '" + rpnArray + "'\r\n");
                    RpnExpression rpnEx = new RpnExpression(rpnArray, dl);
                    //Console.WriteLine(rpnEx.IsInputValid);
                    double result = rpnEx.calculate();
                    String re = result.ToString();
                    if (dl.Equals(','))
                    {
                        re = result.ToString().Replace(".", ",");
                    }
                    Console.WriteLine("The calculated value of the RPN expression is: '" + re + "'\r\n\r\n");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                Console.WriteLine("Please select next step, [1]Continue, [Others]Close & Exit\r\n");
                handle = Console.ReadLine();
            }
            while (handle.Equals("1"));
        }
Пример #4
0
        static void RealWorlExampleMain()
        {
            Console.WriteLine("Real world example");

            Console.WriteLine("Enter RPN expression:");
            Evaluator     evaluator            = new Evaluator();
            string        enteredRpnExpression = Console.ReadLine();
            RpnExpression rpnExpression        = evaluator.Parse(enteredRpnExpression);
            int           result = rpnExpression.Interpret();

            Console.WriteLine("Result: " + result);

            Console.Read();
        }
Пример #5
0
 public void isDoubleTest()
 {
     RpnExpression re = new RpnExpression();
     // Integer is ok to both ',' and '.'
     Assert.IsTrue(re.isDouble("-1", '.'));
     Assert.IsTrue(re.isDouble("100", '.'));
     // Double need to judge if ',' or '.'
     Assert.IsTrue(re.isDouble("0.01", '.'));
     Assert.IsFalse(re.isDouble("0.01", ','));
     Assert.IsTrue(re.isDouble("0,01", ','));
     Assert.IsFalse(re.isDouble("0,01", '.'));
     // Operator should false for both ',' and '.'
     Assert.IsFalse(re.isDouble("/", ','));
     Assert.IsFalse(re.isDouble("/", '.'));
 }
Пример #6
0
 public void calculateTest1()
 {
     // Integer
     RpnExpression re = new RpnExpression("100  200  300  -  +", '.');
     Assert.AreEqual(0, re.calculate());
     re = new RpnExpression("100  20  200  -  30  /  50  *  +", '.');
     Assert.AreEqual(-200, re.calculate());
     re = new RpnExpression("100  200  300  -  +", ',');
     Assert.AreEqual(0, re.calculate());
     re = new RpnExpression("100  20  200  -  30  /  50  *  +", ',');
     Assert.AreEqual(-200, re.calculate());
     // Double
     re = new RpnExpression("10,2  12,5  10  -  0,5  /  50  *  +", ',');
     Assert.AreEqual(260.2, re.calculate());
     re = new RpnExpression("10.2  12.5  10  -  0.5  /  50  *  +", '.');
     Assert.AreEqual(260.2, re.calculate());
 }
Пример #7
0
        /// <summary>
        /// Sequential call of methods to transform and calculate the result of an expression.
        /// </summary>
        /// <param name="expression">Expression to evaluate.</param>
        private void ParseExpression(string expression)
        {
            var mathExpression = new MathExpression(expression);

            if (!mathExpression.IsValid)
            {
                return;
            }

            var listOfElements          = new ListOfElements(mathExpression);
            var rpnExpression           = new RpnExpression(listOfElements);
            var rpnCalculationAlgorithm = new RpnCalculationAlgorithm(rpnExpression);

            if (rpnCalculationAlgorithm.IsValid)
            {
                _result = rpnCalculationAlgorithm.Result;
            }
        }
Пример #8
0
 public void validInputTest()
 {
     RpnExpression re = new RpnExpression();
     // Space array is ok
     Assert.IsTrue(re.validInput("-1    0.01 +", '.'));
     Assert.IsTrue(re.validInput("-1    0,01 +", ','));
     Assert.IsTrue(re.validInput("2,5 5 /", ','));
     Assert.IsTrue(re.validInput("2.5 5 /", '.'));
     // Numbers of operator and digi is not different with 1
     Assert.IsFalse(re.validInput("-1  200   0,01 +", ','));
     Assert.IsTrue(re.validInput("-1  200   0,01 + -", ','));
     // Incorrect & Correct operator
     Assert.IsFalse(re.validInput("-1  200   0,01 + %", ','));
     Assert.IsTrue(re.validInput("-1  200   0,01 + *", ','));
     // Operator incorrect position
     Assert.IsFalse(re.validInput(" - -1  200   0,01 + ", ','));
     Assert.IsFalse(re.validInput("-1  -  200   0,01  *", ','));
     Assert.IsTrue(re.validInput(" -1  200   0,01 / + ", ','));
     Assert.IsTrue(re.validInput("-1     200   0,01  * /", ','));
 }