private static bool IsDigitAnswerQuestion(HtmlStore quest) { bool retValue = false; if (quest.SubItems.Count != 1) { return(retValue); } HtmlStore answer = quest.SubItems[0]; bool isTrue = answer.IsTrue; if (isTrue) { string st = answer.Html; st = st.Trim(); st = st.Replace("<br/>", ""); st = st.Replace(" ", ""); st = st.Replace("±", "±"); int pos = st.IndexOf("±"); if (pos == -1) { if (isFloat(st)) { retValue = true; st = st.Replace(",", "."); answer.Html = st + "±0"; } else { double?dValue = HtmlStore.GetFractionValue(st); if (dValue.HasValue) { retValue = true; st = dValue.Value.ToString().Replace(",", "."); answer.Html = st + "±0"; } } } else { string subst1 = st.Substring(0, pos); string subst2 = st.Substring(pos + 1); if (isFloat(subst1) && isFloat(subst2)) { retValue = true; answer.Html = st; } else { double?dValue = HtmlStore.GetFractionValue(subst1); if (dValue.HasValue && isFloat(subst2)) { retValue = true; st = dValue.Value.ToString().Replace(",", "."); answer.Html = st + "±" + subst2.ToString(); } } } } return(retValue); }
public override bool?IsRightAnswer(Dictionary <string, List <string> > _requestParams, ref string message, ref string questAnswer) { List <string> ans = GetValueList(_requestParams, "tcv_testortext", ref questAnswer); if (ans == null || ans.Count == 0) { return(false); } string answer = questAnswer.Replace(';', '.'); if (answer.EndsWith(".")) { answer = answer.Remove(answer.Length - 1, 1); } var rightAns = _htmlStore.SubItems.Where(c => c.IsTrue == true); if (rightAns.Count() == 0) { return(false); } string right = rightAns.First().Html.Trim(); string sep = CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator; if (sep == ".") { answer = answer.Replace(",", "."); right = right.Replace(",", "."); } else { answer = answer.Replace(".", ","); right = right.Replace(".", ","); } int pos = right.IndexOf("±"); if (pos < 0) { return(false); } string stTrue = right.Substring(0, pos); string stDelta = right.Substring(pos + 1, right.Length - pos - 1); double flTrue = 0; double flDelta = 0; double flUserAnsver = 0; try { flTrue = double.Parse(stTrue, NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat); flDelta = double.Parse(stDelta, NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat); if (flDelta == 0) { flDelta = 0.0001; } flUserAnsver = 0.0f; } catch (Exception ex) { message = ex.Message; return(null); } try { double?fractionValue = HtmlStore.GetFractionValue(answer); if (fractionValue.HasValue) { flUserAnsver = fractionValue.Value; answer = flUserAnsver.ToString(); } else { flUserAnsver = double.Parse(answer, NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat); } } catch { message = "Ответом должно быть десятичное число например \"-3.1415926\"."; return(null); } if ((flUserAnsver <= flTrue + flDelta) && (flUserAnsver >= flTrue - flDelta)) { return(true); } else { var tv = stTrue.Split(new string[] { sep }, StringSplitOptions.RemoveEmptyEntries); if (tv.Length != 2) { return(false); } int fractionLength = tv[1].Length; flUserAnsver = Math.Round(flUserAnsver, fractionLength); if ((flUserAnsver <= flTrue + flDelta) && (flUserAnsver >= flTrue - flDelta)) { return(true); } } return(false); }