public static void Main()
        {
            var calculator = new PrimitiveCalculator(new AdditionStrategy());

            var input = Console.ReadLine();

            while (input != "End")
            {
                var tokens = input.Split();
                if (tokens[0] == "mode")
                {
                    switch (tokens[1])
                    {
                    case "+":
                        calculator.changeStrategy(new AdditionStrategy());
                        break;

                    case "-":
                        calculator.changeStrategy(new SubtractionStrategy());
                        break;

                    case "*":
                        calculator.changeStrategy(new MultiplyStrategy());
                        break;

                    case "/":
                        calculator.changeStrategy(new DivideStrategy());
                        break;
                    }
                }
                else
                {
                    var firstOperand  = int.Parse(tokens[0]);
                    var secondOperand = int.Parse(tokens[1]);
                    var result        = 0;

                    try
                    {
                        result = calculator.performCalculation(firstOperand, secondOperand);
                        Console.WriteLine(result);
                    }
                    catch (ArgumentException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

                input = Console.ReadLine();
            }
        }
Example #2
0
        static void Main()
        {
            var calculator = new PrimitiveCalculator();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] data = input.Split("\t ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                if (data[0] == "mode")
                {
                    char @operator = data[1][0];

                    calculator.changeStrategy(@operator);
                }
                else
                {
                    int number1 = int.Parse(data[0]);
                    int number2 = int.Parse(data[1]);

                    Console.WriteLine(calculator.performCalculation(number1, number2));
                }
            }
        }
        static void Main(string[] args)
        {
            PrimitiveCalculator calculator = new PrimitiveCalculator();

            var input = Console.ReadLine();

            while (input != "End")
            {
                if (input.Contains("mode"))
                {
                    calculator.changeStrategy(input[input.Length - 1]);
                }
                else
                {
                    int[] operands = input
                                     .Split(' ')
                                     .Select(int.Parse)
                                     .ToArray();

                    Console.WriteLine(calculator.performCalculation(operands[0], operands[1]));
                }


                input = Console.ReadLine();
            }
        }
Example #4
0
        static void Main(string[] args)
        {
            PrimitiveCalculator primitiveCalculator = new PrimitiveCalculator(new AdditionStrategy());

            string input = string.Empty;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] commandArgs = input.Split();

                if (commandArgs[0] == "mode")
                {
                    char @operator = commandArgs[1][0];

                    switch (@operator)
                    {
                    case '+':
                        primitiveCalculator.changeStrategy(new AdditionStrategy());
                        break;

                    case '-':
                        primitiveCalculator.changeStrategy(new SubtractionStrategy());
                        break;

                    case '*':
                        primitiveCalculator.changeStrategy(new MultiplicationStrategy());
                        break;

                    case '/':
                        primitiveCalculator.changeStrategy(new DivisionStrategy());
                        break;
                    }
                }
                else
                {
                    int firstNumber  = int.Parse(commandArgs[0]);
                    int secondNumber = int.Parse(commandArgs[1]);

                    int result = primitiveCalculator.performCalculation(firstNumber, secondNumber);
                    Console.WriteLine(result);
                }
            }
        }