Exemplo n.º 1
0
        internal void ApplyInput(ICalculatorInput calculatorinput)
        {
            if (!ValidateInput(calculatorinput))
            {
                return;
            }
            var input = InputHistory.LastOrDefault();

            if (input is CalculatorOperator && calculatorinput is CalculatorOperator)
            {
                InputHistory.RemoveAt(InputHistory.Count - 1);
                InputHistory.Add(calculatorinput);
                FireChange("Input");
                return;
            }

            if ((input == null || input is CalculatorOperator) && calculatorinput.Value == ".")
            {
                InputHistory.Add(InputButtons.FirstOrDefault(b => b.Value == "0"));
                InputHistory.Add(calculatorinput);
            }
            else
            {
                InputHistory.Add(calculatorinput);
            }
            CurrentInput = Input.Replace(" ", "").Split(OperatorButtons.Select(b => b.Value).ToArray(), StringSplitOptions.RemoveEmptyEntries).Where(s => !string.IsNullOrEmpty(s)).LastOrDefault();
            FireChange("Input");
            if (calculatorinput is CalculatorOperator)
            {
                CurrentOperator = calculatorinput as CalculatorOperator;
                Calculate();
            }
        }
Exemplo n.º 2
0
        private void Calculate()
        {
            var calculation = Input.Replace(" ", "");
            var items       = calculation.Split(OperatorButtons.Select(o => o.Value).ToArray(), StringSplitOptions.RemoveEmptyEntries);

            if (items.Count() < 2)
            {
                return;
            }
            List <CalculatorOperator> operators = new List <CalculatorOperator>();

            foreach (string s in calculation.Where(o => OperatorButtons.Select(b => b.Value).Contains(o.ToString())).Select(o => o.ToString()))
            {
                operators.Add(OperatorButtons.FirstOrDefault(o => o.Value == s) as CalculatorOperator);
            }
            int    opindex    = 0;
            string calcresult = null;

            foreach (var item in items)
            {
                if (calcresult == null)
                {
                    calcresult = item;
                    continue;
                }
                var op = operators[opindex];
                switch (op.OperatorType)
                {
                case OperatorTypes.Add:
                {
                    calcresult = (double.Parse(calcresult) + double.Parse(item)).ToString();
                } break;

                case OperatorTypes.Subtract:
                {
                    calcresult = (double.Parse(calcresult) - double.Parse(item)).ToString();
                } break;

                case OperatorTypes.Multiply:
                {
                    calcresult = (double.Parse(calcresult) * double.Parse(item)).ToString();
                } break;

                case OperatorTypes.Divide:
                {
                    calcresult = (double.Parse(calcresult) / double.Parse(item)).ToString();
                } break;
                }
                opindex++;
            }
            Result = Math.Round(decimal.Parse(calcresult), 2).ToString();
            if (CurrentOperator.OperatorType == OperatorTypes.Equals)
            {
                InputHistory.Clear();
                foreach (var c in Result)
                {
                    ApplyInput(InputButtons.FirstOrDefault(i => i.Value.ToString() == c.ToString()));
                }
            }
            FireChange("Input");
        }