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; }
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; }