public void TestSubractCommand()
        {
            var c            = new SubtractCommand(options);
            var actualResult = c.Execute(new long[] { 4, 1, 1 });

            Assert.Equal(2, actualResult);
        }
예제 #2
0
        private void subButton_Click(object sender, EventArgs e)
        {
            expressionLbl.Text += String.IsNullOrEmpty(displayTxt.Text) ? Constants.Subtract : displayTxt.Text + Constants.Subtract;
            var number = new Number
            {
                EnteredNumber = String.IsNullOrEmpty(displayTxt.Text) ? 0 : Convert.ToDouble(displayTxt.Text),
                IsCalculated  = false
            };
            SubtractCommand subCommand;

            //Deferred execution
            if (_bracketCount == 0 || _bracketCount % 2 == 0)
            {
                _numbers.Add(number);
                subCommand = new SubtractCommand(_result, _numbers);
                //Calculating
                _invoker.CalculatingOperations();
            }
            else if (_bracketCount > 0 && _bracketCount % 2 == 0)
            {
                _bracketNumbers.Add(number);
                subCommand = new SubtractCommand(_result, _bracketNumbers);
                _invoker.CalculatingOperations();
            }
            else
            {
                _bracketNumbers.Add(number);
                subCommand = new SubtractCommand(_result, _bracketNumbers);
            }
            //Adding current operation into sequence
            _invoker.IncludeOperations(subCommand);

            displayTxt.Clear();
        }
예제 #3
0
        public double Subtract(double first, double second)
        {
            var cmd = new SubtractCommand(first, second);

            _subtractHandler.Handle(cmd);

            return(_store.Result);
        }
예제 #4
0
 public SYMathCalculator(ISYReciever reciever)
 {
     _addCommand      = new AddCommand(reciever);
     _subtractCommand = new SubtractCommand(reciever);
     _multiplyCommand = new MultiplyCommand(reciever);
     _divCommand      = new DivCommand(reciever);
     _powCommand      = new PowCommand(reciever);
 }
예제 #5
0
        public ViewModel()
        {
            AddCommand      = new AddCommand(this);
            SubtractCommand = new SubtractCommand(this);
            MultiplyCommand = new MultiplyCommand(this);
            DivideCommand   = new DivideCommand(this);

            Results = new ObservableCollection <Result>();
        }
예제 #6
0
        public CalculatorViewModel(ICalculatorOperationsService operationsService = null)
        {
            Calculator         = new CalculatorModel();
            _operationsService = operationsService ?? new CalculatorOperationsService();

            AddCommand      = new AddCommand(this);
            SubtractCommand = new SubtractCommand(this);
            MultiplyCommand = new MultiplyCommand(this);
            DivideCommand   = new DivideCommand(this);
        }
예제 #7
0
        private void UserControl1_Load(object sender, EventArgs e)
        {
            Application.AddMessageFilter(this); //Function to check the events.

            calculator = new Calculator(20, 10);

            addCmd = new AddCommand(calculator);
            subCmd = new SubtractCommand(calculator);
            mulCmd = new MultiplyCommand(calculator);
            divCmd = new DividerCommand(calculator);
        }
예제 #8
0
        public void SubtractsNumberUsingCommandAndReturnsNegativeNumberWithPublicProperty(int number, int expected)
        {
            // Arrange
            var subtractCommand = new SubtractCommand(number);

            // Act
            subtractCommand.Do();
            var result = subtractCommand.Number;

            // Assert
            That(result, Is.EqualTo(expected));
        }
예제 #9
0
        static void Main(string[] args)
        {
            var receiver        = new Calculator(345, 9945);
            var addCommand      = new AddCommand(receiver);
            var subtractCommand = new SubtractCommand(receiver);
            var multiplyCommand = new MultiplyCommand(receiver);

            Console.WriteLine($"Add command result: {addCommand.Execute()}");
            Console.WriteLine($"Subtract command result: {subtractCommand.Execute()}");
            Console.WriteLine($"Multiply command result: {multiplyCommand.Execute()}");
            Console.ReadKey();
        }
예제 #10
0
        public void Should_Subtract_Value()
        {
            CalculatorInvoker calculatorInvolker = new CalculatorInvoker();

            var calculator = new Calculator(3, 4);

            var command = new SubtractCommand(calculator);

            var result = calculatorInvolker.ExecuteCommand(command);

            result.Should().Be(-1);
        }
