Пример #1
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();
            }
        }
Пример #2
0
        /// <summary>
        /// Send a power operation to the LCD device to affect the device itself or the host if it
        /// is configured to do so.
        /// </summary>
        /// <param name="op">Currently the only supported operation is RebootLcd.</param>
        public void SendPowerOperation(PowerOperation op)
        {
            ThrowIfNotConnected();
            switch (op)
            {
            case PowerOperation.RebootLcd:
                var data     = new byte[] { 0x08, 0x12, 0x63 };
                var command  = new CommandPacket(CommandType.SendPowerOperation, (byte)data.Length, data);
                var response = _deviceConnection?.SendReceive(command);
                VerifyResponsePacket(response, CommandType.SendPowerOperation);
                break;

            case PowerOperation.ResetHost:
                throw new NotImplementedException();

            case PowerOperation.PowerOffHost:
                throw new NotImplementedException();

            default:
                throw new NotImplementedException();
            }
        }
Пример #3
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);
            }
        }