コード例 #1
0
        private List <Tuple <decimal, TupleOperation> > GenerateRandomEquation()
        {
            List <Tuple <decimal, TupleOperation> > result = new List <Tuple <decimal, TupleOperation> >();

            int            counter     = 1;
            decimal        term        = 0;
            int            termCount   = TermPool.Count;
            int            opCount     = OperatorPool.Length;
            TupleOperation operation   = new TupleOperation();
            OperationType  lastOperand = OperationType.None;

            while (counter <= NumberOfOperations)
            {
                do
                {
                    term = Convert.ToDecimal(TermPool[EquationArgs.Rand.Next(0, termCount)]);
                }while (lastOperand == OperationType.Divide && term == 0);

                if (counter == NumberOfOperations)
                {
                    operation = new TupleOperation(OperationType.Equal);
                }
                else
                {
                    operation = new TupleOperation(OperatorPool.ElementAt(EquationArgs.Rand.Next(0, opCount)).ToString());
                }

                result.Add(new Tuple <decimal, TupleOperation>(term, operation));
                lastOperand = operation.Operand;
                counter++;
            }

            return(result);
        }
コード例 #2
0
        public void GenerateNewAndEvaluate(IEquationFinderArgs args)
        {
            _result      = null;
            _isSolution  = false;
            EquationArgs = args;

            IncreasingOperations = new List <TupleOperation>();
            DecreasingOperations = new List <TupleOperation>();

            foreach (char op in EquationArgs.OperatorPool)
            {
                TupleOperation tupleOperation = new TupleOperation(op);

                switch (tupleOperation.Operand)
                {
                case OperationType.Add:
                case OperationType.Multiply:
                case OperationType.Exponentiation:
                    IncreasingOperations.Add(tupleOperation);
                    break;

                case OperationType.Subtract:
                case OperationType.Divide:
                    DecreasingOperations.Add(tupleOperation);
                    break;
                }
            }

            BuildEquation();
        }
コード例 #3
0
        public override bool Equals(object obj)
        {
            TupleOperation other = obj as TupleOperation;

            if (other == null)
            {
                return(false);
            }
            return(this.Operand.Equals(other.Operand));
        }
コード例 #4
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);
        }
コード例 #5
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);
            }
        }
コード例 #6
0
 public BigInteger Calculate(BigInteger Value1, BigInteger Value2)
 {
     return(TupleOperation.Calculate(Value1, Value2, this.Operand));
 }
コード例 #7
0
 public static OperationType Parse(char charOperand)
 {
     return(TupleOperation.Parse(charOperand.ToString()));
 }
コード例 #8
0
 public decimal Calculate(decimal Value1, decimal Value2)
 {
     return(TupleOperation.Calculate(Value1, Value2, Operand));
 }