コード例 #1
0
 public static int GetPermutation(InputValues value)
 {
     var nFact = GetFactorial(value.N);
     if (nFact >= Int32.MaxValue)
         throw new Exception("Result is too large.");
     var nrFact = GetFactorial(value.N - value.R);
     return nFact / nrFact;
 }
コード例 #2
0
        public ActionResult Index(FormCollection collection)
        {
            int nn, rr;
            var inputValues = new InputValues();
            string n = collection["N"];
            bool nOK = Int32.TryParse(n, out nn);
            string r = collection["R"];
            bool rOK = Int32.TryParse(r, out rr);
            if (nOK && rOK)
            {
                inputValues = new InputValues() { N = nn, R = rr };
                if (nn < rr)
                {
                    ViewBag.Result = "n cannot be less than r.";
                }
                else
                {
                    try
                    {
                        var result = 0;
                        var resultType = "";
                        var type = collection["hdnOperation"];
                        if (type.ToLower() == "per")
                        {
                            result = Calculator.GetPermutation(inputValues);
                            resultType = "P";
                        }
                        else
                        {
                            result = Calculator.GetCombination(inputValues);
                            resultType = "C";
                        }
                        ViewBag.Result = string.Format("<sub>{0}</sub>{1}<sub>{2}</sub> = {3}", nn, resultType, rr, result);
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Result = ex.Message;

                    }
                }
            }
            else
            {
                ViewBag.Result = "Please enter valid numbers for N and R.";
            }

            return View(inputValues);
        }
コード例 #3
0
 public static int GetCombination(InputValues value)
 {
     return GetFactorial(value.N) / (GetFactorial(value.R) * GetFactorial(value.N - value.R));
 }