private Common.Solution GetProblemSolution(Problem problem) { if (problem != null) { Function func = new Function(problem.Function, problem.Function2); var mistakes = this.Data.Mistakes.All().Where(m => m.ProblemId == problem.Id).ToList(); Common.Mistake[] modelMistakes = new Common.Mistake[4]; for (int i = 0; i < 4; i++) { modelMistakes[i] = new Common.Mistake(mistakes.ElementAt(i).MistakenProduct, mistakes.ElementAt(i).MistakenValue, mistakes.ElementAt(i).IndexOfX, mistakes.ElementAt(i).DisplayProduct); } Common.Solution courseworkSolution = FunctionParser.Solve(func, modelMistakes); courseworkSolution.MatrixG = new int[8, 10]; if (courseworkSolution.SecondTable == null) { for (int i = 0; i < 8; i++) { courseworkSolution.MatrixG[i, 0] = courseworkSolution.FirstTable[i, 3] == courseworkSolution.FirstTable[i, 4] ? 0 : 1; courseworkSolution.MatrixG[i, 1] = courseworkSolution.FirstTable[i, 3] == courseworkSolution.FirstTable[i, 5] ? 0 : 1; courseworkSolution.MatrixG[i, 2] = courseworkSolution.FirstTable[i, 3] == courseworkSolution.FirstTable[i, 6] ? 0 : 1; courseworkSolution.MatrixG[i, 3] = courseworkSolution.FirstTable[i, 3] == courseworkSolution.FirstTable[i, 7] ? 0 : 1; courseworkSolution.MatrixG[i, 4] = courseworkSolution.FirstTable[i, 4] == courseworkSolution.FirstTable[i, 5] ? 0 : 1; courseworkSolution.MatrixG[i, 5] = courseworkSolution.FirstTable[i, 4] == courseworkSolution.FirstTable[i, 6] ? 0 : 1; courseworkSolution.MatrixG[i, 6] = courseworkSolution.FirstTable[i, 4] == courseworkSolution.FirstTable[i, 7] ? 0 : 1; courseworkSolution.MatrixG[i, 7] = courseworkSolution.FirstTable[i, 5] == courseworkSolution.FirstTable[i, 6] ? 0 : 1; courseworkSolution.MatrixG[i, 8] = courseworkSolution.FirstTable[i, 5] == courseworkSolution.FirstTable[i, 7] ? 0 : 1; courseworkSolution.MatrixG[i, 9] = courseworkSolution.FirstTable[i, 6] == courseworkSolution.FirstTable[i, 7] ? 0 : 1; } } else { for (int i = 0; i < 8; i++) { int first = courseworkSolution.FirstTable[i, 3] == courseworkSolution.FirstTable[i, 4] ? 0 : 1; int second = courseworkSolution.SecondTable[i, 3] == courseworkSolution.SecondTable[i, 4] ? 0 : 1; if (first == 1 || second == 1) { courseworkSolution.MatrixG[i, 0] = 1; } else { courseworkSolution.MatrixG[i, 0] = 0; } first = courseworkSolution.FirstTable[i, 3] == courseworkSolution.FirstTable[i, 5] ? 0 : 1; second = courseworkSolution.SecondTable[i, 3] == courseworkSolution.SecondTable[i, 5] ? 0 : 1; if (first == 1 || second == 1) { courseworkSolution.MatrixG[i, 1] = 1; } else { courseworkSolution.MatrixG[i, 1] = 0; } first = courseworkSolution.FirstTable[i, 3] == courseworkSolution.FirstTable[i, 6] ? 0 : 1; second = courseworkSolution.SecondTable[i, 3] == courseworkSolution.SecondTable[i, 6] ? 0 : 1; if (first == 1 || second == 1) { courseworkSolution.MatrixG[i, 2] = 1; } else { courseworkSolution.MatrixG[i, 2] = 0; } first = courseworkSolution.FirstTable[i, 3] == courseworkSolution.FirstTable[i, 7] ? 0 : 1; second = courseworkSolution.SecondTable[i, 3] == courseworkSolution.SecondTable[i, 7] ? 0 : 1; if (first == 1 || second == 1) { courseworkSolution.MatrixG[i, 3] = 1; } else { courseworkSolution.MatrixG[i, 3] = 0; } first = courseworkSolution.FirstTable[i, 4] == courseworkSolution.FirstTable[i, 5] ? 0 : 1; second = courseworkSolution.SecondTable[i, 4] == courseworkSolution.SecondTable[i, 5] ? 0 : 1; if (first == 1 || second == 1) { courseworkSolution.MatrixG[i, 4] = 1; } else { courseworkSolution.MatrixG[i, 4] = 0; } first = courseworkSolution.FirstTable[i, 4] == courseworkSolution.FirstTable[i, 6] ? 0 : 1; second = courseworkSolution.SecondTable[i, 4] == courseworkSolution.SecondTable[i, 6] ? 0 : 1; if (first == 1 || second == 1) { courseworkSolution.MatrixG[i, 5] = 1; } else { courseworkSolution.MatrixG[i, 5] = 0; } first = courseworkSolution.FirstTable[i, 4] == courseworkSolution.FirstTable[i, 7] ? 0 : 1; second = courseworkSolution.SecondTable[i, 4] == courseworkSolution.SecondTable[i, 7] ? 0 : 1; if (first == 1 || second == 1) { courseworkSolution.MatrixG[i, 6] = 1; } else { courseworkSolution.MatrixG[i, 6] = 0; } first = courseworkSolution.FirstTable[i, 5] == courseworkSolution.FirstTable[i, 6] ? 0 : 1; second = courseworkSolution.SecondTable[i, 5] == courseworkSolution.SecondTable[i, 6] ? 0 : 1; if (first == 1 || second == 1) { courseworkSolution.MatrixG[i, 7] = 1; } else { courseworkSolution.MatrixG[i, 7] = 0; } first = courseworkSolution.FirstTable[i, 5] == courseworkSolution.FirstTable[i, 7] ? 0 : 1; second = courseworkSolution.SecondTable[i, 5] == courseworkSolution.SecondTable[i, 7] ? 0 : 1; if (first == 1 || second == 1) { courseworkSolution.MatrixG[i, 8] = 1; } else { courseworkSolution.MatrixG[i, 8] = 0; } first = courseworkSolution.FirstTable[i, 6] == courseworkSolution.FirstTable[i, 7] ? 0 : 1; second = courseworkSolution.SecondTable[i, 6] == courseworkSolution.SecondTable[i, 7] ? 0 : 1; if (first == 1 || second == 1) { courseworkSolution.MatrixG[i, 9] = 1; } else { courseworkSolution.MatrixG[i, 9] = 0; } } } return courseworkSolution; } return null; }
public ActionResult CheckProblemPost() { var userId = this.User.Identity.GetUserId(); var function1 = Request.Form["Function.Function1"]; var function2 = Request.Form["Function.Function2"]; var newProblem = new Problem(userId, function1, function2 == "" ? null : function2, ProblemType.Coursework, ProblemStatus.Active); this.Data.Problems.Add(newProblem); this.Data.SaveChanges(); int[] mistakes = new int[4]; var solutionModel = new ProblemSolutionViewModel(); for (int i = 0; i < 4; i++) { var mistakeIndex = Request.Form["Mistakes[" + i + "].IndexOfX"]; var mistakeProduct = Request.Form["Mistakes[" + i + "].MistakenProduct"]; var mistakeValue = Request.Form["Mistakes[" + i + "].MistakenValue"]; string mistakeDisplay; if (i < 2) { mistakeDisplay = FunctionParser.GetDisplayX(mistakeProduct + mistakeIndex); } else { mistakeDisplay = FunctionParser.GetDisplayProduct(mistakeProduct); } var mistake = new NDKS.Models.Mistake(newProblem.Id, mistakeProduct, Int32.Parse(mistakeValue), mistakeIndex == "" ? null : (int?) Int32.Parse(mistakeIndex), mistakeDisplay); solutionModel.Mistakes.Add(mistake); this.Data.Mistakes.Add(mistake); this.Data.SaveChanges(); mistakes[i] = mistake.Id; } this.Data.SaveChanges(); solutionModel.Solution = this.GetProblemSolution(newProblem); int problemId = newProblem.Id; for (int i = 0; i < mistakes.Length; i++) { var mistake = this.Data.Mistakes.Find(mistakes[i]); this.Data.Mistakes.Delete(mistake); } this.Data.Problems.Delete(problemId); this.Data.SaveChanges(); solutionModel.Functions = FunctionParser.GetDisplayFunction(new Function(function1, function2 == "" ? null : function2)); return View(solutionModel); }
private void AddProblem(System.Collections.Specialized.NameValueCollection form, ProblemType type, string userId) { var function1 = form["Function.Function1"]; var function2 = form["Function.Function2"]; var newProblem = new Problem(userId, function1, function2 == "" ? null : function2, type, ProblemStatus.Active); this.Data.Problems.Add(newProblem); this.Data.SaveChanges(); Common.Mistake[] mistakes = new Common.Mistake[4]; for (int i = 0; i < 4; i++) { var mistakeIndex = form["Mistakes[" + i + "].IndexOfX"]; var mistakeProduct = form["Mistakes[" + i + "].MistakenProduct"]; var mistakeValue = form["Mistakes[" + i + "].MistakenValue"]; string mistakeDisplay; if (i < 2) { mistakeDisplay = FunctionParser.GetDisplayX(mistakeProduct + mistakeIndex); } else { mistakeDisplay = FunctionParser.GetDisplayProduct(mistakeProduct); } var mistake = new NDKS.Models.Mistake(newProblem.Id, mistakeProduct, Int32.Parse(mistakeValue), mistakeIndex == "" ? null : (int?) Int32.Parse(mistakeIndex), mistakeDisplay); this.Data.Mistakes.Add(mistake); } this.Data.SaveChanges(); }
public ActionResult Exercise() { string loggedUserId = User.Identity.GetUserId(); var existingProblem = this.Data.Problems .All().FirstOrDefault(p => p.UserId == loggedUserId && p.ProblemType == ProblemType.Train && p.ProblemStatus == ProblemStatus.Active); if (existingProblem != null) { var func = new Function(existingProblem.Function, existingProblem.Function2); ViewBag.func = FunctionParser.GetDisplayFunction(func); var existingMistakes = this.Data.Mistakes .All() .Where(m => m.ProblemId == existingProblem.Id).ToList(); Common.Mistake[] existingMistakesForSolution = new Common.Mistake[4]; for (int i = 0; i < existingMistakes.Count(); i++) { existingMistakesForSolution[i] = new Common.Mistake( existingMistakes.ElementAt(i).MistakenProduct, existingMistakes.ElementAt(i).MistakenValue, existingMistakes.ElementAt(i).IndexOfX, existingMistakes.ElementAt(i).DisplayProduct); } for (int i = 0; i < existingMistakesForSolution.Length; i++) { ViewBag.func += "</br> T" + (i + 1) + " => " + existingMistakesForSolution[i].DisplayProduct + " = " + existingMistakesForSolution[i].MistakenValue; } var existingSolution = FunctionParser.Solve(func, existingMistakesForSolution); var viewModel = new Models.Solution(existingSolution, existingProblem.Id); return View(viewModel); } function = FunctionParser.Generate(); Common.Mistake[] mistakes = FunctionParser.GenerateMistakes(function); ViewBag.func = FunctionParser.GetDisplayFunction(function); for (int i = 0; i < mistakes.Length; i++) { ViewBag.func += "</br> T" + (i + 1) + " => " + mistakes[i].DisplayProduct + " = " + mistakes[i].MistakenValue; } var solution = FunctionParser.Solve(function, mistakes); ViewBag.table1 = solution.FirstTable; if (solution.SecondTable != null) { ViewBag.table2 = solution.SecondTable; } Problem problem = new Problem( loggedUserId, this.function.Function1, this.function.Function2, ProblemType.Train, ProblemStatus.Active ); this.Data.Problems.Add(problem); this.Data.SaveChanges(); for (int i = 0; i < mistakes.Length; i++) { NDKS.Models.Mistake newMistake = new NDKS.Models.Mistake( problem.Id, mistakes[i].MistakenProduct, mistakes[i].MistakenValue, mistakes[i].IndexOfX, mistakes[i].DisplayProduct); this.Data.Mistakes.Add(newMistake); } this.Data.SaveChanges(); var solutionModel = new Models.Solution(solution, problem.Id); return View(solutionModel); }