예제 #1
0
        private static void LoadHistory(string saveFile, Statuses status)
        {
            List<Question> allQuestion = status.AllQuestion;

            //清空题目数组并重新填充
            allQuestion.Clear();
            status.CurrentIdx = -1;

            // 收集已回答的id列表
            HashSet<int> answeredIds = new HashSet<int>();

            #region 加载文件内容
            using (var sr = new StreamReader(saveFile, GB2312))
            {
                // 第一行是所有题目id列表,兼容代码,不使用这些id了
                string line = sr.ReadLine();

                // 第二行是考试类型
                line = (sr.ReadLine() ?? string.Empty).Trim();
                int typeInt;
                if (!int.TryParse(line, out typeInt))
                {
                    return;
                }
                status.ExamType = (ExamType)typeInt;
                
                // 第3行开始,是回答完毕的用户答案情况
                while (!sr.EndOfStream)
                {
                    line = (sr.ReadLine() ?? string.Empty).Trim();
                    if (line == string.Empty)
                    {
                        continue;
                    }

                    var content = line.Split(','); //题号,回答,是否正确
                    var quesId = int.Parse(content[0].Trim());

                    if (answeredIds.Add(quesId))
                    {
                        var myAnswer = int.Parse(content[1].Trim());
                        Question question = LoadQuestion(quesId);
                        if (question == null)
                        {
                            return;
                        }

                        question.Answer = myAnswer;
                        allQuestion.Add(question);
                    }
                }
            }
            #endregion

            status.CurrentIdx = allQuestion.Count - 1;
            status.PrevIdx = status.CurrentIdx;
            int totalNum;
            bool isRandom;
            if (status.ExamType == ExamType.Examing)
            {
                totalNum = EXAM_NUM;
                isRandom = true;
            }
            else if (status.ExamType == ExamType.Random)
            {
                totalNum = QUESTION_NUM;
                isRandom = true;
            }
            else
            {
                totalNum = QUESTION_NUM;
                isRandom = false;
            }

            int ret = 0;
            // 考题不足时,补充
            while (allQuestion.Count < totalNum)
            {
                if (isRandom)
                {
                    ret = rnd.Next(1, QUESTION_NUM + 1);
                    while (!answeredIds.Add(ret))
                    {
                        ret = rnd.Next(1, QUESTION_NUM + 1);
                    }
                }
                else
                {
                    ret++;
                    while (!answeredIds.Add(ret))
                    {
                        ret++;
                    }
                }
                Question ques = LoadQuestion(ret);
                if (ques == null)
                {
                    return;
                }
                allQuestion.Add(ques);
            }

            answeredIds.Clear();
        }
예제 #2
0
        private static void SaveHistory(Statuses status)
        {
            string saveFile = status.SaveFile;
            if (!File.Exists(saveFile))
            {
                // 文件不存在时,创建
                using (var sw = new StreamWriter(saveFile, false, GB2312))
                {
                    // 保持空行,兼容旧历史的 保存全部题目 行
                    sw.WriteLine();

                    // 保存答题方式
                    sw.WriteLine((int)status.ExamType);
                }
            }
            Question question = status.Ques;
            using (var sw = new StreamWriter(saveFile, true, GB2312))
            {
                // 格式为:题号,回答,是否正确
                sw.WriteLine("{0},{1},{2}",
                    question.id.ToString(), question.Answer.ToString(), question.IsRight.ToString());
            }
        }