void sortlist(ref aoption[] mylist,int count) { aoption tmp; for (int i = count - 1; i > 0; i--) for (int j = 0; j < i; j++) { if (mylist[j].content.CompareTo(mylist[j+1].content) > 0) { tmp = mylist[j]; mylist[j] = mylist[j + 1]; mylist[j + 1] = tmp; } } }
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (webBrowser1.IsBusy) return; if (e.Url.AbsolutePath == "blank") return; if (!debugmode && e.Url.AbsolutePath.Contains("attempt") == false) { return; } if (!ansReady) { MessageBox.Show("题库初始化失败!请联系开发者", "错误!!!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex( @"^.{1}\.", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex reg_page = new System.Text.RegularExpressions.Regex( @"page=(?<page>\d*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex reg_attempt = new System.Text.RegularExpressions.Regex( @"attempt=(?<attempt>\d*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase); string attemptOri = reg_attempt.Match(e.Url.AbsoluteUri).Groups["attempt"].Value; int attempt; int.TryParse(attemptOri, out attempt); string pageOri = reg_page.Match(e.Url.AbsoluteUri).Groups["page"].Value; int page; int.TryParse(pageOri, out page); bool info = false; if (attempt != current_attempt) { init_attempt(attempt); } else if (auto_Ans == 0 && page == 44) { auto_Ans = 1; info = true; } HtmlElement formDom, curQuestion, CurQuestionAwsDiv; formDom = webBrowser1.Document.GetElementById("responseform"); HtmlElementCollection tmp; tmp = webBrowser1.Document.GetElementsByTagName("h1"); int t = searchitem("classname", "headermain", ref tmp); if(t == tmp.Count ) { throw new Exception ("fail to get title"); } string title = tmp[t].InnerText; int lostAns = 0; HtmlElementCollection alldiv = webBrowser1.Document.GetElementsByTagName("div"); HtmlElementCollection allInput = webBrowser1.Document.GetElementsByTagName("input"); for (int i=0; i < alldiv.Count; i++) { if (alldiv[i].GetAttribute("id").Length > 0 && alldiv[i].GetAttribute("id")[0] != 'q') continue; if (!alldiv[i].GetAttribute("classname").Contains("que")) continue; //curQuestion = formDom.Document.GetElementById("q" + i.ToString()); curQuestion = alldiv[i]; tmp = curQuestion.GetElementsByTagName("div"); // 题目 t = searchitem("classname", "qtext", ref tmp); string questionTitle = tmp[t].InnerText; questionTitle = questionTitle.Replace("\r\n", ""); questionTitle = questionTitle.Replace(" ", ""); string questionTitleMd5 = GetMD5(questionTitle); // 分数 t = searchitem("classname", "grade", ref tmp); string questionTypeOri = tmp[t].InnerText; string questionType = ""; if (questionTypeOri == "满分1.00") { questionType = "1"; } else { questionType = "2"; } // 答案列 t = searchitem("classname", "answer", ref tmp); if (t == tmp.Count) throw new Exception("fail to get answerlist"); CurQuestionAwsDiv = tmp[t]; HtmlElementCollection AwsContentList = CurQuestionAwsDiv.GetElementsByTagName("label"); aoption[] tmplist = new aoption[AwsContentList.Count]; for (int j = 0; j < AwsContentList.Count; j++) { tmplist[j].id = AwsContentList[j].GetAttribute("htmlfor"); tmplist[j].content = AwsContentList[j].InnerText; tmplist[j].content = reg.Replace(tmplist[j].content, "").Trim(); } sortlist(ref tmplist, AwsContentList.Count); if (!ans.ContainsKey(questionTitleMd5)) { lostAns++; curQuestion.Style = "border: 5px red solid;"; debugstr += '"' + questionTitleMd5 + "\","; debugstr += '"' + questionType + "\","; debugstr += '"' + questionTitle + "\","; for (int k = 0; k < tmplist.Length; k++) { debugstr += '"' + tmplist[k].content + "\","; } debugstr += "\r\n"; debugupdate = true; continue; } for (int j = 0; j < ans[questionTitleMd5].num; j++) { for (int k = 0; k < allInput.Count; k++) { if (allInput[k].Id == tmplist[ans[questionTitleMd5].anslist[j]].id) { if (allInput[k].GetAttribute("checked").ToLower() == "true") { } else { allInput[k].InvokeMember("click"); break; } } } } } if (lostAns > 0) { /* MessageBox.Show("共计 " + lostAns.ToString() + " 道题未找到答案。题目已经用红线标出;\r\n请协助提交题库并谨慎答题提交。" , "警告!", MessageBoxButtons.OK, MessageBoxIcon.Warning); //MessageBox.Show(debugstr); if (debugInfo) { Form2 debugtool = new Form2(); debugtool.setDebugInfo(debugstr); debugtool.ShowDialog(); debugtool.Dispose(); } * */ auto_Ans_Status[page] = 0; } else { auto_Ans_Status[page] = 1; } // 翻页 if (auto_Ans == 0 && debugmode == false) { for (int i = 0; i < alldiv.Count; i++) { if (alldiv[i].GetAttribute("classname").Contains("submitbtns")) { alldiv[i].GetElementsByTagName("input")[0].InvokeMember("click"); break; } } } // 提示 if (info == true) { string info_str = ""; int errpage = 0; for (int i = 0; i < 45; i++) { if (auto_Ans_Status[i] == 0) { info_str += (i+1).ToString() + ","; errpage++; } } if (errpage > 0) { MessageBox.Show("以下页面未找到答案或者答题发生意外: \r\n" + info_str + "\r\n请协助提交题库并谨慎答题提交。" , "警告!", MessageBoxButtons.OK, MessageBoxIcon.Warning); if (debugInfo && debugupdate) { Form2 debugtool = new Form2(); debugtool.setDebugInfo(debugstr); debugtool.ShowDialog(); debugtool.Dispose(); } } else { MessageBox.Show("辅助答题已经完成,请仔细检查是否有错误并谨慎提交。" , "警告!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (webBrowser1.IsBusy) { return; } if (e.Url.AbsolutePath == "blank") { return; } if (!debugmode && e.Url.AbsolutePath.Contains("attempt") == false) { return; } if (!ansReady) { MessageBox.Show("题库初始化失败!请联系开发者", "错误!!!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex( @"^.{1}\.", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex reg_page = new System.Text.RegularExpressions.Regex( @"page=(?<page>\d*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex reg_attempt = new System.Text.RegularExpressions.Regex( @"attempt=(?<attempt>\d*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase); string attemptOri = reg_attempt.Match(e.Url.AbsoluteUri).Groups["attempt"].Value; int attempt; int.TryParse(attemptOri, out attempt); string pageOri = reg_page.Match(e.Url.AbsoluteUri).Groups["page"].Value; int page; int.TryParse(pageOri, out page); bool info = false; if (attempt != current_attempt) { init_attempt(attempt); } else if (auto_Ans == 0 && page == 44) { auto_Ans = 1; info = true; } HtmlElement formDom, curQuestion, CurQuestionAwsDiv; formDom = webBrowser1.Document.GetElementById("responseform"); HtmlElementCollection tmp; tmp = webBrowser1.Document.GetElementsByTagName("h1"); int t = searchitem("classname", "headermain", ref tmp); if (t == tmp.Count) { throw new Exception("fail to get title"); } string title = tmp[t].InnerText; int lostAns = 0; HtmlElementCollection alldiv = webBrowser1.Document.GetElementsByTagName("div"); HtmlElementCollection allInput = webBrowser1.Document.GetElementsByTagName("input"); for (int i = 0; i < alldiv.Count; i++) { if (alldiv[i].GetAttribute("id").Length > 0 && alldiv[i].GetAttribute("id")[0] != 'q') { continue; } if (!alldiv[i].GetAttribute("classname").Contains("que")) { continue; } //curQuestion = formDom.Document.GetElementById("q" + i.ToString()); curQuestion = alldiv[i]; tmp = curQuestion.GetElementsByTagName("div"); // 题目 t = searchitem("classname", "qtext", ref tmp); string questionTitle = tmp[t].InnerText; questionTitle = questionTitle.Replace("\r\n", ""); questionTitle = questionTitle.Replace(" ", ""); string questionTitleMd5 = GetMD5(questionTitle); // 分数 t = searchitem("classname", "grade", ref tmp); string questionTypeOri = tmp[t].InnerText; string questionType = ""; if (questionTypeOri == "满分1.00") { questionType = "1"; } else { questionType = "2"; } // 答案列 t = searchitem("classname", "answer", ref tmp); if (t == tmp.Count) { throw new Exception("fail to get answerlist"); } CurQuestionAwsDiv = tmp[t]; HtmlElementCollection AwsContentList = CurQuestionAwsDiv.GetElementsByTagName("label"); aoption[] tmplist = new aoption[AwsContentList.Count]; for (int j = 0; j < AwsContentList.Count; j++) { tmplist[j].id = AwsContentList[j].GetAttribute("htmlfor"); tmplist[j].content = AwsContentList[j].InnerText; tmplist[j].content = reg.Replace(tmplist[j].content, "").Trim(); } sortlist(ref tmplist, AwsContentList.Count); if (!ans.ContainsKey(questionTitleMd5)) { lostAns++; curQuestion.Style = "border: 5px red solid;"; debugstr += '"' + questionTitleMd5 + "\","; debugstr += '"' + questionType + "\","; debugstr += '"' + questionTitle + "\","; for (int k = 0; k < tmplist.Length; k++) { debugstr += '"' + tmplist[k].content + "\","; } debugstr += "\r\n"; debugupdate = true; continue; } for (int j = 0; j < ans[questionTitleMd5].num; j++) { for (int k = 0; k < allInput.Count; k++) { if (allInput[k].Id == tmplist[ans[questionTitleMd5].anslist[j]].id) { if (allInput[k].GetAttribute("checked").ToLower() == "true") { } else { allInput[k].InvokeMember("click"); break; } } } } } if (lostAns > 0) { /* * MessageBox.Show("共计 " + lostAns.ToString() + " 道题未找到答案。题目已经用红线标出;\r\n请协助提交题库并谨慎答题提交。" + , "警告!", MessageBoxButtons.OK, MessageBoxIcon.Warning); + //MessageBox.Show(debugstr); + if (debugInfo) + { + Form2 debugtool = new Form2(); + debugtool.setDebugInfo(debugstr); + debugtool.ShowDialog(); + debugtool.Dispose(); + } * */ auto_Ans_Status[page] = 0; } else { auto_Ans_Status[page] = 1; } // 翻页 if (auto_Ans == 0 && debugmode == false) { for (int i = 0; i < alldiv.Count; i++) { if (alldiv[i].GetAttribute("classname").Contains("submitbtns")) { alldiv[i].GetElementsByTagName("input")[0].InvokeMember("click"); break; } } } // 提示 if (info == true) { string info_str = ""; int errpage = 0; for (int i = 0; i < 45; i++) { if (auto_Ans_Status[i] == 0) { info_str += (i + 1).ToString() + ","; errpage++; } } if (errpage > 0) { MessageBox.Show("以下页面未找到答案或者答题发生意外: \r\n" + info_str + "\r\n请协助提交题库并谨慎答题提交。" , "警告!", MessageBoxButtons.OK, MessageBoxIcon.Warning); if (debugInfo && debugupdate) { Form2 debugtool = new Form2(); debugtool.setDebugInfo(debugstr); debugtool.ShowDialog(); debugtool.Dispose(); } } else { MessageBox.Show("辅助答题已经完成,请仔细检查是否有错误并谨慎提交。" , "警告!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }