Beispiel #1
0
        public void Run()
        {
            string command;

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

                if (data[0] == "mode")
                {
                    char calculationType          = data[1][0];
                    ICalculationStrategy strategy = null;

                    switch (calculationType)
                    {
                    case '-':
                        strategy = new SubtractionStrategy();
                        break;

                    case '+':
                        strategy = new AdditionStrategy();
                        break;

                    case '*':
                        strategy = new MultiplyingStrategy();
                        break;

                    case '/':
                        strategy = new DivisionStrategy();
                        break;
                    }

                    this.calculator.ChangeStrategy(strategy);
                }
                else
                {
                    int firstOperand  = int.Parse(data[0]);
                    int secondOperand = int.Parse(data[1]);

                    int result = calculator.PerformCalculation(firstOperand, secondOperand);
                    Console.WriteLine(result);
                }
            }
        }
Beispiel #2
0
        public void Run()
        {
            string input;

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

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

                    IStrategy strategy = null;

                    switch (@operator)
                    {
                    case '+':
                        strategy = new AdditionStrategy();
                        break;

                    case '-':
                        strategy = new SubtractionStrategy();
                        break;

                    case '*':
                        strategy = new MultiplyingStrategy();
                        break;

                    case '/':
                        strategy = new DividingStrategy();
                        break;
                    }

                    this.calculator.ChangeStrategy(strategy);
                }
                else
                {
                    int firstOperand  = int.Parse(inputTokens[0]);
                    int secondOperand = int.Parse(inputTokens[1]);
                    Console.WriteLine(this.calculator.PerformCalculation(firstOperand, secondOperand));
                }
            }
        }