Esempio n. 1
0
 //生成表达式
 public GenerateExp(
     bool hasNegative/*负数*/,
     bool hasMultDiv/*乘除法*/,
     bool hasBrack/*括号*/,
     bool hasFrac/*分数*/,
     int opCount/*运算符个数上限*/,
     int exerciseCount/*数量上限*/,
     int leftRange/*左值域*/,
     int rightRange/*右值域*/,
     string ExePath,
     string AnsPath,
     ref int FactCount/*实际产生值*/
     )
 {
     //只需要一个随机种子就够了
     int iRnd = DateTime.Now.Millisecond;
     Random rand = new Random(iRnd);
     //需要一个用来判重的HashSet
     HashSet<string> MinusHash = new HashSet<string>();
     //按照操作符上限生成
     StringBuilder buildExercise = new StringBuilder();
     StringBuilder buildAnswer = new StringBuilder();
     DateTime time1=DateTime.Now, time2;
     int i = 0;
     try {
         for (i = 0; i < exerciseCount;)
         {
             try {
                 //在1和OpCount之间生成一个操作符的数量,注意这里的opCount是需要带上+1的!!
                 int OpCount = rand.Next(1, opCount + 1);
                 string[] operation = GenerateOperation(rand, OpCount, hasMultDiv);
                 //因为操作数总比操作符多1个,所以FracCount=OpCount+1
                 int FracCount = OpCount + 1;
                 Fraction[] Numbers = GenerateNumber(rand, FracCount, hasNegative/*控制生成的数里是否有负数*/, hasFrac/*生成的数里是否有分数*/, leftRange, rightRange);
                 //生成一个表达式啦!
                 ExpTree Expression = new ExpTree(Numbers, operation, hasNegative, hasBrack, hasFrac, leftRange, rand);
                 /*,如果最后出现了超过范围的数,会被转换成负数,那该怎么办呢... [FIXME]*/
                 //如果发现之前已经有重复的式子了,那就重新生成一个
                 if (MinusHash.Contains(Expression.TreeMinusLamda))
                 {
                     time2 = DateTime.Now;
                     TimeSpan Ts = time2 - time1;
                     if (Ts.Seconds >= 20)
                         throw new MyException.OwnException("这式子要爆炸!");
                     continue;
                 }
                 i++;
                 MinusHash.Add(Expression.TreeMinusLamda);
                 buildExercise.Append(i + "." + Expression.TreeInfix + Environment.NewLine);
                 buildAnswer.Append(i + "." + Expression.Answer + Environment.NewLine);
                 time1 = DateTime.Now;
             }
             //如果遭遇了溢出错误,则继续生成...
            catch (System.OverflowException)
             {
                 time2 = DateTime.Now;
                 TimeSpan Ts = time2 - time1;
                 if (Ts.Seconds >= 20)
                     throw new MyException.OwnException("这式子要爆炸!");
                 continue;
             }
         }
     }
     catch(MyException.OwnException)
     {
         throw new MyException.TooManyException("抱歉,我实在产生不了这么多式子,放过我吧...");
     }
     finally
     {
         FactCount = i;
         ExeToFile(buildExercise,ExePath);
         AnsToFile(buildAnswer,AnsPath);
     }
 }
Esempio n. 2
0
        //生成表达式
        public GenerateExp(
            bool hasNegative /*负数*/,
            bool hasMultDiv /*乘除法*/,
            bool hasBrack /*括号*/,
            bool hasFrac /*分数*/,
            int opCount /*运算符个数上限*/,
            int exerciseCount /*数量上限*/,
            int leftRange /*左值域*/,
            int rightRange /*右值域*/,
            string ExePath,
            string AnsPath,
            ref int FactCount/*实际产生值*/
            )
        {
            //只需要一个随机种子就够了
            int    iRnd = DateTime.Now.Millisecond;
            Random rand = new Random(iRnd);
            //需要一个用来判重的HashSet
            HashSet <string> MinusHash = new HashSet <string>();
            //按照操作符上限生成
            StringBuilder buildExercise = new StringBuilder();
            StringBuilder buildAnswer = new StringBuilder();
            DateTime      time1 = DateTime.Now, time2;
            int           i = 0;

            try {
                for (i = 0; i < exerciseCount;)
                {
                    try {
                        //在1和OpCount之间生成一个操作符的数量,注意这里的opCount是需要带上+1的!!
                        int      OpCount   = rand.Next(1, opCount + 1);
                        string[] operation = GenerateOperation(rand, OpCount, hasMultDiv);
                        //因为操作数总比操作符多1个,所以FracCount=OpCount+1
                        int        FracCount = OpCount + 1;
                        Fraction[] Numbers   = GenerateNumber(rand, FracCount, hasNegative /*控制生成的数里是否有负数*/, hasFrac /*生成的数里是否有分数*/, leftRange, rightRange);
                        //生成一个表达式啦!
                        ExpTree Expression = new ExpTree(Numbers, operation, hasNegative, hasBrack, hasFrac, leftRange, rand);
                        /*,如果最后出现了超过范围的数,会被转换成负数,那该怎么办呢... [FIXME]*/
                        //如果发现之前已经有重复的式子了,那就重新生成一个
                        if (MinusHash.Contains(Expression.TreeMinusLamda))
                        {
                            time2 = DateTime.Now;
                            TimeSpan Ts = time2 - time1;
                            if (Ts.Seconds >= 20)
                            {
                                throw new MyException.OwnException("这式子要爆炸!");
                            }
                            continue;
                        }
                        i++;
                        MinusHash.Add(Expression.TreeMinusLamda);
                        buildExercise.Append(i + "." + Expression.TreeInfix + Environment.NewLine);
                        buildAnswer.Append(i + "." + Expression.Answer + Environment.NewLine);
                        time1 = DateTime.Now;
                    }
                    //如果遭遇了溢出错误,则继续生成...
                    catch (System.OverflowException)
                    {
                        time2 = DateTime.Now;
                        TimeSpan Ts = time2 - time1;
                        if (Ts.Seconds >= 20)
                        {
                            throw new MyException.OwnException("这式子要爆炸!");
                        }
                        continue;
                    }
                }
            }
            catch (MyException.OwnException)
            {
                throw new MyException.TooManyException("抱歉,我实在产生不了这么多式子,放过我吧...");
            }
            finally
            {
                FactCount = i;
                ExeToFile(buildExercise, ExePath);
                AnsToFile(buildAnswer, AnsPath);
            }
        }