Ejemplo n.º 1
0
        public void Divide()
        {
            var expected = (double)10 / 3;
            var result   = new DivideOperation().Perform(10, 3);

            Assert.AreEqual(expected, result);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var input = Console.ReadLine();

            while (input != "exit")
            {
                var     op5 = new PowerOperation();
                var     op4 = new DivideOperation(op5);
                var     op3 = new MultiplyOperation(op4);
                var     op2 = new SubstractOperation(op3);
                var     op1 = new AddOperation(op2);
                Command command;
                try
                {
                    command = new Command(input);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }

                var result = op1.Calculate(command);
                Console.Write(result == null
                    ? $"Operation {command.Operation} is not supported"
                    : $"{command} = {result} \n");

                input = Console.ReadLine();
            }
        }
Ejemplo n.º 3
0
        public void DivedeOperation_GetTextOperation()
        {
            IOperation sut = new DivideOperation(1, 1);

            string text = sut.GetTextOperation();

            Assert.AreEqual("1 / 1", text);
        }
Ejemplo n.º 4
0
        public void DivedeOperation_ReturnTheRightResult()
        {
            IOperation sut = new DivideOperation(1, 1);

            decimal result = sut.Calculate();

            Assert.AreEqual(1, result);
        }
Ejemplo n.º 5
0
        public void Create_DivideOperation_ReturnInstance()
        {
            IOperation sut = new DivideOperation(3, 9);

            Assert.IsNotNull(sut);
            Assert.AreEqual(sut.FirstNumber, 3);
            Assert.AreEqual(sut.SecondNumber, 9);
        }
Ejemplo n.º 6
0
        public void DivideNumbers()
        {
            DivideOperation op = DivideOperation.Instance;

            Assert.AreEqual(1 / 2.0, op.Apply(1, 2));
            Assert.AreEqual(2, op.Apply(4, 2));
            Assert.AreEqual(1.2 / 3.4, op.Apply(1.2, 3.4));
            Assert.AreEqual(1 / 2.3, op.Apply(1, 2.3));
            Assert.AreEqual(1.2 / 3, op.Apply(1.2, 3));
        }
Ejemplo n.º 7
0
        public void DivideOperation_Test()
        {
            DivideOperation op = new DivideOperation(new Integer(4), new Integer(2));

            Assert.AreEqual(2, op.Value);
            Assert.AreEqual("4 / 2", op.ToString());

            op = new DivideOperation(new Integer(2), new Integer(4));
            Assert.AreEqual((decimal)0.5, op.Value);
            Assert.AreEqual("2 / 4", op.ToString());
        }
Ejemplo n.º 8
0
        public void DivideOperation_Calculate_Success()
        {
            var operation = new DivideOperation();
            var input     = new OperationInput <int>()
            {
                Num1 = 4, Num2 = 2
            };
            var output = operation.Do(input);

            Assert.IsNotNull(output);
            Assert.IsInstanceOfType(output, typeof(OperationOutput <int>));
            Assert.AreEqual(((OperationOutput <int>)output).Result, 2);
        }
Ejemplo n.º 9
0
        public void Equation_Test3()
        {
            //Try to represent the equation ((6 / 3) + 2) x 5
            IEquation node1 = new DivideOperation(new Integer(6), new Integer(3));

            Assert.AreEqual(2, node1.Value);

            IEquation node2 = new AddOperation(node1, new Integer(2));

            Assert.AreEqual(4, node2.Value);

            IEquation equation = new MultiplyOperation(node2, new Integer(5));

            Assert.AreEqual(20, equation.Value);
            Assert.AreEqual("((6 / 3) + 2) x 5", equation.ToString());
        }
Ejemplo n.º 10
0
        public void DivideRealToVector()
        {
            DivideOperation op = DivideOperation.Instance;

            Vector v = new Vector(new object[] { 1, 2, 3 });

            var result = op.Apply(1.5, v);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Vector));

            var vector = (Vector)result;

            Assert.AreEqual(3, vector.Length);
            Assert.AreEqual(1.5 / 1, vector[0]);
            Assert.AreEqual(1.5 / 2, vector[1]);
            Assert.AreEqual(1.5 / 3, vector[2]);
        }
Ejemplo n.º 11
0
        public void DivideVectorByInteger()
        {
            DivideOperation op = DivideOperation.Instance;

            Vector v = new Vector(new object[] { 1, 2, 3 });

            var result = op.Apply(v, 2);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Vector));

            var vector = (Vector)result;

            Assert.AreEqual(3, vector.Length);
            Assert.AreEqual(1 / 2.0, vector[0]);
            Assert.AreEqual(1, vector[1]);
            Assert.AreEqual(3 / 2.0, vector[2]);
        }
Ejemplo n.º 12
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();
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
 protected VisitResult VisitDivide(DivideOperation operation)
 {
     return(VisitBinary(Expression.Divide, operation));
 }
Ejemplo n.º 15
0
        /// <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);
            }
        }