示例#1
0
        public void Solutions_DivideOperator_Test()
        {
            //Try to represent the equation ((5+1) x 3) - 4
            IEquation node1     = new AddOperation(new Integer(5), new Integer(1));
            IEquation node2     = new MultiplyOperation(node1, new Integer(3));
            IEquation equation1 = new MinusOperation(node2, new Integer(4));

            //Try to represent the equation 1 + 2  +3
            IEquation node3     = new AddOperation(new Integer(1), new Integer(2));
            IEquation equation2 = new AddOperation(node3, new Integer(3));

            Solutions solutions = new Solutions();

            solutions.Add(equation1);
            solutions.Add(equation2);

            IEquation equation3 = new Integer(14);

            Solutions new_solutions = solutions / equation3;

            Assert.AreEqual(2, new_solutions.Count);
            Assert.AreEqual(1, new_solutions[0].Value);
            Assert.AreEqual((decimal)6 / (decimal)14, new_solutions[1].Value);

            new_solutions = solutions / new Integer(3);
            Assert.AreEqual(2, new_solutions.Count);
            Assert.AreEqual((decimal)14 / (decimal)3, new_solutions[0].Value);
            Assert.AreEqual(2, new_solutions[1].Value);
        }
示例#2
0
        public void MinusOperation_Test()
        {
            MinusOperation op = new MinusOperation(new Integer(2), new Integer(4));

            Assert.AreEqual(-2, op.Value);
            Assert.AreEqual("2 - 4", op.ToString());
        }
示例#3
0
        public void Equation_Test1()
        {
            //Try to represent the equation ((5+1) x 3) - 4
            IEquation node1 = new AddOperation(new Integer(5), new Integer(1));

            Assert.AreEqual(6, node1.Value);

            MultiplyOperation node2 = new MultiplyOperation(node1, new Integer(3));

            Assert.AreEqual(18, node2.Value);

            MinusOperation equation = new MinusOperation(node2, new Integer(4));

            Assert.AreEqual(14, equation.Value);
            Assert.AreEqual("((5 + 1) x 3) - 4", equation.ToString());
        }
示例#4
0
        private void startTheQuiz()
        {
            // Create object to corresponding operation
            sumOperation      = new SumOperation(50, 50, sum);
            minusOperation    = new MinusOperation(50, 50, subtraction);
            divideOperation   = new DivideOperation(50, 50, division);
            multiplyOperation = new MultiplyOperation(50, 50, multiplication);

            // Change labels with question mark to value
            setLabels(plusLeftLabel, plusRightLabel, sumOperation);
            setLabels(minusLeftLabel, minusRightLabel, minusOperation);
            setLabels(divideLeftLabel, divieRightLabel, divideOperation);
            setLabels(timesLeftLabel, timesRightLabel, multiplyOperation);

            // Start the timer
            timeLeft       = 30;
            timeLabel.Text = "30 seconds";
            timer.Start();
        }
示例#5
0
        public void PerformOperation(OperationType operation)
        {
            //int operation;
            IOperation operationType = null;

            switch (operation)
            {
            case OperationType.Add:
            {
                operationType = new AddOperation();
                break;
            }

            case OperationType.Minus:
            {
                operationType = new MinusOperation();
                break;
            }

            case OperationType.Multiply:
            {
                operationType = new MultiplyOperation();
                break;
            }

            case OperationType.Divide:
            {
                operationType = new DivideOperation();
                break;
            }

            case OperationType.Negate:
            {
                operationType = new NegateOperation();
                break;
            }
            }
            if (operationType != null)
            {
                operationType.Perform(numbers);
            }
        }
        static void Main14()
        {
            Console.WriteLine("Hello Interpreter Pattern!");

            var context = new Context();

            var a = new TerminalExpression("a");
            var b = new TerminalExpression("b");
            var c = new TerminalExpression("c");

            context.Add(a, 10);
            context.Add(b, 15);
            context.Add(c, 5);


            int result = new MinusOperation(new PlusOperation(a, b), c).Interpreter(context);

            Console.WriteLine($"a = {a.Interpreter(context)}");
            Console.WriteLine($"b = {b.Interpreter(context)}");
            Console.WriteLine($"c = {c.Interpreter(context)}");
            Console.WriteLine($"a + b - c = {result}");

            Console.ReadLine();
        }
示例#7
0
        public static Operation CreateOperation(string operate)
        {
            Operation operation = null;

            switch (operate)
            {
            case "+":
                operation = new AddOperation();
                break;

            case "-":
                operation = new MinusOperation();
                break;;

            case "*":
                operation = new MultiplyOperation();
                break;

            case "/":
                operation = new DivideOperation();
                break;
            }
            return(operation);
        }
示例#8
0
文件: Calculator.cs 项目: jbvera/WOS
        /// <summary>
        /// Calls the IOperation that corresponds to the input OperationType
        /// </summary>
        /// <param name="operationType">The operation to perform.</param>
        public void PerformOperation(OperationType operationType)
        {
            IOperation operation = null;

            switch (operationType)
            {
            case OperationType.Add:
                operation = new AddOperation();
                break;

            case OperationType.Minus:
                operation = new MinusOperation();
                break;

            case OperationType.Multiply:
                operation = new MultiplyOperation();
                break;

            case OperationType.Divide:
                operation = new DivideOperation();
                break;

            case OperationType.Negate:
                operation = new NegateOperation();
                break;

            case OperationType.SquareRoot:
                operation = new SquareRootOperation();
                break;

            case OperationType.Exponential:
                operation = new ExponentialOperation();
                break;

            case OperationType.Power:
                operation = new PowerOperation();
                break;

            case OperationType.Reciprocal:
                operation = new ReciprocalOperation();
                break;

            case OperationType.Sine:
                operation = new SineOperation();
                break;

            case OperationType.Cosine:
                operation = new CosineOperation();
                break;

            case OperationType.Clear:
                operation = new ClearOperation();
                break;

            case OperationType.Swap:
                operation = new SwapOperation();
                break;

            case OperationType.Rotate:
                operation = new RotateOperation();
                break;
            }
            if (operation != null)
            {
                /*
                 * Exception handling is done within each class that implements IOperation.
                 * Refactoring to multiple operation types (e.g. BinaryOperation,
                 * UnaryOperation, and StackOperation) can make the code DRYer as each type
                 * can use similar exception handling.
                 */
                operation.Perform(stack);
            }
        }