Esempio n. 1
0
        public static IStrategy FindStrategy(char @operator)
        {
            IStrategy strategyToChange = null;

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

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

            case '*':
                strategyToChange = new MultiplicationStrategy();
                break;

            case '/':
                strategyToChange = new DivisionStrategy();
                break;

            default:
                throw new ArgumentException($"Invalid operator {@operator} !");
            }

            return(strategyToChange);
        }
 public PrimitiveCalculator()
 {
     this.additionStrategy       = new AdditionStrategy();
     this.subtractionStrategy    = new SubtractionStrategy();
     this.multiplicationStrategy = new MultiplicationStrategy();
     this.divisionStrategy       = new DivisionStrategy();
     this.isAddition             = true;
 }
Esempio n. 3
0
        static void Main()                                                                    // 100/100 - Пример за DependencyInversion с ctor Injection!!!
        {
            PrimitiveCalculator calculator = new PrimitiveCalculator(new AdditionStrategy()); // default strategy!

            string input;

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

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

                    ICalculationStrategy strategy = null;

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

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

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

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

                    if (strategy == null)
                    {
                        throw new ArgumentException("Inavalid mode!");
                    }

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

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

                    Console.WriteLine(result);
                }
            }
        }
        static void Main(string[] args)
        {
            var calculator = new PrimitiveCalculator();

            while (true)
            {
                var input = Console.ReadLine();

                if (input == "End")
                {
                    break;
                }

                if (char.IsDigit(input[0]) || input[0] == '-')
                {
                    var numbers = input.Split().Select(int.Parse).ToArray();

                    var firstOperand  = numbers[0];
                    var secondOperand = numbers[1];

                    Console.WriteLine(calculator.PerformCalculation(firstOperand, secondOperand));
                }
                else
                {
                    var operation = input.Split();

                    var @operator = operation[1];

                    IStrategy strategy = null;

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

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

                    case "*":
                        strategy = new MultiplyStrategy();
                        break;

                    case "/":
                        strategy = new DivideStrategy();
                        break;
                    }

                    calculator.ChangeStrategy(strategy);
                }
            }
        }