Beispiel #1
0
        //从题目文件中读入题目,计算答案并与之前已经获得的答案进行对比。
        public void ReadExerciseFile()
        {
            FileStream stream = new FileStream(ExePath, FileMode.Open);
            //以Unicode格式打开
            StreamReader sr    = new StreamReader(stream, Encoding.Unicode);
            string       line  = "";
            int          index = 1;

            while ((line = sr.ReadLine()) != null)
            {
                //获取题目表达式
                string Exercise = CheckExeIndex(line, index);
                //获取正确的答案
                string CorrectAnswer = new AnswerGet(Exercise).getAnswer();
                if (index > FileAnswer.Count())
                {
                    throw new MyException.OwnException("没做完题还想来对答案,哼!你只做了" + FileAnswer.Count() + "道题呀!");
                }
                //如果答案与当前序列的文件答案一致,则将序号记录入正确集
                if (CorrectAnswer == FileAnswer[index - 1])
                {
                    Correct.Add(index);
                }
                //如果答案与当前序列的文件答案不一致,则将序号记录入错误集
                else
                {
                    Wrong.Add(index);
                }
                index++;
            }
        }
Beispiel #2
0
 //从题目文件中读入题目,计算答案并与之前已经获得的答案进行对比。
 public void ReadExerciseFile()
 {
     FileStream stream = new FileStream(ExePath, FileMode.Open);
     //以Unicode格式打开
     StreamReader sr = new StreamReader(stream, Encoding.Unicode);
     string line = "";
     int index = 1;
     while ((line = sr.ReadLine()) != null)
     {
         //获取题目表达式
         string Exercise = CheckExeIndex(line, index);
         //获取正确的答案
         string CorrectAnswer =new AnswerGet(Exercise).getAnswer();
         if (index > FileAnswer.Count())
             throw new MyException.OwnException("没做完题还想来对答案,哼!你只做了"+FileAnswer.Count()+"道题呀!");
         //如果答案与当前序列的文件答案一致,则将序号记录入正确集
         if (CorrectAnswer == FileAnswer[index - 1])
             Correct.Add(index);
         //如果答案与当前序列的文件答案不一致,则将序号记录入错误集
         else
             Wrong.Add(index);
         index++;
     }
 }
Beispiel #3
0
        //将一个中缀表达式转换为后缀表达式
        public string InfixToSuffix(string Infix)
        {
            //输入一个不带括号的中缀表达式,将其转换为一个后缀表达式
            string[] split = Infix.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            //操作符栈
            Stack <string> StackOp = new Stack <string>();
            string         Suffix  = "";

            foreach (string token in split)
            {
                if (token == "-" || token == "+" || token == "×" || token == "÷")
                {
                    //如果操作符栈是空的,操作符入栈
                    if (StackOp.Count == 0)
                    {
                        StackOp.Push(token);
                    }
                    //否则比较当前操作符与操作符栈顶操作符的优先级
                    else
                    {
                        //获取当前操作符的优先级
                        int TokenPrior = AnswerGet.getPriority(token);
                        //获取操作符栈栈顶操作符的优先级
                        int TopPrior = AnswerGet.getPriority(StackOp.Peek());
                        //如果当前操作符优先级比栈顶操作符优先级大,则入栈
                        if (TokenPrior > TopPrior)
                        {
                            StackOp.Push(token);
                        }
                        else
                        {
                            //否则一直弹出并加入到后缀表达式中,到当前操作符优先级比栈顶的大为止
                            while (TokenPrior <= TopPrior)
                            {
                                //操作符出栈,加入到后缀表达式中
                                Suffix += " " + StackOp.Pop() + " ";
                                if (StackOp.Count > 0)
                                {
                                    //获取栈顶操作符的优先级
                                    TopPrior = AnswerGet.getPriority(StackOp.Peek());
                                }
                                else
                                {
                                    break;
                                }
                            }
                            //这时操作符优先级比栈顶的大,或者栈顶已经被清空
                            StackOp.Push(token);
                        }
                    }
                }
                else if (token == "=")
                {
                    while (StackOp.Count != 0)
                    {
                        //操作符进入后缀表达式,直到操作符栈为空
                        Suffix += " " + StackOp.Pop() + " ";
                    }
                }
                else
                {
                    Suffix += " " + token + " ";
                }
            }
            return(Suffix + " =");
        }