Ejemplo n.º 1
0
        public static void Main()
        {
            string input      = Console.ReadLine();
            var    calculator = new PrimitiveCalculator();

            while (input != "End")
            {
                string[] inputArgs = input.Split();

                if (inputArgs[0] == "mode")
                {
                    switch (inputArgs[1])
                    {
                    case "/":
                        IStrategy division = new DevisionStrategy();
                        calculator.ChangeStrategy(division);
                        break;

                    case "*":
                        IStrategy multiply = new MultipllyStrategy();
                        calculator.ChangeStrategy(multiply);
                        break;

                    case "-":
                        IStrategy substract = new SubtractionStrategy();
                        calculator.ChangeStrategy(substract);
                        break;

                    case "+":
                        IStrategy adding = new AdditionStrategy();
                        calculator.ChangeStrategy(adding);
                        break;

                    default:
                        throw new ArgumentException();
                    }
                }
                else
                {
                    int firstOperant  = int.Parse(inputArgs[0]);
                    int secondOperant = int.Parse(inputArgs[1]);
                    var result        = calculator.PerformCalculation(firstOperant, secondOperant);
                    Console.WriteLine(result);
                }

                input = Console.ReadLine();
            }
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            var input      = Console.ReadLine();
            var calculator = new PrimitiveCalculator();

            while (input != "End")
            {
                string[] inputArgs = input.Split();
                if (inputArgs[0] == "mode")
                {
                    switch (inputArgs[1][0])
                    {
                    case '/':
                        var devision = new DevisionStrategy();
                        calculator.ChangeStrategy(devision);
                        break;

                    case '*':
                        var multyplication = new MultyplicationStrategy();
                        calculator.ChangeStrategy(multyplication);
                        break;

                    case '-':
                        var substraction = new SubstractionStrategy();
                        calculator.ChangeStrategy(substraction);
                        break;

                    case '+':
                        var addition = new AdditionStrategy();
                        calculator.ChangeStrategy(addition);
                        break;
                    }
                }
                else
                {
                    int firstOperand  = int.Parse(inputArgs[0]);
                    int secondOperand = int.Parse(inputArgs[1]);

                    int result = calculator.PerformCalculation(firstOperand, secondOperand);

                    Console.WriteLine(result);
                }

                input = Console.ReadLine();
            }
        }