Exemplo n.º 1
0
        public IEnumerable <MathsExercises> GetExample(int age)
        {
            //  return _context.Exercises.ToArray<MathsExercises>();
            var rng       = new Random();
            int SettingId = rng.Next(1, 101);

            Guid guid = Guid.NewGuid();

            for (int i = 0; i < 5; i++)
            {
                MathsExercises item = new MathsExercises();
                item.SettingId   = SettingId;// guid.ToString();
                item.Formula     = Logic.GetRandomFormula();
                item.UserAnswer  = "";
                item.RightAnswer = CalcParenthesesExpression.CalculateParenthesesExpression(item.Formula);
                item.CreateTime  = DateTime.Now;
                item.SaveTime    = null;

                // {  HashValue = guid.ToString(),Formula = "1+2", Anwser = "", CreateTime = DateTime.Now, SaveTime = null
                _context.Exercises.Add(item);
            }
            _context.SaveChanges();

            return(_context.Exercises.Where(item => item.SettingId == SettingId).ToList());

            // return Enumerable.Range(1, 5).Select(index => new MathsExercise.Models.MathsExercises
            // {
            //     // DateFormatted = DateTime.Now.AddDays(index).ToString("d"),
            //     // TemperatureC = rng.Next(-20, 55),
            //     // Summary = Summaries[rng.Next(Summaries.Length)]
            //     ID = index,
            //     HashValue = index.ToString(),
            //     Formula="1+2",
            //     Anwser ="",
            //     CreateTime = DateTime.Today ,
            //     SaveTime = DateTime.Now

            // });
        }
Exemplo n.º 2
0
        public IEnumerable <VMathsExercise> GetQuestion(string hashvalue, int amount, string operation, int quantityarithmetic, string maxValueType, int maxValue)
        {
            #region Initial paramter
            if (hashvalue.Length != 36)
            {
                // wrong value for hashvalue;
                return(null);
            }
            int           _amount             = 20;
            List <string> operations          = new List <string>();
            int           _quantityarithmetic = 2;
            // ensure quantity is valid data.
            switch (amount)
            {
            case 5:
                _amount = amount;
                break;

            case 10:
                _amount = amount;
                break;

            case 30:
                _amount = amount;
                break;

            case 40:
                _amount = amount;
                break;

            case 50:
                _amount = amount;
                break;

            default:
                _amount = 20;
                break;
            }
            Char[] items = operation.ToLower().ToCharArray();
            foreach (char item in items)
            {
                if (item == 'a')
                {
                    operations.Add("+"); continue;
                }
                if (item == 'b')
                {
                    operations.Add("-"); continue;
                }
                if (item == 'c')
                {
                    operations.Add("*"); continue;
                }
                if (item == 'd')
                {
                    operations.Add("/"); continue;
                }
            }
            //ensure operations has one action at least.
            if (operations.Count <= 0)
            {
                operations.Add("+");
            }
            //ensure quantityarithmetic is between 1 and 5;
            switch (quantityarithmetic)
            {
            case 1:
                _quantityarithmetic = quantityarithmetic;
                break;

            case 3:
                _quantityarithmetic = quantityarithmetic;
                break;

            case 4:
                _quantityarithmetic = quantityarithmetic;
                break;

            case 5:
                _quantityarithmetic = quantityarithmetic;
                break;

            default:
                _quantityarithmetic = 2;
                break;
            }
            //
            string _maxValueType = "2";
            int    _maxValue     = maxValue < 1?20:maxValue;

            Setting setting = new Setting();
            setting.CreateTime          = DateTime.Now;
            setting.Amount              = _amount;
            setting.QuantityOfOperation = _quantityarithmetic;
            setting.MaxValue            = _maxValue;
            setting.GUIDValue           = hashvalue;
            setting.Operations          = "";
            foreach (string item in operations)
            {
                setting.Operations += item;
            }
            // _context.Setting.Add(setting);
            // _context.SaveChanges();
            SettingBll settingBll = new SettingBll(this._context);
            if (settingBll.Insert(setting))
            {
                setting = settingBll.GetOne(hashvalue);
            }
            else
            {
                return(null);
            }

            #endregion
            MathsExercisesBll     exerBll   = new MathsExercisesBll(this._context);
            List <MathsExercises> Exercises = new List <MathsExercises>();
            for (int i = 0; i < _amount; i++)
            {
                // Console.WriteLine("I=" + i.ToString());
                MathsExercises item = new MathsExercises();
                item.SettingId   = setting.ID;
                item.Formula     = Logic.GetRandomFormula(operations, _quantityarithmetic, _maxValueType, _maxValue);
                item.UserAnswer  = "";
                item.RightAnswer = CalcParenthesesExpression.CalculateParenthesesExpression(item.Formula);
                item.CreateTime  = DateTime.Now;
                item.SaveTime    = null;

                // {  HashValue = guid.ToString(),Formula = "1+2", Anwser = "", CreateTime = DateTime.Now, SaveTime = null
                // _context.Exercises.Add(item);
                Exercises.Add(item);
            }
            exerBll.Create(Exercises);


            List <MathsExercises> result = exerBll.GetData(setting.ID);
            List <VMathsExercise> rtn    = new List <VMathsExercise>();
            foreach (MathsExercises question in result)
            {
                VMathsExercise item = new VMathsExercise();
                item.ID          = question.ID;
                item.Formula     = question.Formula;
                item.SettingId   = question.SettingId;
                item.CreateTime  = question.CreateTime;
                item.RightAnswer = question.RightAnswer;
                rtn.Add(item);
            }
            return(rtn);
        }