Exemplo n.º 1
0
        public InputPart(string input)
        {
            // Check if it is a number
            if (int.TryParse(input, out var intOutput))
            {
                Type      = InputTypes.Value;
                _intValue = intOutput;
                return;
            }

            // Check if it is a binary operator
            if (InputTypeHelper.IsOperator(input, out var typeOutput))
            {
                Type = typeOutput;
            }
        }
Exemplo 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));
            }
        }