├── .gitignore ├── CalculateIt2.sln ├── LICENSE ├── README.md ├── docs └── ClassDiagram.vsdx ├── src └── CalculateIt2.Engine │ ├── CalculateIt2.Engine.csproj │ ├── CalculateIt2.Engine.dll.nuspec │ ├── Calculation.cs │ ├── CalculationVisitor.cs │ ├── CompositeCalculation.cs │ ├── ConstantCalculation.cs │ ├── Extensions.cs │ ├── Generation │ ├── ArithmeticEquationGenerator.cs │ ├── ClozeQuestionGenerator.cs │ ├── ComparisonQuestionGenerator.cs │ ├── EquationGenerator.cs │ ├── QuestionGenerationResult.cs │ ├── QuestionGenerator.cs │ └── RegularQuestionGenerator.cs │ ├── IVisitor.cs │ ├── IVisitorAcceptor.cs │ ├── Operator.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── Rules │ ├── AvoidDivideByZeroRule.cs │ ├── AvoidNegativeResultRule.cs │ ├── ConstantCalculationCounter.cs │ ├── DivisibilityEnsuranceRule.cs │ ├── IRule.cs │ └── RandomizedCalculationValueAdjustment.cs │ ├── SpacingOption.cs │ ├── Utils.cs │ └── key.snk └── tests └── CalculateIt2.Tests ├── CalculateIt2.Tests.csproj ├── EquationFormationTests.cs ├── Properties └── AssemblyInfo.cs └── packages.config /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | -------------------------------------------------------------------------------- /CalculateIt2.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CalculateIt2.Engine", "src\CalculateIt2.Engine\CalculateIt2.Engine.csproj", "{B3F3898B-7C5C-4CAE-8A68-1C69E23A9C3C}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CalculateIt2.Tests", "tests\CalculateIt2.Tests\CalculateIt2.Tests.csproj", "{85CB7B9E-227F-4898-A161-5935CD9142B3}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {B3F3898B-7C5C-4CAE-8A68-1C69E23A9C3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {B3F3898B-7C5C-4CAE-8A68-1C69E23A9C3C}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {B3F3898B-7C5C-4CAE-8A68-1C69E23A9C3C}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {B3F3898B-7C5C-4CAE-8A68-1C69E23A9C3C}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {85CB7B9E-227F-4898-A161-5935CD9142B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {85CB7B9E-227F-4898-A161-5935CD9142B3}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {85CB7B9E-227F-4898-A161-5935CD9142B3}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {85CB7B9E-227F-4898-A161-5935CD9142B3}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Sunny Chen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CalculateIt2 2 | A calculation generation engine for students. 3 | -------------------------------------------------------------------------------- /docs/ClassDiagram.vsdx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daxnet/CalculateIt2/344c9080235f680cd28bf6c4958c2f87b8c92cbc/docs/ClassDiagram.vsdx -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/CalculateIt2.Engine.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {B3F3898B-7C5C-4CAE-8A68-1C69E23A9C3C} 8 | Library 9 | Properties 10 | CalculateIt2.Engine 11 | CalculateIt2.Engine 12 | v4.6.1 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | ..\..\bin\ 28 | TRACE 29 | prompt 30 | 4 31 | bin\Release\CalculateIt2.Engine.XML 32 | 33 | 34 | true 35 | 36 | 37 | key.snk 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | Always 78 | 79 | 80 | 81 | 82 | 89 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/CalculateIt2.Engine.dll.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | CalculateIt2.Engine 5 | 1.0.0 6 | daxnet 7 | daxnet 8 | https://github.com/daxnet/CalculateIt2/blob/master/LICENSE 9 | https://github.com/daxnet/CalculateIt2 10 | true 11 | The core components for generating arithmetic equations and arithmetic questions. 12 | Copyright 2016 by daxnet 13 | Calculation Equation Generator 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Calculation.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using System; 38 | 39 | namespace CalculateIt2.Engine 40 | { 41 | /// 42 | /// Represents an arithmetic calculation. 43 | /// 44 | /// 45 | public abstract class Calculation : IVisitorAcceptor 46 | { 47 | #region Public Properties 48 | /// 49 | /// Gets the value of the current calculation. 50 | /// 51 | /// 52 | /// The value of the calculation. 53 | /// 54 | public abstract long Value { get; } 55 | #endregion 56 | 57 | #region Public Methods 58 | /// 59 | /// Accepts the specified visitor. 60 | /// 61 | /// The visitor instance to be accepted. 62 | public abstract void Accept(IVisitor visitor); 63 | 64 | /// 65 | /// Returns a more human-readable string that represents the current calculation instance. 66 | /// 67 | /// The spacing option. 68 | /// A string with better readability. 69 | /// 70 | public abstract string ToFormattedString(SpacingOption option = SpacingOption.None); 71 | 72 | /// 73 | /// Merges the specified calculations with a given 74 | /// 75 | /// The left side of the calculation. 76 | /// The right side of the calculation. 77 | /// The operator to merge the two operations. 78 | /// The merged operation. 79 | /// Both left and right calculations are empty. 80 | public static Calculation Merge(Calculation left, Calculation right, Operator @operator) 81 | { 82 | if (left == null && right == null) 83 | { 84 | throw new ArgumentNullException("Both left and right calculations are empty."); 85 | } 86 | if (left == null) 87 | { 88 | return right; 89 | } 90 | if (right == null) 91 | { 92 | return left; 93 | } 94 | 95 | return new CompositeCalculation(left, right, @operator); 96 | } 97 | #endregion 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/CalculationVisitor.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | namespace CalculateIt2.Engine 38 | { 39 | /// 40 | /// Represents the visitor that can visit each node of a calculation. 41 | /// 42 | /// 43 | public abstract class CalculationVisitor : IVisitor 44 | { 45 | #region Public Methods 46 | /// 47 | /// Visits the given object as an acceptor. 48 | /// 49 | /// The object being visited. 50 | public void Visit(IVisitorAcceptor acceptor) 51 | { 52 | if (acceptor is ConstantCalculation) 53 | { 54 | this.VisitConstantCalculation(acceptor as ConstantCalculation); 55 | } 56 | if (acceptor is CompositeCalculation) 57 | { 58 | this.VisitCompositeCalculation(acceptor as CompositeCalculation); 59 | } 60 | } 61 | #endregion 62 | 63 | #region Protected Methods 64 | /// 65 | /// Visits the constant calculation. 66 | /// 67 | /// The constant calculation. 68 | protected virtual void VisitConstantCalculation(ConstantCalculation constantCalculation) { } 69 | 70 | /// 71 | /// Visits the composite calculation. 72 | /// 73 | /// The composite calculation. 74 | protected virtual void VisitCompositeCalculation(CompositeCalculation compositeCalculation) { } 75 | #endregion 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/CompositeCalculation.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | namespace CalculateIt2.Engine 38 | { 39 | public class CompositeCalculation : Calculation 40 | { 41 | #region Ctor 42 | /// 43 | /// Initializes a new instance of the class. 44 | /// 45 | /// The left hand side of the calculation. 46 | /// The right hand side of the calculation. 47 | /// The arithmetic operator to be used. 48 | public CompositeCalculation(Calculation left, Calculation right, Operator @operator) 49 | { 50 | this.Left = left; 51 | this.Right = right; 52 | this.Operator = @operator; 53 | } 54 | #endregion 55 | 56 | #region Public Properties 57 | /// 58 | /// Gets the left hand side of the calculation. 59 | /// 60 | /// 61 | /// The left part of the calculation. 62 | /// 63 | public Calculation Left { get; } 64 | /// 65 | /// Gets the right hand side of the calculation. 66 | /// 67 | /// 68 | /// The right part of the calculation. 69 | /// 70 | public Calculation Right { get; } 71 | /// 72 | /// Gets the arithmetic operator. 73 | /// 74 | /// 75 | /// The arithmetic operator. 76 | /// 77 | public Operator Operator { get; } 78 | /// 79 | /// Gets the value of the current calculation. 80 | /// 81 | /// 82 | /// The value of the calculation. 83 | /// 84 | public override long Value 85 | { 86 | get 87 | { 88 | switch(Operator) 89 | { 90 | case Operator.Add: 91 | return Left.Value + Right.Value; 92 | case Operator.Sub: 93 | return Left.Value - Right.Value; 94 | case Operator.Mul: 95 | return Left.Value * Right.Value; 96 | case Operator.Div: 97 | return Left.Value / Right.Value; 98 | default: 99 | return long.MinValue; 100 | } 101 | } 102 | } 103 | #endregion 104 | 105 | #region Public Methods 106 | /// 107 | /// Accepts the specified visitor. 108 | /// 109 | /// The visitor instance to be accepted. 110 | public override void Accept(IVisitor visitor) 111 | { 112 | Left.Accept(visitor); 113 | visitor.Visit(this); 114 | Right.Accept(visitor); 115 | } 116 | 117 | /// 118 | /// Returns a that represents this instance. 119 | /// 120 | /// 121 | /// A that represents this instance. 122 | /// 123 | public override string ToString() 124 | { 125 | string operatorSign = null; 126 | switch(Operator) 127 | { 128 | case Operator.Add: 129 | operatorSign = "+"; 130 | break; 131 | case Operator.Sub: 132 | operatorSign = "-"; 133 | break; 134 | case Operator.Mul: 135 | operatorSign = "*"; 136 | break; 137 | case Operator.Div: 138 | operatorSign = "/"; 139 | break; 140 | } 141 | if (Left is CompositeCalculation && 142 | !(Right is CompositeCalculation) && 143 | OperatorPrecedence((Left as CompositeCalculation).Operator) < OperatorPrecedence(this.Operator)) 144 | { 145 | return $"({Left}){operatorSign}{Right}"; 146 | } 147 | 148 | if (!(Left is CompositeCalculation) && 149 | Right is CompositeCalculation && 150 | OperatorPrecedence((Right as CompositeCalculation).Operator) < OperatorPrecedence(this.Operator)) 151 | { 152 | return $"{Left}{operatorSign}({Right})"; 153 | } 154 | 155 | if (Left is CompositeCalculation && Right is CompositeCalculation) 156 | { 157 | return $"({Left}){operatorSign}({Right})"; 158 | } 159 | 160 | return $"{Left}{operatorSign}{Right}"; 161 | } 162 | 163 | /// 164 | /// Returns a more human-readable string that represents the current calculation instance. 165 | /// 166 | /// The spacing option. 167 | /// 168 | /// A string with better readability. 169 | /// 170 | /// 171 | public override string ToFormattedString(SpacingOption option = SpacingOption.None) 172 | { 173 | string operatorSign = null; 174 | string spacing = ""; 175 | switch (option) 176 | { 177 | case SpacingOption.Thin: 178 | spacing = " "; 179 | break; 180 | case SpacingOption.Thick: 181 | spacing = " "; 182 | break; 183 | } 184 | switch (Operator) 185 | { 186 | case Operator.Add: 187 | operatorSign = $"{spacing}+{spacing}"; 188 | break; 189 | case Operator.Sub: 190 | operatorSign = $"{spacing}-{spacing}"; 191 | break; 192 | case Operator.Mul: 193 | operatorSign = $"{spacing}×{spacing}"; 194 | break; 195 | case Operator.Div: 196 | operatorSign = $"{spacing}÷{spacing}"; 197 | break; 198 | } 199 | if (Left is CompositeCalculation && 200 | !(Right is CompositeCalculation) && 201 | OperatorPrecedence((Left as CompositeCalculation).Operator) < OperatorPrecedence(this.Operator)) 202 | { 203 | return $"({Left.ToFormattedString(option)}){operatorSign}{Right.ToFormattedString(option)}"; 204 | } 205 | 206 | if (!(Left is CompositeCalculation) && 207 | Right is CompositeCalculation && 208 | OperatorPrecedence((Right as CompositeCalculation).Operator) < OperatorPrecedence(this.Operator)) 209 | { 210 | return $"{Left.ToFormattedString(option)}{operatorSign}({Right.ToFormattedString(option)})"; 211 | } 212 | 213 | if (Left is CompositeCalculation && Right is CompositeCalculation) 214 | { 215 | return $"({Left.ToFormattedString(option)}){operatorSign}({Right.ToFormattedString(option)})"; 216 | } 217 | 218 | return $"{Left.ToFormattedString(option)}{operatorSign}{Right.ToFormattedString(option)}"; 219 | } 220 | #endregion 221 | 222 | #region Private Methods 223 | private static int OperatorPrecedence(Operator op) 224 | { 225 | return op == Operator.Add || op == Operator.Sub ? 1 : 2; 226 | } 227 | #endregion 228 | } 229 | } 230 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/ConstantCalculation.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | 38 | namespace CalculateIt2.Engine 39 | { 40 | /// 41 | /// Represents a constant calculation that has a given object as its value. 42 | /// 43 | /// 44 | public sealed class ConstantCalculation : Calculation 45 | { 46 | #region Private Fields 47 | private long value; 48 | #endregion 49 | 50 | #region Ctor 51 | /// 52 | /// Initializes a new instance of the class. 53 | /// 54 | /// The value to be used for initializing this constant calculation. 55 | public ConstantCalculation(long value) 56 | { 57 | this.value = value; 58 | } 59 | #endregion 60 | 61 | #region Public Properties 62 | /// 63 | /// Gets the value of the current calculation. 64 | /// 65 | /// 66 | /// The value of the calculation. 67 | /// 68 | public override long Value 69 | { 70 | get { return this.value; } 71 | } 72 | #endregion 73 | 74 | #region Public Methods 75 | /// 76 | /// Accepts the specified visitor. 77 | /// 78 | /// The visitor instance to be accepted. 79 | public override void Accept(IVisitor visitor) => visitor.Visit(this); 80 | 81 | /// 82 | /// Returns a string that represents the current object. 83 | /// 84 | /// A string that represents the current object. 85 | public override string ToString() => this.Value.ToString(); 86 | 87 | /// 88 | /// Returns a more human-readable string that represents the current calculation instance. 89 | /// 90 | /// The spacing option. 91 | /// A string with better readability. 92 | /// 93 | public override string ToFormattedString(SpacingOption option = SpacingOption.None) => this.Value.ToString(); 94 | 95 | /// 96 | /// Operator override for implicitly converts the given value into a constant calculation. 97 | /// 98 | /// The value to be converted. 99 | public static implicit operator ConstantCalculation(long x) => new ConstantCalculation(x); 100 | 101 | /// 102 | /// Operator override for implicitly converts the given into a value. 103 | /// 104 | /// The to be converted. 105 | public static implicit operator long (ConstantCalculation c) => c.Value; 106 | #endregion 107 | 108 | #region Internal Methods 109 | /// 110 | /// Explicitly sets the value of current ConstantCalculation instance. 111 | /// 112 | /// The value to be set to this instance. 113 | internal void SetValue(long value) 114 | { 115 | this.value = value; 116 | } 117 | #endregion 118 | 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Extensions.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | 38 | namespace CalculateIt2.Engine 39 | { 40 | /// 41 | /// Represents the class that holds all the extension methods. 42 | /// 43 | public static class Extensions 44 | { 45 | /// 46 | /// Converts a value to the calculation instance. 47 | /// 48 | /// The value to be converted. 49 | /// 50 | public static Calculation ToCalculation(this long i) 51 | { 52 | return new ConstantCalculation(i); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Generation/ArithmeticEquationGenerator.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using CalculateIt2.Engine.Rules; 38 | using System; 39 | using System.Collections.Generic; 40 | 41 | namespace CalculateIt2.Engine.Generation 42 | { 43 | /// 44 | /// Represents the arithmetic equation generator. 45 | /// 46 | /// 47 | public sealed class ArithmeticEquationGenerator : EquationGenerator 48 | { 49 | #region Private Fields 50 | private int max; 51 | private string acceptableOperators; 52 | private int numOfFactors_min; 53 | private int numOfFactors_max; 54 | private readonly Random rnd = new Random(DateTime.Now.Millisecond); 55 | #endregion 56 | 57 | #region Ctor 58 | /// 59 | /// Initializes a new instance of the class. 60 | /// 61 | /// The formation of the equation that is going to be generated. 62 | /// A list of instances that is registered with current equation generator. 63 | public ArithmeticEquationGenerator(string formation, params IRule[] rules) 64 | : base(formation, rules) 65 | { 66 | } 67 | #endregion 68 | 69 | #region Public Methods 70 | /// 71 | /// Generates the calculation based on the given formation. 72 | /// 73 | /// 74 | /// The being generated. 75 | /// 76 | /// Cannot generate the equation: the given equation generation formation is not valid, please see ErrorMessages property for details. 77 | public override Calculation Generate() 78 | { 79 | if (!this.IsValid) 80 | { 81 | throw new InvalidOperationException("Cannot generate the equation: the given equation generation formation is not valid, please see ErrorMessages property for details."); 82 | } 83 | Calculation result = null; 84 | var numOfFactors = numOfFactors_min; 85 | if (numOfFactors_max != 0) 86 | { 87 | numOfFactors = rnd.Next(numOfFactors_min, numOfFactors_max + 1); 88 | } 89 | 90 | for (var idx = 0; idx < numOfFactors; idx++) 91 | { 92 | long factor = rnd.Next(max + 1); 93 | var @operator = Utils.GenerateRandomOperator(this.acceptableOperators); 94 | Calculation left = result, right = new ConstantCalculation(factor); 95 | 96 | if (@operator == Operator.Add || @operator == Operator.Mul) 97 | { 98 | var seed = rnd.Next(DateTime.Now.Millisecond); 99 | if ((seed % 2) == 0) 100 | { 101 | left = new ConstantCalculation(factor); 102 | right = result; 103 | } 104 | } 105 | 106 | if (this.rules != null) 107 | { 108 | foreach (var rule in rules) 109 | { 110 | rule.Apply(left, right, Parameters, ref @operator); 111 | } 112 | } 113 | 114 | result = Calculation.Merge(left, right, @operator); 115 | } 116 | return result; 117 | } 118 | #endregion 119 | 120 | #region Protected Properties 121 | /// 122 | /// Gets a value which represents the regular expression pattern of the formation. 123 | /// 124 | /// 125 | /// The formation pattern. 126 | /// 127 | protected override string FormationPattern => @"^{(?\d+)}(?(\+)?(\-)?(\*)?(\/)?){1}(\|(?\d+)(-(?\d+))?)?$"; 128 | #endregion 129 | 130 | #region Protected Methods 131 | /// 132 | /// Validates the parameters that are extracted from the given formation. 133 | /// 134 | /// The parameters to be validated. 135 | /// 136 | /// true if all the parameters are valid, otherwise, false. 137 | /// 138 | protected override bool ValidateParameters(IDictionary parameters) 139 | { 140 | max = Convert.ToInt32(parameters["max"]); 141 | if (max <= 0) 142 | { 143 | errorMessages.Add("Proposed maximum value should be larger than zero."); 144 | } 145 | 146 | acceptableOperators = parameters["operator"]; 147 | if (!int.TryParse(parameters["factors_min"], out numOfFactors_min)) 148 | { 149 | numOfFactors_min = 2; 150 | } 151 | 152 | if (!int.TryParse(parameters["factors_max"], out numOfFactors_max)) 153 | { 154 | numOfFactors_max = 0; 155 | } 156 | 157 | if (numOfFactors_max != 0 && numOfFactors_min > numOfFactors_max) 158 | { 159 | errorMessages.Add("Maximum number of factors should be larger than or equal to the minimum value."); 160 | } 161 | 162 | if (string.IsNullOrEmpty(acceptableOperators)) 163 | { 164 | errorMessages.Add("No acceptable operator has been specified."); 165 | } 166 | 167 | return errorMessages.Count == 0; 168 | } 169 | #endregion 170 | 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Generation/ClozeQuestionGenerator.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using System; 38 | using System.Text.RegularExpressions; 39 | 40 | namespace CalculateIt2.Engine.Generation 41 | { 42 | /// 43 | /// Represents the question generator that generates the cloze questions. 44 | /// 45 | /// 46 | /// Cloze questions are the questions that leaves a place holder in an arithmetic equation, requiring 47 | /// the student to fill in a number so that the equation can balance. For example, given a calculation: 48 | /// 49 | /// 2 + ( ) = 5 50 | /// 51 | /// The student should fill 3 into the parenthesis. 52 | /// 53 | /// The CloseQuestionGenerator will generate the string '2 + ( ) = 5' as the formula, and number 3 is also 54 | /// provided in the generated result. 55 | /// 56 | /// 57 | public sealed class ClozeQuestionGenerator : QuestionGenerator 58 | { 59 | #region Private Fields 60 | private const string DigitalPattern = @"\d+"; 61 | private readonly Regex regex = new Regex(DigitalPattern); 62 | private readonly Random rnd = new Random(DateTime.Now.Millisecond); 63 | #endregion 64 | 65 | #region Ctor 66 | /// 67 | /// Initializes a new instance of the class. 68 | /// 69 | /// The place holder of the question where the students should put the answer in. 70 | /// The value which indicates the spacing options of the generated question. 71 | public ClozeQuestionGenerator(string placeHolder = "( )", SpacingOption spacingOption = SpacingOption.Thin) : base(placeHolder, spacingOption) 72 | { 73 | } 74 | #endregion 75 | 76 | #region Public Methods 77 | /// 78 | /// Generates the arithmetic question based on the given calculation. 79 | /// 80 | /// The calculation equation from which the question is generated. 81 | /// A instance which contains the question formular and the answer. 82 | public override QuestionGenerationResult Generate(Calculation calculation) 83 | { 84 | var calculationString = calculation.ToFormattedString(this.SpacingOption); 85 | var digitalMatches = regex.Matches(calculationString); 86 | var matchesArray = new Match[digitalMatches.Count]; 87 | digitalMatches.CopyTo(matchesArray, 0); 88 | var idx = rnd.Next(matchesArray.Length); 89 | var selectedIndex = matchesArray[idx].Index; 90 | var selectedValue = Convert.ToInt64(matchesArray[idx].Value); 91 | 92 | var formula = $@"{regex.Replace(calculationString, match => 93 | { 94 | if (match.Index == selectedIndex) 95 | { 96 | return PlaceHolder; 97 | } 98 | return match.Value; 99 | })} = {calculation.Value}"; 100 | 101 | return new QuestionGenerationResult(formula, selectedValue); 102 | } 103 | #endregion 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Generation/ComparisonQuestionGenerator.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using System; 38 | 39 | namespace CalculateIt2.Engine.Generation 40 | { 41 | /// 42 | /// Represents the question generator that generates the comparison questions. 43 | /// 44 | /// 45 | /// A comparison question is the one that, it will give the student a calculation, as the left 46 | /// part of the equation, and then propose a value which might be less than, greater than or 47 | /// equal to the value of the calculation. The student should use < > and = signs to make 48 | /// the equation reasonable. 49 | /// 50 | /// For example: 51 | /// 52 | /// 2 + 3 ○ 6 53 | /// 54 | /// The students should fill < sign in the circle. 55 | /// 56 | /// 57 | public sealed class ComparisonQuestionGenerator : QuestionGenerator 58 | { 59 | #region Private Fields 60 | private readonly int threshold; 61 | private readonly Random rnd = new Random(DateTime.Now.Millisecond); 62 | #endregion 63 | 64 | #region Ctor 65 | /// 66 | /// Initializes a new instance of the class. 67 | /// 68 | /// The threshold. 69 | /// The place holder. 70 | /// The spacing option. 71 | public ComparisonQuestionGenerator(int threshold, string placeHolder = "\u25CB", SpacingOption spacingOption = SpacingOption.Thin) 72 | : base(placeHolder, spacingOption) 73 | { 74 | this.threshold = threshold; 75 | } 76 | #endregion 77 | 78 | #region Public Methods 79 | /// 80 | /// Generates the arithmetic question based on the given calculation. 81 | /// 82 | /// The calculation equation from which the question is generated. 83 | /// A instance which contains the question formular and the answer. 84 | public override QuestionGenerationResult Generate(Calculation calculation) 85 | { 86 | var value = calculation.Value; 87 | var min = value > threshold ? value - threshold : 0; 88 | var max = value + threshold + 1; 89 | 90 | var v = rnd.Next(Convert.ToInt32(min), Convert.ToInt32(max)); 91 | 92 | return new QuestionGenerationResult($"{calculation.ToFormattedString(this.SpacingOption)} {this.PlaceHolder} {v}", 93 | value == v ? "=" : 94 | ( 95 | value > v ? ">" : "<" 96 | )); 97 | } 98 | #endregion 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Generation/EquationGenerator.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using System.Linq; 38 | using CalculateIt2.Engine.Rules; 39 | using System.Collections.Generic; 40 | using System.Text.RegularExpressions; 41 | 42 | namespace CalculateIt2.Engine.Generation 43 | { 44 | /// 45 | /// Represents the base class for the equation generators. 46 | /// 47 | public abstract class EquationGenerator 48 | { 49 | #region Fields 50 | private readonly Dictionary parameters = new Dictionary(); 51 | 52 | /// 53 | /// The local list for storing the error message texts. 54 | /// 55 | protected readonly List errorMessages = new List(); 56 | 57 | /// 58 | /// The local list which contains all the registered rules. 59 | /// 60 | protected readonly List rules = new List(); 61 | #endregion 62 | 63 | #region Ctor 64 | /// 65 | /// Initializes a new instance of EquationGenerator class. 66 | /// 67 | /// The formation of the equation that is going to be generated. 68 | /// A list of instances that is registered with current equation generator. 69 | public EquationGenerator(string formation, params IRule[] rules) 70 | { 71 | // Adds the AvoidDivideByZeroRule, as it is a mandatory for a arithmetic calculation. 72 | this.rules.Add(new AvoidDivideByZeroRule()); 73 | 74 | // Load additional rules and register to the current generator instance. 75 | if (rules != null) 76 | { 77 | foreach(var rule in rules) 78 | { 79 | if (this.rules.Any(r => r.GetType() == rule.GetType())) continue; 80 | this.rules.Add(rule); 81 | } 82 | } 83 | 84 | this.Formation = formation; 85 | errorMessages.Clear(); 86 | 87 | // Firstly use the regular expression to validate the given formation. 88 | var regex = new Regex(this.FormationPattern); 89 | var match = regex.Match(this.Formation); 90 | IsValid = match.Success; 91 | if (IsValid) 92 | { 93 | // If the given formation can pass the regular expression matching, the matching groups 94 | // will be extracted from the formation, and be stored into the local dictionary as parameters. 95 | foreach (var groupName in regex.GetGroupNames()) 96 | { 97 | parameters.Add(groupName, match.Groups[groupName].Value); 98 | } 99 | 100 | // Passing the parameters to the ValidateParamters method for additional validation. For example, 101 | // check if the parameters are valid. 102 | IsValid = ValidateParameters(parameters); 103 | } 104 | else 105 | { 106 | // If the given formation cannot pass the regular expression matching, an error 107 | // message will be added to the local list. 108 | errorMessages.Add("The formation passed in cannot match the required pattern criteria."); 109 | } 110 | } 111 | #endregion 112 | 113 | #region Public Properties 114 | /// 115 | /// Gets a value which indicates whether the current EquationGenerator 116 | /// is in a valid state. 117 | /// 118 | /// 119 | /// true if the current EquationGenerator is in a valid state; otherwise, false. 120 | /// 121 | public bool IsValid { get; } 122 | 123 | /// 124 | /// Gets the formation of the equations being generated. 125 | /// 126 | /// 127 | /// The formation of the equations being generated. 128 | /// 129 | public string Formation { get; } 130 | 131 | /// 132 | /// Gets the error messages being generated when doing the generator validation. 133 | /// 134 | /// 135 | /// The error messages. 136 | /// 137 | public IEnumerable ErrorMessages => errorMessages; 138 | #endregion 139 | 140 | #region Protected Properties 141 | /// 142 | /// Gets a value which represents the regular expression pattern of the formation. 143 | /// 144 | /// 145 | /// The formation pattern. 146 | /// 147 | protected abstract string FormationPattern { get; } 148 | 149 | /// 150 | /// Gets the parameters that are extracted from the given formation. 151 | /// 152 | /// 153 | /// The parameters. 154 | /// 155 | protected IDictionary Parameters => parameters; 156 | #endregion 157 | 158 | #region Public Methods 159 | /// 160 | /// Generates the calculation based on the given formation. 161 | /// 162 | /// The being generated. 163 | public abstract Calculation Generate(); 164 | #endregion 165 | 166 | #region Protected Methods 167 | /// 168 | /// Validates the parameters that are extracted from the given formation. 169 | /// 170 | /// The parameters to be validated. 171 | /// true if all the parameters are valid, otherwise, false. 172 | protected virtual bool ValidateParameters(IDictionary parameters) => true; 173 | #endregion 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Generation/QuestionGenerationResult.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | 38 | namespace CalculateIt2.Engine.Generation 39 | { 40 | /// 41 | /// Represents the result of a question generation. 42 | /// 43 | /// The type of the answer to the generated question. 44 | public sealed class QuestionGenerationResult 45 | { 46 | #region Private Fields 47 | private readonly string formula; 48 | private readonly TAnswer answer; 49 | #endregion 50 | 51 | #region Ctor 52 | /// 53 | /// Initializes a new instance of the class. 54 | /// 55 | /// The presentation of the formula that the question generator has generated. 56 | /// The answer to the generated question. 57 | internal QuestionGenerationResult(string formula, TAnswer answer) 58 | { 59 | this.formula = formula; 60 | this.answer = answer; 61 | } 62 | #endregion 63 | 64 | #region Public Properties 65 | /// 66 | /// Gets the presentation of the formula that the question generator has generated. 67 | /// 68 | /// 69 | /// The presentation of the formula that the question generator has generated. 70 | /// 71 | public string Formula => formula; 72 | 73 | /// 74 | /// Gets the answer to the generated question. 75 | /// 76 | /// 77 | /// The answer to the generated question. 78 | /// 79 | public TAnswer Answer => answer; 80 | #endregion 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Generation/QuestionGenerator.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | namespace CalculateIt2.Engine.Generation 38 | { 39 | /// 40 | /// Represents the base class of question generators. 41 | /// 42 | public abstract class QuestionGenerator 43 | { 44 | #region Private Fields 45 | private readonly string placeHolder; 46 | private readonly SpacingOption spacingOption; 47 | #endregion 48 | 49 | #region Ctor 50 | /// 51 | /// Initializes a new instance of the class. 52 | /// 53 | /// The place holder of the question where the students should put the answer in. 54 | /// The value which indicates the spacing options of the generated question. 55 | protected QuestionGenerator(string placeHolder, SpacingOption spacingOption) 56 | { 57 | this.placeHolder = placeHolder; 58 | this.spacingOption = spacingOption; 59 | } 60 | #endregion 61 | 62 | #region Public Methods 63 | /// 64 | /// Generates the arithmetic question based on the given calculation. 65 | /// 66 | /// The calculation equation from which the question is generated. 67 | /// A instance which contains the question formular and the answer. 68 | public abstract QuestionGenerationResult Generate(Calculation calculation); 69 | #endregion 70 | 71 | #region Protected Properties 72 | /// 73 | /// Gets the place holder of the question where the students should put the answer in. 74 | /// 75 | /// 76 | /// The place holder of the question where the students should put the answer in. 77 | /// 78 | protected string PlaceHolder => placeHolder; 79 | 80 | /// 81 | /// Gets the value which indicates the spacing options of the generated question. 82 | /// 83 | /// 84 | /// The value which indicates the spacing options of the generated question. 85 | /// 86 | protected SpacingOption SpacingOption => spacingOption; 87 | #endregion 88 | 89 | 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Generation/RegularQuestionGenerator.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | namespace CalculateIt2.Engine.Generation 38 | { 39 | /// 40 | /// Represents the regular question generator. 41 | /// 42 | /// 43 | /// This question generator simply generates a regular arithmetic equation question. For example: 44 | /// 45 | /// 2 + 3 = ____ 46 | /// 47 | /// The students should fll in 5 in the place holder. 48 | /// 49 | /// 50 | public sealed class RegularQuestionGenerator : QuestionGenerator 51 | { 52 | #region Ctor 53 | /// 54 | /// Initializes a new instance of the class. 55 | /// 56 | /// The place holder of the question where the students should put the answer in. 57 | /// The value which indicates the spacing options of the generated question. 58 | public RegularQuestionGenerator(string placeHolder = "____", SpacingOption spacingOption = SpacingOption.Thin) 59 | : base(placeHolder, spacingOption) 60 | { 61 | } 62 | #endregion 63 | 64 | #region Public Methods 65 | /// 66 | /// Generates the arithmetic question based on the given calculation. 67 | /// 68 | /// The calculation equation from which the question is generated. 69 | /// A instance which contains the question formular and the answer. 70 | public override QuestionGenerationResult Generate(Calculation calculation) 71 | { 72 | return new QuestionGenerationResult($"{calculation.ToFormattedString(this.SpacingOption)} = {PlaceHolder}", calculation.Value); 73 | } 74 | #endregion 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/IVisitor.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | namespace CalculateIt2.Engine 38 | { 39 | /// 40 | /// Represents that the implemented classes are object hierarchy visitors. 41 | /// 42 | public interface IVisitor 43 | { 44 | /// 45 | /// Visits the given object as an acceptor. 46 | /// 47 | /// The object being visited. 48 | void Visit(IVisitorAcceptor acceptor); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/IVisitorAcceptor.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | namespace CalculateIt2.Engine 38 | { 39 | /// 40 | /// Represents that the implemented classes are visitor acceptors. 41 | /// 42 | public interface IVisitorAcceptor 43 | { 44 | /// 45 | /// Accepts the specified visitor. 46 | /// 47 | /// The visitor instance to be accepted. 48 | void Accept(IVisitor visitor); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Operator.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using System; 38 | 39 | namespace CalculateIt2.Engine 40 | { 41 | /// 42 | /// Represents the arithmetic calculation operators. 43 | /// 44 | [Flags] 45 | public enum Operator 46 | { 47 | /// 48 | /// Indicates that the operator has not been defined. 49 | /// 50 | None = 0, 51 | 52 | /// 53 | /// Indicates the Add (+) operator. 54 | /// 55 | Add = 1, 56 | 57 | /// 58 | /// Indicates the Subtraction (-) operator. 59 | /// 60 | Sub = 2, 61 | 62 | /// 63 | /// Indicates the Multiplicity (*) operator. 64 | /// 65 | Mul = 4, 66 | 67 | /// 68 | /// Indicates the Division (/) operator. 69 | /// 70 | Div = 8 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("CalculateIt2.Engine")] 9 | [assembly: AssemblyDescription("The core components for generating arithmetic equations and arithmetic questions.")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("daxnet")] 12 | [assembly: AssemblyProduct("CalculateIt2")] 13 | [assembly: AssemblyCopyright("Copyright © 2016 by daxnet.")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("b3f3898b-7c5c-4cae-8a68-1c69e23a9c3c")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Rules/AvoidDivideByZeroRule.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using System; 38 | using System.Collections.Generic; 39 | 40 | namespace CalculateIt2.Engine.Rules 41 | { 42 | /// 43 | /// Represents that when the operator is , the equation should ensure that 44 | /// the value of the right side calculation should not be zero, so that the division by zero would never occur. 45 | /// 46 | /// 47 | internal sealed class AvoidDivideByZeroRule : IRule 48 | { 49 | #region Private Fields 50 | private static readonly Random rnd = new Random(DateTime.Now.Millisecond); 51 | #endregion 52 | 53 | #region Public Methods 54 | /// 55 | /// Applies the current rule to the calculation, by fixing the values on either 56 | /// left or right hand side of the calculation. The operator will also has the chance 57 | /// to be changed when applying the rule. 58 | /// 59 | /// The left hand side of the calculation. 60 | /// The right hand side of the calculation. 61 | /// The calculation generation parameters that are extracted from the equation formation. 62 | /// The operator to join the two calculations. 63 | public void Apply(Calculation left, Calculation right, IDictionary parameters, ref Operator @operator) 64 | { 65 | if (right == null) 66 | { 67 | return; 68 | } 69 | 70 | if (@operator == Operator.Div && 71 | right.Value == 0) 72 | { 73 | var max = Convert.ToInt32(parameters["max"]); 74 | 75 | var counter = new ConstantCalculationCounter(); 76 | right.Accept(counter); 77 | 78 | var adjustment = new RandomizedCalculationValueAdjustment(1, max, counter.NumOfConstantCalculations, x => x == 0); 79 | while (right.Value == 0) 80 | { 81 | adjustment.Reset(); 82 | right.Accept(adjustment); 83 | } 84 | } 85 | } 86 | #endregion 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Rules/AvoidNegativeResultRule.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using System; 38 | using System.Collections.Generic; 39 | 40 | namespace CalculateIt2.Engine.Rules 41 | { 42 | /// 43 | /// Represents that when the operator is , the equation should ensure that 44 | /// the value of the left calculation is larger than or equal to the value of the right calculation, 45 | /// so that the generated equation will never has a negative value. 46 | /// 47 | /// 48 | public sealed class AvoidNegativeResultRule : IRule 49 | { 50 | #region Private Fields 51 | private static readonly Random rnd = new Random(DateTime.Now.Millisecond); 52 | #endregion 53 | 54 | #region Public Methods 55 | /// 56 | /// Applies the current rule to the calculation, by fixing the values on either 57 | /// left or right hand side of the calculation. The operator will also has the chance 58 | /// to be changed when applying the rule. 59 | /// 60 | /// The left hand side of the calculation. 61 | /// The right hand side of the calculation. 62 | /// The calculation generation parameters that are extracted from the equation formation. 63 | /// The operator to join the two calculations. 64 | public void Apply(Calculation left, Calculation right, IDictionary parameters, ref Operator @operator) 65 | { 66 | if (left == null || right == null) 67 | return; 68 | 69 | var leftValue = left.Value; 70 | var rightValue = right.Value; 71 | 72 | if (@operator == Operator.Sub && 73 | leftValue < rightValue) 74 | { 75 | var max = Convert.ToInt32(parameters["max"]); 76 | 77 | var leftConstant = left as ConstantCalculation; 78 | var rightConstant = right as ConstantCalculation; 79 | 80 | if (leftConstant == null && 81 | rightConstant == null) 82 | { 83 | return; 84 | } 85 | 86 | if (rightConstant != null) 87 | { 88 | rightConstant.SetValue(rnd.Next(Convert.ToInt32(leftValue) + 1)); 89 | } 90 | else if (leftConstant != null) 91 | { 92 | leftConstant.SetValue(rnd.Next(Convert.ToInt32(rightValue), max + 1)); 93 | } 94 | 95 | } 96 | } 97 | #endregion 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Rules/ConstantCalculationCounter.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | namespace CalculateIt2.Engine.Rules 38 | { 39 | /// 40 | /// Represents the calculation visitor that counts the constant calculations 41 | /// in a given composite calculation. 42 | /// 43 | internal sealed class ConstantCalculationCounter : CalculationVisitor 44 | { 45 | #region Private Fields 46 | private int numOfConstantCalculations; 47 | #endregion 48 | 49 | #region Public Properties 50 | /// 51 | /// Gets the number of constant calculations. 52 | /// 53 | /// 54 | /// The number of constant calculations. 55 | /// 56 | public int NumOfConstantCalculations => this.numOfConstantCalculations; 57 | #endregion 58 | 59 | #region Protected Methods 60 | /// 61 | /// Visits the constant calculation. 62 | /// 63 | /// The constant calculation. 64 | protected override void VisitConstantCalculation(ConstantCalculation constantCalculation) 65 | { 66 | base.VisitConstantCalculation(constantCalculation); 67 | this.numOfConstantCalculations++; 68 | } 69 | #endregion 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Rules/DivisibilityEnsuranceRule.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using System; 38 | using System.Collections.Generic; 39 | 40 | namespace CalculateIt2.Engine.Rules 41 | { 42 | /// 43 | /// Represents that the generated arithmetic equation should ensure that when the calculation operator is 44 | /// , the left side of the calculation should be divisible by the right side of the calculation. 45 | /// 46 | /// 47 | public sealed class DivisibilityEnsuranceRule : IRule 48 | { 49 | #region Private Fields 50 | private static readonly Random rnd = new Random(DateTime.Now.Millisecond); 51 | #endregion 52 | 53 | #region Public Methods 54 | /// 55 | /// Applies the current rule to the calculation, by fixing the values on either 56 | /// left or right hand side of the calculation. The operator will also has the chance 57 | /// to be changed when applying the rule. 58 | /// 59 | /// The left hand side of the calculation. 60 | /// The right hand side of the calculation. 61 | /// The calculation generation parameters that are extracted from the equation formation. 62 | /// The operator to join the two calculations. 63 | public void Apply(Calculation left, Calculation right, IDictionary parameters, ref Operator @operator) 64 | { 65 | if (left == null || right == null) 66 | { 67 | return; 68 | } 69 | 70 | var leftValue = left.Value; 71 | var rightValue = right.Value; 72 | 73 | if (@operator == Operator.Div && 74 | (leftValue % rightValue) != 0) 75 | { 76 | var max = Convert.ToInt32(parameters["max"]); 77 | 78 | var leftConstant = left as ConstantCalculation; 79 | var rightConstant = right as ConstantCalculation; 80 | 81 | if (leftConstant == null && 82 | rightConstant == null) 83 | { 84 | return; 85 | } 86 | 87 | if (leftConstant != null) 88 | { 89 | var i = 0; 90 | do 91 | { 92 | i++; 93 | } while (i * rightValue <= max); 94 | var proposedLeftConstantValue = rnd.Next(i) * rightValue; 95 | leftConstant.SetValue(proposedLeftConstantValue); 96 | } 97 | else if (rightConstant != null) 98 | { 99 | var possibleValues = new List(); 100 | for (var i = 1; i <= Math.Min(leftValue, max); i++) 101 | { 102 | if ((leftValue % i) == 0) 103 | { 104 | possibleValues.Add(i); 105 | } 106 | } 107 | var proposedRightConstantValue = possibleValues[rnd.Next(possibleValues.Count)]; 108 | rightConstant.SetValue(proposedRightConstantValue); 109 | } 110 | } 111 | } 112 | #endregion 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Rules/IRule.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using System.Collections.Generic; 38 | 39 | namespace CalculateIt2.Engine.Rules 40 | { 41 | /// 42 | /// Represents that the implemented classes are the equation generation rules 43 | /// that are responsible for fixing the calculations in a equation for particular 44 | /// purposes. 45 | /// 46 | public interface IRule 47 | { 48 | /// 49 | /// Applies the current rule to the calculation, by fixing the values on either 50 | /// left or right hand side of the calculation. The operator will also has the chance 51 | /// to be changed when applying the rule. 52 | /// 53 | /// The left hand side of the calculation. 54 | /// The right hand side of the calculation. 55 | /// The calculation generation parameters that are extracted from the equation formation. 56 | /// The operator to join the two calculations. 57 | void Apply(Calculation left, Calculation right, IDictionary parameters, ref Operator @operator); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Rules/RandomizedCalculationValueAdjustment.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using System; 38 | 39 | namespace CalculateIt2.Engine.Rules 40 | { 41 | /// 42 | /// Represents the class that can modify the constant calculations 43 | /// in a given composite calculation instance. 44 | /// 45 | internal sealed class RandomizedCalculationValueAdjustment : CalculationVisitor 46 | { 47 | #region Private Fields 48 | private readonly int min; 49 | private readonly int max; 50 | private readonly int totalNumberOfConstantCalculations; 51 | private readonly Func exclusionExpectation; 52 | private int currentIdx = 0; 53 | private readonly Random rnd = new Random(DateTime.Now.Millisecond); 54 | private int hitIndex; 55 | #endregion 56 | 57 | #region Ctor 58 | /// 59 | /// Initializes a new instance of the class. 60 | /// 61 | /// The minimum of the value that can be generated and being used by the adjustment. 62 | /// The maximum of the value that can be generated and being used by the adjustment. 63 | /// The total number of constant calculations. 64 | /// The exclusion expectation. 65 | public RandomizedCalculationValueAdjustment(int min, int max, int totalNumberOfConstantCalculations, Func exclusionExpectation = null) 66 | { 67 | this.min = min; 68 | this.max = max; 69 | this.exclusionExpectation = exclusionExpectation; 70 | this.totalNumberOfConstantCalculations = totalNumberOfConstantCalculations; 71 | } 72 | #endregion 73 | 74 | #region Internal Methods 75 | /// 76 | /// Resets the counters of the calculation adjustment. 77 | /// 78 | internal void Reset() 79 | { 80 | currentIdx = 0; 81 | hitIndex = rnd.Next(totalNumberOfConstantCalculations); 82 | } 83 | #endregion 84 | 85 | #region Protected Methods 86 | /// 87 | /// Visits the constant calculation. 88 | /// 89 | /// The constant calculation. 90 | protected override void VisitConstantCalculation(ConstantCalculation constantCalculation) 91 | { 92 | if (currentIdx == hitIndex) 93 | { 94 | constantCalculation.SetValue(GetValue()); 95 | } 96 | currentIdx++; 97 | } 98 | #endregion 99 | 100 | #region Private Methods 101 | private int GetValue() 102 | { 103 | int value; 104 | if (exclusionExpectation != null) 105 | { 106 | do 107 | { 108 | if (max == 0) 109 | { 110 | value = rnd.Next(0, min + 1); 111 | } 112 | else 113 | { 114 | value = rnd.Next(min, max + 1); 115 | } 116 | } while (exclusionExpectation(value)); 117 | } 118 | else 119 | { 120 | if (max == 0) 121 | { 122 | value = rnd.Next(0, min + 1); 123 | } 124 | else 125 | { 126 | value = rnd.Next(min, max + 1); 127 | } 128 | } 129 | return value; 130 | } 131 | #endregion 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/SpacingOption.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | namespace CalculateIt2.Engine 38 | { 39 | /// 40 | /// Represents the spacing option when generating formatted string 41 | /// for the calculations. 42 | /// 43 | public enum SpacingOption 44 | { 45 | /// 46 | /// Indicates that no space should be added before and after the operator signs. 47 | /// 48 | None, 49 | /// 50 | /// Indicates that one space should be added before and after the operator signs. 51 | /// 52 | Thin, 53 | /// 54 | /// Indicates that three spaces should be added before and after the operator signs. 55 | /// 56 | Thick 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/Utils.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using System; 38 | using System.Collections.Generic; 39 | using System.Linq; 40 | 41 | namespace CalculateIt2.Engine 42 | { 43 | /// 44 | /// Represents the utility class. 45 | /// 46 | internal static class Utils 47 | { 48 | private static readonly Dictionary OperatorSigns = new Dictionary 49 | { 50 | { Operator.Add, "+"}, 51 | { Operator.Sub, "-"}, 52 | { Operator.Mul, "*"}, 53 | { Operator.Div, "/"} 54 | }; 55 | 56 | private static readonly Random rnd = new Random(DateTime.Now.Millisecond); 57 | 58 | /// 59 | /// Generates the calculation operator randomly. 60 | /// 61 | /// The acceptable operators that can be considered into the operator generating. 62 | /// The operator that should be excluded from the generating operators. 63 | /// A randomly generated operator. 64 | public static Operator GenerateRandomOperator(string acceptableOperators, Operator bypass = Operator.None) 65 | { 66 | // If the proposed bypassing operator is the only one that is allowed to be 67 | // returned, then return it. 68 | if (bypass != Operator.None && 69 | acceptableOperators.Length == 1 && 70 | acceptableOperators.Contains(OperatorSigns[bypass])) 71 | { 72 | return bypass; 73 | } 74 | 75 | while (true) 76 | { 77 | var idx = rnd.Next(4); 78 | var kvp = OperatorSigns.ElementAt(idx); 79 | if (acceptableOperators.Contains(kvp.Value) && kvp.Key != bypass) 80 | { 81 | return kvp.Key; 82 | } 83 | } 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/CalculateIt2.Engine/key.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daxnet/CalculateIt2/344c9080235f680cd28bf6c4958c2f87b8c92cbc/src/CalculateIt2.Engine/key.snk -------------------------------------------------------------------------------- /tests/CalculateIt2.Tests/CalculateIt2.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | Debug 7 | AnyCPU 8 | {85CB7B9E-227F-4898-A161-5935CD9142B3} 9 | Library 10 | Properties 11 | CalculateIt2.Tests 12 | CalculateIt2.Tests 13 | v4.6.1 14 | 512 15 | 16 | 17 | 18 | 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | ..\..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll 46 | True 47 | 48 | 49 | ..\..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll 50 | True 51 | 52 | 53 | ..\..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll 54 | True 55 | 56 | 57 | ..\..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll 58 | True 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | {b3f3898b-7c5c-4cae-8a68-1c69e23a9c3c} 71 | CalculateIt2.Engine 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. 81 | 82 | 83 | 84 | 91 | -------------------------------------------------------------------------------- /tests/CalculateIt2.Tests/EquationFormationTests.cs: -------------------------------------------------------------------------------- 1 | // ================================================================================================== 2 | // 3 | // _____ _ _ _ _____ _ ___ 4 | // / ____| | | | | | | |_ _| | | |__ \ 5 | // | | __ _ | | ___ _ _ | | __ _ | |_ ___ | | | |_ ) | 6 | // | | / _` | | | / __| | | | | | | / _` | | __| / _ \ | | | __| / / 7 | // | |____ | (_| | | | | (__ | |_| | | | | (_| | | |_ | __/ _| |_ | |_ / /_ 8 | // \_____| \__,_| |_| \___| \__,_| |_| \__,_| \__| \___| |_____| \__| |____| 9 | // 10 | // An Arithmetic Equation Generator with Question Generation Capacity 11 | // Copyright © 2016 by daxnet (Sunny Chen) 12 | // https://github.com/daxnet/CalculateIt2 13 | // 14 | // MIT License 15 | // 16 | // Copyright(c) 2016 Sunny Chen 17 | // 18 | // Permission is hereby granted, free of charge, to any person obtaining a copy 19 | // of this software and associated documentation files (the "Software"), to deal 20 | // in the Software without restriction, including without limitation the rights 21 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | // copies of the Software, and to permit persons to whom the Software is 23 | // furnished to do so, subject to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be included in all 26 | // copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 | // SOFTWARE. 35 | // ================================================================================================== 36 | 37 | using CalculateIt2.Engine.Generation; 38 | using Xunit; 39 | 40 | namespace CalculateIt2.Tests 41 | { 42 | public class EquationFormationTests 43 | { 44 | [Fact(DisplayName = "TestTest")] 45 | public void TestTest() 46 | { 47 | Assert.True(2 == 2); 48 | } 49 | 50 | [Fact(DisplayName = "NoOperatorSpecifiedTest")] 51 | public void NoOperatorSpecifiedTest() 52 | { 53 | var formation = "{20}"; 54 | var generator = new ArithmeticEquationGenerator(formation); 55 | Assert.False(generator.IsValid); 56 | } 57 | 58 | [Fact(DisplayName = "OnlyAddOperatorTest")] 59 | public void OnlyAddOperatorTest() 60 | { 61 | var formation = "{20}+"; 62 | var generator = new ArithmeticEquationGenerator(formation); 63 | Assert.True(generator.IsValid); 64 | } 65 | 66 | [Fact(DisplayName = "OnlySubOperatorTest")] 67 | public void OnlySubOperatorTest() 68 | { 69 | var formation = "{20}-"; 70 | var generator = new ArithmeticEquationGenerator(formation); 71 | Assert.True(generator.IsValid); 72 | } 73 | 74 | [Fact(DisplayName = "OnlyMulOperatorTest")] 75 | public void OnlyMulOperatorTest() 76 | { 77 | var formation = "{20}*"; 78 | var generator = new ArithmeticEquationGenerator(formation); 79 | Assert.True(generator.IsValid); 80 | } 81 | 82 | [Fact(DisplayName = "OnlyDivOperatorTest")] 83 | public void OnlyDivOperatorTest() 84 | { 85 | var formation = "{20}/"; 86 | var generator = new ArithmeticEquationGenerator(formation); 87 | Assert.True(generator.IsValid); 88 | } 89 | 90 | [Fact(DisplayName = "CombinedOperator1Test")] 91 | public void CombinedOperator1Test() 92 | { 93 | var formation = "{20}+-"; 94 | var generator = new ArithmeticEquationGenerator(formation); 95 | Assert.True(generator.IsValid); 96 | } 97 | 98 | [Fact(DisplayName = "CombinedOperator2Test")] 99 | public void CombinedOperator2Test() 100 | { 101 | var formation = "{20}*/"; 102 | var generator = new ArithmeticEquationGenerator(formation); 103 | Assert.True(generator.IsValid); 104 | } 105 | 106 | [Fact(DisplayName = "CombinedOperator3Test")] 107 | public void CombinedOperator3Test() 108 | { 109 | var formation = "{20}+-*/"; 110 | var generator = new ArithmeticEquationGenerator(formation); 111 | Assert.True(generator.IsValid); 112 | } 113 | 114 | [Fact(DisplayName = "DuplicatedOperatorTest")] 115 | public void DuplicatedOperatorTest() 116 | { 117 | var formation = "{20}++"; 118 | var generator = new ArithmeticEquationGenerator(formation); 119 | Assert.False(generator.IsValid); 120 | } 121 | 122 | [Fact(DisplayName = "ExactFactorNumberTest")] 123 | public void ExactFactorNumberTest() 124 | { 125 | var formation = "{20}+|2"; 126 | var generator = new ArithmeticEquationGenerator(formation); 127 | Assert.True(generator.IsValid); 128 | } 129 | 130 | [Fact(DisplayName = "RangeFactorNumberTest")] 131 | public void RangeFactorNumberTest() 132 | { 133 | var formation = "{20}+|2-3"; 134 | var generator = new ArithmeticEquationGenerator(formation); 135 | Assert.True(generator.IsValid); 136 | } 137 | 138 | [Fact(DisplayName = "InvalidFactorNumberRangeTest")] 139 | public void InvalidFactorNumberRangeTest() 140 | { 141 | var formation = "{20}+|3-2"; 142 | var generator = new ArithmeticEquationGenerator(formation); 143 | Assert.False(generator.IsValid); 144 | } 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /tests/CalculateIt2.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("CalculateIt2.Tests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("CalculateIt2.Tests")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("85cb7b9e-227f-4898-a161-5935cd9142b3")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /tests/CalculateIt2.Tests/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | --------------------------------------------------------------------------------