예제 #1
0
        private List <CalculatorNumber> CalculateByOperators(List <CalculatorNumber> numbers, Operator[] operators)
        {
            List <CalculatorNumber> results = new List <CalculatorNumber>
            {
                new CalculatorNumber()
            };

            foreach (CalculatorNumber number in numbers)
            {
                CalculatorNumber result          = results[results.Count - 1];
                bool             isOperatorFound = false;

                foreach (Operator @operator in operators)
                {
                    if (number.Operator == @operator)
                    {
                        result.Value    = OperatorCalculate(result.Value, result.Operator, number.Value);
                        result.Operator = number.Operator;
                        isOperatorFound = true;
                    }
                }

                if (!isOperatorFound)
                {
                    result.Value    = OperatorCalculate(result.Value, result.Operator, number.Value);
                    result.Operator = number.Operator;
                    results.Add(new CalculatorNumber());
                }
            }

            return(results);
        }
예제 #2
0
 private void BeginCalculating()
 {
     try
     {
         List <CalculatorNumber> numbersRaw        = GetNumbers(calculation);
         List <CalculatorNumber> numbersAfterRAndP = CalculateRootsAndPowers(numbersRaw);
         List <CalculatorNumber> numbersAfterTAndD = CalculateTimesAndDivide(numbersAfterRAndP);
         CalculatorNumber        result            = CalculatePlusAndMinus(numbersAfterTAndD);
         Result = result.Value.ToString();
     }
     catch (Exception exception)
     {
         throw exception;
     }
 }
예제 #3
0
        private List <CalculatorNumber> GetNumbers(string calculation)
        {
            List <CalculatorNumber> numbers = new List <CalculatorNumber>();
            CalculatorNumber        number  = new CalculatorNumber();
            string numberText = "";

            try
            {
                for (int i = 0; i < calculation.Length; i++)
                {
                    if (calculation[i] == ' ')
                    {
                        continue;
                    }
                    else if (CheckNumber(calculation[i]))
                    {
                        numberText += calculation[i];
                    }
                    else if (CheckOperator(calculation[i]))
                    {
                        number.Value    = double.Parse(numberText);
                        number.Operator = (Operator)calculation[i];
                        numbers.Add(number);

                        number     = new CalculatorNumber();
                        numberText = "";
                    }
                }

                if (numberText != "")
                {
                    number.Value    = double.Parse(numberText);
                    number.Operator = Operator.Asign;
                    numbers.Add(number);
                }
            }
            catch
            {
                throw exceptionCalculationError;
            }

            return(numbers);
        }