예제 #11
0
        static void Main(string[] args)
        {
            User     user  = new User();
            ICommand _add1 = new AddCommand(2, 3);

            user.ExecuteCommand(_add1);
            ICommand _subtract1 = new SubtractCommand(3, 2);

            user.ExecuteCommand(_subtract1);
            user.Undo();
            user.Redo();
        }
예제 #12
0
        public void SubtractsNumberAndUndoesOperationResultingInBaseZeroValue(int number, int expected)
        {
            // Arrange
            var subtractCommand = new SubtractCommand(number);

            subtractCommand.Do();

            // Act
            subtractCommand.Undo();
            var result = subtractCommand.Number;

            // Assert
            That(result, Is.EqualTo(expected));
        }
예제 #13
0
        /// <summary>
        /// Creates selected command instance
        /// </summary>
        /// <param name="operation">Operation type</param>
        /// <param name="mathUnit">Math unit</param>
        /// <returns>Command instance</returns>
        public static Command Create(string operation, MathUnit mathUnit)
        {
            Command command = null;

            switch (operation)
            {
            case "+":
                command = new AdditionCommand(mathUnit);
                break;

            case "-":
                command = new SubtractCommand(mathUnit);
                break;

            case "*":
                command = new MultiplicationCommand(mathUnit);
                break;

            case "/":
                command = new DivisionCommand(mathUnit);
                break;

            case "pow":
                command = new PowCommand(mathUnit);
                break;

            case "extract":
                command = new ExtractCommand(mathUnit);
                break;

            case "round":
                command = new RoundCommand(mathUnit);
                break;

            case "exp":
                command = new ExpCommand(mathUnit);
                break;

            case "log":
                command = new LogCommand(mathUnit);
                break;
            }
            return(command);
        }
예제 #14
0
        private ICommand CommandFromString(string name, string[] arguments)
        {
            ICommand command = null;

            switch (name)
            {
            case "SET":
            {
                Register register = Registers[arguments[0]];
                int      value    = int.Parse(arguments[1]);

                command = new SetCommand(register, value);
                break;
            }

            case "ADD":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new AddCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "SUB":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new SubtractCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "MUL":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new MultiplyCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "DIV":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new DivideCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "OUT":
            {
                if (Registers.ContainsKey(arguments[0]))
                {
                    Register register = Registers[arguments[0]];
                    command = new OutCommand(register);
                }
                else
                {
                    command = new OutCommand(arguments[0]);
                }

                break;
            }

            case "HLT":
            {
                command = new HaltCommand();
                break;
            }

            case "JMP":
            {
                string label = arguments[0];

                command = new JumpCommand(label);
                break;
            }

            case "JIF":
            {
                Register      registerOne     = Registers[arguments[0]];
                Register      registerTwo     = Registers[arguments[2]];
                string        conditionString = arguments[1];
                JumpCondition jumpCondition   = JumpCondition.Undefined;
                switch (conditionString)
                {
                case "==":
                    jumpCondition = JumpCondition.Equal;
                    break;

                case "<":
                    jumpCondition = JumpCondition.LessThan;
                    break;

                case "<=":
                    jumpCondition = JumpCondition.LessThanOrEqual;
                    break;

                case ">":
                    jumpCondition = JumpCondition.GreaterThan;
                    break;

                case ">=":
                    jumpCondition = JumpCondition.GreaterThanOrEqual;
                    break;

                case "!=":
                    jumpCondition = JumpCondition.NotEqual;
                    break;
                }

                string label = arguments[3];

                command = new JumpIfCommand(registerOne, registerTwo, label, jumpCondition);
                break;
            }

            case "CPY":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];

                command = new CopyCommand(registerOne, registerTwo);
                break;
            }

            default:
                if (name == "str:")
                {
                    string stringKey   = arguments[0];
                    string stringValue = string.Empty;
                    for (int i = 1; i < arguments.Length; i++)
                    {
                        stringValue += arguments[i];
                        if (i < arguments.Length)
                        {
                            stringValue += " ";
                        }
                    }

                    Program.RegisterString(stringKey, stringValue);
                }
                else if (name.Length > 1 && name.EndsWith(":"))
                {
                    Program.MakeLabel(name.Substring(0, name.Length - 1));
                    break;
                }
                break;
            }

            return(command);
        }