Ejemplo n.º 1
0
        private decimal Solve()
        {
            if (_result == null)
            {
                TupleOperation lastOperation = new TupleOperation(OperationType.None);
                decimal        runningTotal  = 0;
                foreach (Tuple <decimal, TupleOperation> t in Equation)
                {
                    if (lastOperation.Operand == OperationType.None)
                    {
                        runningTotal = (decimal)t.Item1;
                    }
                    else
                    {
                        runningTotal = lastOperation.Calculate((decimal)runningTotal, (decimal)t.Item1);
                    }
                    lastOperation = t.Item2;
                }
                _result = runningTotal;
            }

            return((decimal)_result);
        }
Ejemplo n.º 2
0
 public BigInteger Calculate(BigInteger Value1, BigInteger Value2)
 {
     return(TupleOperation.Calculate(Value1, Value2, this.Operand));
 }
Ejemplo n.º 3
0
        private void BuildEquation()
        {
            if (_result != null)
            {
                return;
            }

            int termCount         = TermPool.Count;
            int increasingOpCount = IncreasingOperations.Count;
            int decreasingOpCount = DecreasingOperations.Count;

            int            counter          = 1;
            BigInteger     currentTerm      = 0;
            BigInteger     runningTotal     = 0;
            TupleOperation currentOperation = new TupleOperation();
            TupleOperation lastOperation    = new TupleOperation(OperationType.None);

            List <Tuple <BigInteger, TupleOperation> > result = new List <Tuple <BigInteger, TupleOperation> >();

            while (counter <= NumberOfOperations)
            {
                do
                {
                    currentTerm = TermPool[EquationArgs.Rand.Next(0, termCount)];
                }while (lastOperation.Operand == OperationType.Divide && currentTerm == 0);


                if (lastOperation.Operand == OperationType.None)
                {
                    runningTotal = currentTerm;
                }
                else
                {
                    runningTotal = lastOperation.Calculate((BigInteger)runningTotal, currentTerm);
                }


                if (counter == NumberOfOperations)
                {
                    currentOperation = new TupleOperation(OperationType.Equal);
                }
                else
                {
                    switch (runningTotal.CompareTo(TargetValue))
                    {
                    case -1:

                        currentOperation = IncreasingOperations.ElementAt(EquationArgs.Rand.Next(0, increasingOpCount));

                        break;

                    case 1:

                        currentOperation = DecreasingOperations.ElementAt(EquationArgs.Rand.Next(0, decreasingOpCount));

                        break;

                    case 0:
                        currentOperation = new TupleOperation(OperationType.Equal);
                        result.Add(new Tuple <BigInteger, TupleOperation>(currentTerm, currentOperation));
                        Equation = result;
                        _result  = runningTotal;

                        if (EquationArgs.TargetValuePredicate == ResultPredicate.IsDivisibleBy)
                        {
                            _isSolution = ((_result != 0) && (_result % TargetValue == 0));
                        }
                        else if (EquationArgs.TargetValuePredicate == ResultPredicate.IsEqualTo)
                        {
                            _isSolution = (_result == TargetValue);
                        }

                        return;
                    }



                    //
                }

                result.Add(new Tuple <BigInteger, TupleOperation>(currentTerm, currentOperation));

                lastOperation = currentOperation;
                counter++;
            }


            Equation = result;
            _result  = runningTotal;

            if (EquationArgs.TargetValuePredicate == ResultPredicate.IsDivisibleBy)
            {
                _isSolution = ((_result != 0) && (_result % TargetValue == 0));
            }
            else if (EquationArgs.TargetValuePredicate == ResultPredicate.IsEqualTo)
            {
                _isSolution = (_result == TargetValue);
            }
        }
Ejemplo n.º 4
0
 public decimal Calculate(decimal Value1, decimal Value2)
 {
     return(TupleOperation.Calculate(Value1, Value2, Operand));
 }