)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Math (Ia.TentPlay.Cl.Model.Memorise)

Public general use code classes and xml files that we've compiled and used over the years:

Math Class

    1: using System.Data;
    2: using System.Collections;
    3: using System;
    4: using System.Linq;
    5: using System.Collections.Generic;
    6: using System.IO;
    7:  
    8: namespace Ia.TentPlay.Cl.Model.Memorise
    9: {
   10:     ////////////////////////////////////////////////////////////////////////////
   11:  
   12:     /// <summary publish="true">
   13:     /// Math Class
   14:     /// </summary>
   15:     /// <value>
   16:     /// https://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx
   17:     /// </value>
   18:     /// <remarks> 
   19:     /// Copyright © 2008-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   20:     ///
   21:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   22:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   23:     ///
   24:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   25:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   26:     /// 
   27:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   28:     /// 
   29:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   30:     /// </remarks> 
   31:     public class Math : Test
   32:     {
   33:         private Dictionary<string, float> additionDicitonary, subtractionDicitonary, multiplicationDicitonary, divisionDicitonary;
   34:  
   35:         protected override TestTopic testTopic
   36:         {
   37:             get
   38:             {
   39:                 return Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.Math;
   40:             }
   41:         }
   42:  
   43:         ////////////////////////////////////////////////////////////////////////////
   44:  
   45:         /// <summary>
   46:         ///
   47:         /// </summary>
   48:         public enum Operation { Addition, Subtraction, Multiplication, Division };
   49:  
   50:         ////////////////////////////////////////////////////////////////////////////
   51:  
   52:         /// <summary>
   53:         ///
   54:         /// </summary>
   55:         public Math() { }
   56:  
   57:         ////////////////////////////////////////////////////////////////////////////
   58:  
   59:         /// <summary>
   60:         ///
   61:         /// </summary>
   62:         public override void PopulateTestDatabaseTableWithInitialQuestionsIfEmpty(Guid userId)
   63:         {
   64:             int count;
   65:             Ia.TentPlay.Cl.Model.Memorise.Score score;
   66:  
   67:             using (var db = new Ia.TentPlay.Db())
   68:             {
   69:                 count = (from s in db.Scores where s.TestId == (int)Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.Math select s).Count();
   70:  
   71:                 if (count == 0)
   72:                 {
   73:                     // Addition
   74:                     foreach (KeyValuePair<string, float> kvp in AdditionDicitonary)
   75:                     {
   76:                         score = new Ia.TentPlay.Cl.Model.Memorise.Score();
   77:  
   78:                         score.Question = kvp.Key;
   79:                         score.Answer = kvp.Value.ToString();
   80:                         score.TestId = (int)testTopic;
   81:                         score.TypeId = (int)Operation.Addition;
   82:                         score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
   83:                         score.UserId = userId;
   84:  
   85:                         db.Scores.Add(score);
   86:                     }
   87:  
   88:                     // Subtraction
   89:                     foreach (KeyValuePair<string, float> kvp in SubtractionDicitonary)
   90:                     {
   91:                         score = new Ia.TentPlay.Cl.Model.Memorise.Score();
   92:  
   93:                         score.Question = kvp.Key;
   94:                         score.Answer = kvp.Value.ToString();
   95:                         score.TestId = (int)testTopic;
   96:                         score.TypeId = (int)Operation.Subtraction;
   97:                         score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
   98:                         score.UserId = userId;
   99:  
  100:                         db.Scores.Add(score);
  101:                     }
  102:  
  103:                     // Multiplication
  104:                     foreach (KeyValuePair<string, float> kvp in MultiplicationDicitonary)
  105:                     {
  106:                         score = new Ia.TentPlay.Cl.Model.Memorise.Score();
  107:  
  108:                         score.Question = kvp.Key;
  109:                         score.Answer = kvp.Value.ToString();
  110:                         score.TestId = (int)testTopic;
  111:                         score.TypeId = (int)Operation.Multiplication;
  112:                         score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
  113:                         score.UserId = userId;
  114:  
  115:                         db.Scores.Add(score);
  116:                     }
  117:  
  118:                     // Division
  119:                     foreach (KeyValuePair<string, float> kvp in DivisionDicitonary)
  120:                     {
  121:                         score = new Ia.TentPlay.Cl.Model.Memorise.Score();
  122:  
  123:                         score.Question = kvp.Key;
  124:                         score.Answer = kvp.Value.ToString();
  125:                         score.TestId = (int)testTopic;
  126:                         score.TypeId = (int)Operation.Division;
  127:                         score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
  128:                         score.UserId = userId;
  129:  
  130:                         db.Scores.Add(score);
  131:                     }
  132:  
  133:                     db.SaveChanges();
  134:                 }
  135:             }
  136:         }
  137:  
  138:         ////////////////////////////////////////////////////////////////////////////
  139:  
  140:         /// <summary>
  141:         ///
  142:         /// </summary>
  143:         public override int WeightedRandomTypeIdAccordingTypeDistribution(Guid userId)
  144:         {
  145:             int countOfAllTypes, countOfTypeId1, countOfTypeId2, countOfTypeId3, countOfTypeId4, typeId;
  146:             ArrayList countArrayList;
  147:  
  148:             using (var db = new Ia.TentPlay.Db())
  149:             {
  150:                 countOfAllTypes = (from s in db.Scores where s.TestId == (int)testTopic select s).Count();
  151:                 countOfTypeId1 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)Operation.Addition select s).Count();
  152:                 countOfTypeId2 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)Operation.Subtraction select s).Count();
  153:                 countOfTypeId3 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)Operation.Multiplication select s).Count();
  154:                 countOfTypeId4 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)Operation.Division select s).Count();
  155:  
  156:                 countArrayList = new ArrayList(countOfAllTypes);
  157:  
  158:                 // below: populate ArrayList with TypeId keys a number of times equal to its count
  159:                 for (int i = 0; i < countOfTypeId1; i++) countArrayList.Add((int)Operation.Addition);
  160:                 for (int i = 0; i < countOfTypeId2; i++) countArrayList.Add((int)Operation.Subtraction);
  161:                 for (int i = 0; i < countOfTypeId3; i++) countArrayList.Add((int)Operation.Multiplication);
  162:                 for (int i = 0; i < countOfTypeId4; i++) countArrayList.Add((int)Operation.Division);
  163:  
  164:                 typeId = (int)countArrayList[random.Next(countOfAllTypes)];
  165:             }
  166:  
  167:             return typeId;
  168:         }
  169:  
  170:         ////////////////////////////////////////////////////////////////////////////
  171:  
  172:         /// <summary>
  173:         /// 
  174:         /// </summary>
  175:         public Dictionary<string, float> AdditionDicitonary
  176:         {
  177:             get
  178:             {
  179:                 int i, j;
  180:                 float value;
  181:                 string key;
  182:  
  183:                 if (additionDicitonary == null || additionDicitonary.Count == 0)
  184:                 {
  185:                     additionDicitonary = new Dictionary<string, float>();
  186:  
  187:                     for (i = 0; i <= 12; i++)
  188:                     {
  189:                         for (j = 0; j <= 12; j++)
  190:                         {
  191:                             key = i + "+" + j;
  192:                             value = i + j;
  193:  
  194:                             additionDicitonary[key] = value;
  195:                         }
  196:                     }
  197:                 }
  198:  
  199:                 return additionDicitonary;
  200:             }
  201:         }
  202:  
  203:         ////////////////////////////////////////////////////////////////////////////
  204:  
  205:         /// <summary>
  206:         /// 
  207:         /// </summary>
  208:         public Dictionary<string, float> SubtractionDicitonary
  209:         {
  210:             get
  211:             {
  212:                 int i, j;
  213:                 float value;
  214:                 string key;
  215:  
  216:                 if (subtractionDicitonary == null || subtractionDicitonary.Count == 0)
  217:                 {
  218:                     subtractionDicitonary = new Dictionary<string, float>();
  219:  
  220:                     for (i = 0; i <= 12; i++)
  221:                     {
  222:                         for (j = 0; j <= 12; j++)
  223:                         {
  224:                             key = i + "-" + j;
  225:                             value = i - j;
  226:  
  227:                             subtractionDicitonary[key] = value;
  228:                         }
  229:                     }
  230:                 }
  231:  
  232:                 return subtractionDicitonary;
  233:             }
  234:         }
  235:  
  236:         ////////////////////////////////////////////////////////////////////////////
  237:  
  238:         /// <summary>
  239:         /// 
  240:         /// </summary>
  241:         public Dictionary<string, float> MultiplicationDicitonary
  242:         {
  243:             get
  244:             {
  245:                 int i, j;
  246:                 float value;
  247:                 string key;
  248:  
  249:                 if (multiplicationDicitonary == null || multiplicationDicitonary.Count == 0)
  250:                 {
  251:                     multiplicationDicitonary = new Dictionary<string, float>();
  252:  
  253:                     for (i = 0; i <= 12; i++)
  254:                     {
  255:                         for (j = 0; j <= 12; j++)
  256:                         {
  257:                             key = i + "*" + j;
  258:                             value = i * j;
  259:  
  260:                             multiplicationDicitonary[key] = value;
  261:                         }
  262:                     }
  263:                 }
  264:  
  265:                 return multiplicationDicitonary;
  266:             }
  267:         }
  268:  
  269:         ////////////////////////////////////////////////////////////////////////////
  270:  
  271:         /// <summary>
  272:         /// 
  273:         /// </summary>
  274:         public Dictionary<string, float> DivisionDicitonary
  275:         {
  276:             get
  277:             {
  278:                 int i, j;
  279:                 float value;
  280:                 string key;
  281:  
  282:                 if (divisionDicitonary == null || divisionDicitonary.Count == 0)
  283:                 {
  284:                     divisionDicitonary = new Dictionary<string, float>();
  285:  
  286:                     for (i = 0; i <= 12; i++)
  287:                     {
  288:                         for (j = 1; j <= 12; j++)
  289:                         {
  290:                             key = i + "/" + j;
  291:                             value = i / j;
  292:  
  293:                             divisionDicitonary[key] = value;
  294:                         }
  295:                     }
  296:                 }
  297:  
  298:                 return divisionDicitonary;
  299:             }
  300:         }
  301:  
  302:         ////////////////////////////////////////////////////////////////////////////
  303:         ////////////////////////////////////////////////////////////////////////////    
  304:     }
  305:  
  306:     ////////////////////////////////////////////////////////////////////////////
  307:     ////////////////////////////////////////////////////////////////////////////   
  308: }