private void button1_Click(object sender, EventArgs e) { var test = new Form2 { Text = $@"{textBox1.Text} | {textBox2.Text} | {comboBox1.Text}" }; test.Qp = QuestionPack.FromFile($".\\quests\\{comboBox1.Text}"); if (test.ShowDialog() != DialogResult.OK) { return; } var sum = test.Answers.Sum(i => i.Value); var cnt = test.Answers.Count; var prc = sum * 100f / cnt; var scr = prc * 5f / 100f; File.AppendAllText($".\\quests\\{comboBox1.Text.Replace(".qst",".ans")}", $"Дата : {DateTime.Now.ToShortDateString()} {DateTime.Now.ToLongTimeString()}\r\n" + $"Студент : {textBox1.Text}\r\n" + $"Группа : {textBox2.Text}\r\n" + $"Оценка : {Math.Round(scr, 0)} ({Math.Round(scr, 4)})\r\n" + $"Время : {(test.Qp.TimeToTest>0 ? $"{(test.Ended- test.Started).TotalSeconds:N0}" : "0" )} сек.\r\n" + $"-------------------------------------------------------------------\r\n" + $"Вопросов: {Math.Round(sum, 0)} из {cnt} ({Math.Round(prc, 3)}% верно)\r\n" + $"Баллы : ({Math.Round(sum, 2):N2}) {string.Join(" | ", test.Answers.OrderBy(a=>a.Key).Select(a => Math.Round(a.Value, 2)).ToArray())}\r\n" + $"===================================================================\r\n", Encoding.UTF8 ); string wa = ""; for (int i = 0; i < test.Answers.Count; i++) { if (test.Answers[i] < 1f) { wa += $"{i}] {test.Answers[i]}%\n"; } } MessageBox.Show( $"Вы прошли тест: {comboBox1.Text}\n\nВаша оценка: {Math.Round(scr, 0)} ({Math.Round(scr, 4)})\n\nПравильных ответов: {Math.Round(sum, 0)} из {cnt} ({Math.Round(prc, 3)}%){(wa != "" ? $"\n\nСписок неверных ответов:\n{wa.Trim()}" : "")}", @"Тест пройден", MessageBoxButtons.OK, MessageBoxIcon.Information); }
public static QuestionPack FromFile(string fileName) { if (string.IsNullOrEmpty(fileName)) { return(null); } var q = new QuestionPack(); try { using (var f = new FileStream(fileName, FileMode.Open, FileAccess.Read)) using (var r = new BinaryReader(f)) { q.Author = r.ReadString(); q.Title = r.ReadString(); q.Discipline = r.ReadString(); q.Theme = r.ReadString(); q.Questions = new List <Question>(); q.TimeToAnswer = r.ReadInt32(); q.TimeToTest = r.ReadInt32(); var qc = r.ReadInt32(); for (var i = 0; i < qc; i++) { var bin = r.ReadBytes(r.ReadInt32()); var qt = Question.FromBinary(bin); if (qt != null) { q.Questions.Add(qt); } } } } catch { return(null); } return(q); }