예제 #1
0
        /// <summary>
        /// Runs the thing.
        /// </summary>
        internal static void Run()
        {
            var five = new BinaryAddOperation(2, 3);
            Console.WriteLine(five.Operand() + " 5");
            var ten = new BinaryMultiplyOperation(five, 2);
            Console.WriteLine(ten.Operand() + " 10");

            var negativeFour = new UnarySubtractOperation(4);
            Console.WriteLine(negativeFour.Operand() + " -4");
            var four = new UnarySubtractOperation(negativeFour);
            Console.WriteLine(four.Operand() + " 4");

            Console.WriteLine();
            Console.WriteLine(new BinaryAddOperation(3, new BinaryMultiplyOperation(4, 5)).Operand() + " 23");
            Console.WriteLine(new BinaryMultiplyOperation(new BinaryAddOperation(2, 3), 4).Operand() + " 20");

            Console.WriteLine();

            // 2*3+(4-2)*2
            Console.WriteLine(new BinaryAddOperation(new BinaryMultiplyOperation(2, 3), new BinaryMultiplyOperation(new BinarySubtractOperation(4, 2), 2)).Operand() + " 10");

            // 2*(2*2)*2
            Console.WriteLine(new BinaryMultiplyOperation(new BinaryMultiplyOperation(2, new BinaryMultiplyOperation(2, 2)), 2).Operand() + " 16");

            // 1+(2+(3+4)+5)+6
            Console.WriteLine(new BinaryAddOperation(new BinaryAddOperation(1, new BinaryAddOperation(new BinaryAddOperation(2, new BinaryAddOperation(3, 4)), 5)), 6).Operand() + " 21");

            // 1+(2-(3+4)+5)+6
            Console.WriteLine(new BinaryAddOperation(new BinaryAddOperation(1, new BinaryAddOperation(new BinarySubtractOperation(2, new BinaryAddOperation(3, 4)), 5)), 6).Operand() + " 7");

            // 1+(2+(3+4)-5)+6
            Console.WriteLine(new BinaryAddOperation(new BinaryAddOperation(1, new BinarySubtractOperation(new BinaryAddOperation(2, new BinaryAddOperation(3, 4)), 5)), 6).Operand() + " 11");

            // ABCDEFGHIJKLMOP
            // 1+(2+(3+4)*5)+6
            //                    O                      B                  A      E                  D      K                           H                  G  I   L           P
            Console.WriteLine(new BinaryAddOperation(new BinaryAddOperation(1, new BinaryAddOperation(2, new BinaryMultiplyOperation(new BinaryAddOperation(3, 4), 5))), 6).Operand() + " 44");

            // ABCDEFGHIJKLMOP
            // 1+(2+(3+4)*5)+6
            //                    B                  A      O                      E                  D      K                           H                  G  I   L           P
            Console.WriteLine(new BinaryAddOperation(1, new BinaryAddOperation(new BinaryAddOperation(2, new BinaryMultiplyOperation(new BinaryAddOperation(3, 4), 5)), 6)).Operand() + " 44");

            Console.WriteLine();
            Console.WriteLine(Evaluator.Evaluate("2+3") + " 5");
            Console.WriteLine(Evaluator.Evaluate("2+3+5") + " 10");
            Console.WriteLine(Evaluator.Evaluate("2+3+5+10+10") + " 30");

            Console.WriteLine();
            foreach (var token in Tokenize("2+3"))
            {
                Console.WriteLine(token);
            }
        }
예제 #2
0
        /// <summary>
        /// Evaluates the <paramref name="expr" /> into a <see cref="Double" /> value.
        /// </summary>
        /// <param name="expr">The <see cref="String" /> to evaluate.</param>
        /// <returns>The <paramref name="expr" /> as a <see cref="Double" />.</returns>
        public static double? Evaluate(string expr)
        {
            var multi = expr.Split('+').Select(Convert.ToDouble).ToList();

            Operation operation = new BinaryAddOperation(multi[0], multi[1]);
            for (var i = 2; i < multi.Count; i++)
            {
                operation = operation.Add(new UnaryAddOperation(multi[i]));
            }

            return Convert.ToDouble(operation.Operand().ToString());
        }