public IOperation CreateOperation(OperationTypes operationType)
        {
            IOperation newOperation = null;

            switch (operationType)
            {
            case OperationTypes.Addition:
                newOperation = new AdditionOperation();
                break;

            case OperationTypes.Subtraction:
                newOperation = new SubtractionOperation();
                break;

            case OperationTypes.Multiplication:
                newOperation = new MultiplicationOperation();
                break;

            case OperationTypes.Division:
                newOperation = new DivisionOperation();
                break;

            case OperationTypes.Modulus:
                newOperation = new ModulusOperation();
                break;

            case OperationTypes.Undefined:
                newOperation = new NullOperation();
                break;
            }

            return(newOperation);
        }
예제 #2
0
        public void AdditionFractionWithDifferenceDenominatorsReturnsResultedFraction()
        {
            var firstFraction     = new Fraction("1/2");
            var secondFraction    = new Fraction("1/3");
            var additionOperation = new AdditionOperation();

            var resultFraction = additionOperation.Execute(firstFraction, secondFraction);

            Assert.Equal(5, resultFraction.Numerator);
            Assert.Equal(6, resultFraction.Denominator);
        }
        public void operationhandler()
        {
            IArithmaticOperation addition       = new AdditionOperation();
            IArithmaticOperation subtraction    = new SubtractionOperation();
            IArithmaticOperation multiplication = new MultiplicationOperation();
            IArithmaticOperation division       = new DivisionOperation();

            addition.setNext(subtraction);
            subtraction.setNext(multiplication);
            multiplication.setNext(division);

            Request request = new Request(_num1, _num2);

            addition.Operation(request);
        }
예제 #4
0
        static void Main(string[] args)
        {
            var gen     = new StringSearchGenerator();
            var options = new StringSearchGeneratorOptions()
            {
                Width      = 10,
                Height     = 10,
                Directions = new List <CellDirection>()
                {
                    CellDirection.Right, CellDirection.Down
                },
                Title            = "Test Math Search",
                FillerCharacters = Defaults.Numbers,
            };

            for (int i = 0; i < 100; i++)
            {
                var probs = new List <IMathOperation>()
                {
                    MultiplicationOperation.NewRandom(25, 100),
                    AdditionOperation.NewRandom(100, 100000),
                    SubtractionOperation.NewRandomNonNegative(1, 100),
                    DivisionOperation.NewRandomIntegerResult(10, 100)
                };

                var prob = probs.GetRandom();

                if (prob.ValueDisplay.Length > 1)
                {
                    options.ItemsToPlace.Add(new MathProblemPuzzleEntry(prob));
                }
            }

            var puzzle = gen.Generate(options);

            Console.WriteLine(puzzle.Title);
            Console.WriteLine();
            Console.WriteLine(puzzle.Grid.ToString());
            Console.WriteLine();
            puzzle.Items.ForEach(i => Console.WriteLine(i.DisplayValue));
            Console.WriteLine();

            Console.ReadLine();
        }
예제 #5
0
        private IEnumerable <IPuzzleEntry> RandomMathProblemsForPuzzle()
        {
            while (true)
            {
                var probs = new List <IMathOperation>()
                {
                    MultiplicationOperation.NewRandom(25, 100),
                    AdditionOperation.NewRandom(100, 100000),
                    SubtractionOperation.NewRandomNonNegative(1, 100),
                    DivisionOperation.NewRandomIntegerResult(10, 100)
                };

                var prob = probs.GetRandom();

                if (prob.ValueDisplay.Length > 1)
                {
                    yield return(new MathProblemPuzzleEntry(prob));
                }
            }
        }
        public EquationTermBase Invoke(EquationTermBase term)
        {
            var operation = (EquationBinaryOperationBase)term;

            AdditionOperation result;

            if (MatchShiftRight(operation))
            {
                var leftOperation = (EquationBinaryOperationBase)operation.Left;

                result = new AdditionOperation(leftOperation.Right, new AdditionOperation(leftOperation.Left, operation.Right));
            }
            else
            {
                var rightOperation = (EquationBinaryOperationBase)operation.Right;
                result = new AdditionOperation(rightOperation.Right, new AdditionOperation(rightOperation.Left, operation.Left));
            }

            return(result);
        }
예제 #7
0
        static void Main(string[] args)
        {
            var        parser    = new InputStringParser();
            IOperation operation = new AdditionOperation();;
            var        argValues = CommandLine.Parser.Default.ParseArguments <CommandLineArgumentOptions>(args);

            if (!argValues.Errors.Any())
            {
                // Values are available here
                if (!string.IsNullOrEmpty(argValues.Value.AlternateDelimiter))
                {
                    parser.Settings.Delimiters.Add(argValues.Value.AlternateDelimiter);
                }
                parser.Settings.AllowNegativeValues = argValues.Value.AllowNegativeValues;
                parser.Settings.MaximumValue        = argValues.Value.MaximumValue;
                if (argValues.Value.Operation == Operations.Add)
                {
                    operation = new AdditionOperation();
                }
                else if (argValues.Value.Operation == Operations.Subtract)
                {
                    operation = new SubtractionOperation();
                }
                else if (argValues.Value.Operation == Operations.Multiply)
                {
                    operation = new MultiplicationOperation();
                }
                else if (argValues.Value.Operation == Operations.Divide)
                {
                    operation = new DivisionOperation();
                }
            }
            else
            {
                return;
            }

            var calculator = new StringCalculator(parser);

            Console.WriteLine(calculator.PromptString);
            Console.WriteLine("Multiline strings may be entered. Input an empty line to terminate the input.  " +
                              "The application will repeat execution until the console is closed or terminated with 'Ctrl+C'");
            while (true)
            {
                try
                {
                    string line;
                    string input = string.Empty;
                    while (!String.IsNullOrWhiteSpace(line = Console.ReadLine()))
                    {
                        input += line + "\n";
                    }
                    var result = calculator.Calculate(input.Trim(), operation).NumberSentence;
                    Console.WriteLine($"Result: {result}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error Calculating Value.  Error details: {ex.Message}");
                }
                Console.WriteLine("\n");
            }
        }