Advertisement
Guest User

math.ps1

a guest
Sep 20th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #   .SYNOPSIS
  2. #       Generate math worksheets randomly.
  3. #
  4. #   .DESCRIPTION
  5. #       Used to generate addition, subtraction, and multiplication worksheets. Can generate math problems that are
  6. #       1, 2, or 3 digits long. The Carry switch determines if 'number carrying' is used or not.
  7. #  
  8. #   .PARAMETER Digits
  9. #       Specifies the number of digits to use for the math problems. Minimum value is 1. Maximum value is 3. Default is 1.
  10. #      
  11. #   .PARAMETER Operation
  12. #       Specifies the operation for the generated math problems. Valid selections are: Addition, Subtraction, or Multiplication.
  13. #       When Multiplication is selected, only 4 rows of problems are generated in order to provide extra space for problem solving.
  14. #
  15. #       If parameter value is not specified, Addition problems are generated.
  16. #          
  17. #   .PARAMETER Carry
  18. #       Specifies that 'number carrying' operations can be used.
  19. #      
  20. #   .NOTES
  21. #        Author: Joshua Beckman
  22. #        Editor: Anthony Franca
  23. #       Version: 2019.09.20
  24. #       Purpose: Educational
  25. #
  26. #   .LINK
  27. #       https://www.dowst.dev/homework-double-digit-addition-without-carrying/
  28.  
  29. param(
  30.     [parameter(Mandatory = $false)]
  31.     [ValidateRange(1,3)][int]$Digits = 1,
  32.     [parameter(Mandatory = $false)]
  33.     [ValidateSet("Addition","Subtraction","Multiplication")][string]$Operation = "Addition",
  34.     [parameter(Mandatory = $false)]
  35.     [switch]$Carry
  36. )
  37.  
  38. [System.Collections.Generic.List[PSObject]] $page = @()
  39. $j = 0
  40. $blank120 = "<td height=""120""> </td>`n" * 9
  41. $blank75 = "<td height=""75""> </td>`n" * 9
  42. $blank52 = "<td height=""52""> </td>`n" * 9
  43. $blank20 = "<td height=""20""><br></td>`n" * 9
  44. If($Operation -eq "Addition" -OR $Operation -eq "Subtraction"){$Count = 5}
  45. If($Operation -eq "Multiplication"){$Count = 4}
  46. while($j -lt $Count){
  47.     [System.Collections.Generic.List[PSObject]] $rowA = @()
  48.     [System.Collections.Generic.List[PSObject]] $rowB = @()
  49.     $i=0
  50.     while($i -lt 5){
  51.         If($Digits -ge 1){
  52.             $a = Get-Random -Minimum 1 -Maximum 10
  53.             If($Operation -eq "Subtraction"){
  54.                 $b = Get-Random -Minimum 0 -Maximum $a
  55.             }ElseIf($Operation -eq "Addition"){
  56.                 If($Carry -eq $true){
  57.                     $b = Get-Random -Minimum 0 -Maximum 10
  58.                 }Else{
  59.                     $b = Get-Random -Minimum 0 -Maximum (9-$a+1)
  60.                 }
  61.             }ElseIf($Operation -eq "Multiplication"){
  62.                 $b = Get-Random -Minimum 1 -Maximum 10
  63.             }
  64.         }
  65.         If($Digits -ge 2){
  66.             $c = Get-Random -Minimum 1 -Maximum 10
  67.             If($Operation -eq "Subtraction"){
  68.                 If($Carry -eq $true){
  69.                     $d = Get-Random -Minimum 0 -Maximum 10
  70.                 }Else{
  71.                     $d = Get-Random -Minimum 0 -Maximum $c
  72.                 }
  73.             }ElseIf($Operation -eq "Addition"){
  74.                 If($Carry -eq $true){
  75.                     $d = Get-Random -Minimum 0 -Maximum 10
  76.                 }Else{
  77.                     $d = Get-Random -Minimum 0 -Maximum (9-$c+1)
  78.                 }
  79.             }ElseIf($Operation -eq "Multiplication"){
  80.                 $d = Get-Random -Minimum 1 -Maximum 10
  81.             }
  82.         }
  83.         If($Digits -ge 3){
  84.             $e = Get-Random -Minimum 1 -Maximum 10
  85.             If($Operation -eq "Subtraction"){
  86.                 If($Carry -eq $true){
  87.                     $f = Get-Random -Minimum 0 -Maximum 10
  88.                 }Else{
  89.                     $f = Get-Random -Minimum 0 -Maximum $e
  90.                 }
  91.             }ElseIf($Operation -eq "Addition"){
  92.                 If($Carry -eq $true){
  93.                     $f = Get-Random -Minimum 0 -Maximum 10
  94.                 }Else{
  95.                     $f = Get-Random -Minimum 0 -Maximum (9-$e+1)
  96.                 }
  97.             }ElseIf($Operation -eq "Multiplication"){
  98.                 $f = Get-Random -Minimum 1 -Maximum 10
  99.             }
  100.         }
  101.         if(($b + $d) -gt 0){
  102.             if($b -eq 0){$b=' '}
  103.             $rowA.Add("<td class=""xl66"" height=""52"" width=""115"">$a$c$e</td>`n")
  104.             If($Operation -eq "Addition"){
  105.                 $rowB.Add("<td class=""xl65"" height=""52"" width=""115"">+ $b$d$f</td>`n")
  106.             }ElseIf($Operation -eq "Subtraction"){
  107.                 $rowB.Add("<td class=""xl65"" height=""52"" width=""115"">- $b$d$f</td>`n")
  108.             }ElseIf($Operation -eq "Multiplication"){
  109.                 $rowB.Add("<td class=""xl65"" height=""52"" width=""115"">x $b$d$f</td>`n")
  110.             }
  111.             $i++
  112.         }
  113.     }
  114.  
  115.     $tableRows = New-Object System.Text.StringBuilder
  116.     $tableRows.Append('<tr height="52">') | Out-Null
  117.     $tableRows.Append($rowA -join('<td class="xl66" width="15"><br>')) | Out-Null
  118.     $tableRows.Append('</tr><tr height="52">') | Out-Null
  119.     $tableRows.Append($rowB -join('<td class="xl66" width="15"><br>')) | Out-Null
  120.     If($Operation -eq "Multiplication"){
  121.         $tableRows.Append("</tr><tr height=""52"">$blank120</tr>") | Out-Null
  122.     }Else{
  123.         $tableRows.Append("</tr><tr height=""52"">$blank75</tr>") | Out-Null
  124.     }
  125.     $page.Add($tableRows.ToString())
  126.     $j++
  127. }
  128.  
  129. If($Operation -eq "Addition"){
  130.     $bodyTop = @'
  131.    <html><head><title>Addition</title><style>.xl65{mso-style-parent:style0;font-size:36.0pt;mso-number-format:"\@";
  132.    text-align:right;border-top:none;border-right:none;border-bottom:1.5pt solid blue;border-left:none;color:blue}
  133.    .xl66{mso-style-parent:style0;font-size:36.0pt;mso-number-format:"\@";text-align:right;color:blue}</style></head><body>
  134.    <table style="text-align: left; width: 635px; height: 60px;" border="0" cellpadding="0" cellspacing="0"><tbody>
  135. '@
  136. }ElseIf($Operation -eq "Subtraction"){
  137.     $bodyTop = @'
  138.    <html><head><title>Subtraction</title><style>.xl65{mso-style-parent:style0;font-size:36.0pt;mso-number-format:"\@";
  139.    text-align:right;border-top:none;border-right:none;border-bottom:1.5pt solid blue;border-left:none;color:blue}
  140.    .xl66{mso-style-parent:style0;font-size:36.0pt;mso-number-format:"\@";text-align:right;color:blue}</style></head><body>
  141.    <table style="text-align: left; width: 635px; height: 60px;" border="0" cellpadding="0" cellspacing="0"><tbody>
  142. '@
  143. }ElseIf($Operation -eq "Multiplication"){
  144.     $bodyTop = @'
  145.    <html><head><title>Multiplication</title><style>.xl65{mso-style-parent:style0;font-size:36.0pt;mso-number-format:"\@";
  146.    text-align:right;border-top:none;border-right:none;border-bottom:1.5pt solid blue;border-left:none;color:blue}
  147.    .xl66{mso-style-parent:style0;font-size:36.0pt;mso-number-format:"\@";text-align:right;color:blue}</style></head><body>
  148.    <table style="text-align: left; width: 635px; height: 60px;" border="0" cellpadding="0" cellspacing="0"><tbody>
  149. '@
  150. }
  151.  
  152. $bodyBotton = @'
  153. </tbody></table><br><br></body></html>
  154. '@
  155. If($Operation -eq "Addition"){
  156.     $bodyTop + $page -join("<tr height=""20"">$blank20</tr>") + $bodyBotton | out-file '.\addition.html'
  157. }ElseIf($Operation -eq "Subtraction"){
  158.     $bodyTop + $page -join("<tr height=""20"">$blank20</tr>") + $bodyBotton | out-file '.\subtraction.html'
  159. }ElseIf($Operation -eq "Multiplication"){
  160.     $bodyTop + $page -join("<tr height=""20"">$blank20</tr>") + $bodyBotton | out-file '.\multiplication.html'
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement