public ActionResult Calculate() { var vm = new LCMViewModel() { History = new History(), AlgorithmTypes = _context.AlgorithmTypes.ToList() }; return(View("Index", vm)); }
public ActionResult Calculate(LCMViewModel lcmViewModel) { if (!ModelState.IsValid) { var vm = new LCMViewModel() { History = new History(), AlgorithmTypes = _context.AlgorithmTypes.ToList() }; return(View("Index", vm)); } int[] arr = Array.ConvertAll(lcmViewModel.History.Inputs.Split(','), a => Convert.ToInt32(a)); var userId = User.Identity.GetUserId(); var watch = new System.Diagnostics.Stopwatch(); watch.Start(); long beforeAlloc = GC.GetTotalMemory(false); long result = 1; if (lcmViewModel.History.AlgorithmTypeId == 1) { result = lcmUsingBestTimeComplexity(arr, arr.Length); } else if (lcmViewModel.History.AlgorithmTypeId == 2) { result = lcmUsingBestSpaceComplexity(arr); } else { result = lcmUsingOptimalComplexity(arr); } long afterAlloc = GC.GetTotalMemory(false); watch.Stop(); var history = new History() { Inputs = lcmViewModel.History.Inputs, AlgorithmTypeId = lcmViewModel.History.AlgorithmTypeId, //need to calculate TimeComplexity = Convert.ToString(watch.ElapsedMilliseconds) + " ms", SpaceComplexity = Convert.ToString(afterAlloc - beforeAlloc) + " B", Result = Convert.ToString(result), UserId = userId }; _context.Histories.Add(history); var vm1 = new LCMViewModel() { AlgorithmTypes = _context.AlgorithmTypes.ToList(), History = history }; _context.SaveChanges(); return(View("Index", vm1)); }