Ejemplo n.º 1
0
        public static InputPart[] Parse(string input)
        {
            var inputParts = input.Split(' ');
            var output     = new InputPart[inputParts.Length];

            for (var i = 0; i < inputParts.Length; i++)
            {
                // Parse each part into a InputPart object
                output[i] = new InputPart(inputParts[i]);
            }

            // TODO: Should we check for Invalid objects here
            if (output.Any(x => x.Type == InputTypes.Invalid))
            {
                Console.WriteLine("Invalid input");
                return(null);
            }

            return(output);
        }
Ejemplo n.º 2
0
        private static void EvaluateOperation(int operatorIndex, ref InputPart[] parts)
        {
            var inputType = parts[operatorIndex].Type;

            if (InputTypeHelper.IsOperator(inputType.ToString(), out var operatorType))
            {
                // Gets the values of the simplified operation
                var first = 0;

                if (operatorType != InputTypes.FLIP)
                {
                    // Type is flip so no need to check left of operator
                    first = parts[operatorIndex - 1].Value;
                }

                var second = parts[operatorIndex + 1].Value;

                var twoInputValues = true;
                var result         = 0;

                switch (inputType)
                {
                case InputTypes.AND:
                    result = first & second;
                    parts[operatorIndex - 1] = new InputPart(result);
                    break;

                case InputTypes.OR:
                    result = first | second;
                    parts[operatorIndex - 1] = new InputPart(result);
                    break;

                case InputTypes.XOR:
                    result = first ^ second;
                    parts[operatorIndex - 1] = new InputPart(result);
                    break;

                case InputTypes.RSHIFT:
                    result = first >> second;
                    parts[operatorIndex - 1] = new InputPart(result);
                    break;

                case InputTypes.LSHIFT:
                    result = first << second;
                    parts[operatorIndex - 1] = new InputPart(result);
                    break;

                case InputTypes.FLIP:
                    twoInputValues           = false;
                    result                   = ~second;
                    parts[operatorIndex + 1] = new InputPart(result);
                    break;

                default:
                    throw new NotImplementedException();
                }


                // Remove the redundant values
                parts = parts.RemoveAt(operatorIndex);

                if (twoInputValues)
                {
                    Output.ShowWorking(inputType, result, first, second);

                    // Index changes, so operatorIndex + 1, becomes operatorIndex again
                    parts = parts.RemoveAt(operatorIndex);
                }
                else
                {
                    Output.ShowWorking(inputType, result, second);
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(inputType));
            }
        }