Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (* Задание 1 isLeapYear *)
  2. fun isLeapYear (year : int) : bool =
  3.   year mod 400 = 0
  4.   orelse year mod 100 <> 0 andalso year mod 4 = 0
  5.  
  6. (* Задание 2 isCorrectDate *)
  7. (* Код выглядит не совсем так как хотел но в sml блок if bool then - else -  не раздельны*)
  8. fun isCorrectDate (d : int, m : int, y : int) : bool =
  9.   if d < 1
  10.   then false
  11.   else if m = 1 orelse m = 3 orelse m = 5 orelse m = 7
  12.           orelse m = 8 orelse m = 10 orelse m = 12
  13.        then if d <= 31 then true else false
  14.   else if m = 4 orelse m = 6 orelse m = 9 orelse m = 11
  15.        then if d <= 30 then true else false
  16.   else if m = 2
  17.        then if d <= 28 then true
  18.        else if d = 29 andalso isLeapYear y
  19.             then true else false
  20.   else false
  21.  
  22.  
  23. (* Задание 3 newStyleCorrection *)
  24. fun newStyleCorrection (d : int, m : int, y : int) : int =
  25.   let
  26.     val gregorianLY : int = y div 4 - y div 100 + y div 400;              (* Определяем количеств високосных годов *)
  27.     val julianLY : int = y div 4;                                         (* Количество високосных годов по юлианскому *)
  28.     val correction : int = if y mod 4 = 0 andalso not (isLeapYear y)      (* корректируем на прошло ли 28 февраля если по юлияновском высокосный а по григ. нет *)
  29.                               then if m <= 2 then 1 else 0
  30.                               else 0
  31.   in
  32.     julianLY - (gregorianLY + correction) - 2
  33.   end
  34.  
  35. (* Задание 4 getNthInt *)
  36. fun getNthInt (lst : int list, n : int) : int =
  37.   if n > 0
  38.   then getNthInt (tl lst, n - 1)
  39.   else hd lst
  40.  
  41. (* Задание 5 getNthStr *)
  42. fun getNthStr (lst : string list, n : int) : string =
  43.   if n > 0
  44.   then getNthStr (tl lst, n - 1)
  45.   else hd lst
  46.  
  47. (* Задание 6 lastSmaller *)
  48.  
  49. fun lastSmaller (amount : int, lst : int list) : int =
  50.   let
  51.     fun helperFunc (res : int, lst : int list) : int =
  52.     if not (null lst) andalso hd lst < amount             (* lst <> nil *)
  53.     then helperFunc(hd lst, tl lst)
  54.     else res
  55.   in
  56.     if hd lst >= amount                                   (* предпологается что список сорт. по взрст.*)
  57.     then 0
  58.     else helperFunc(hd lst, lst)
  59.   end
  60.  
  61.  
  62. (* Списки для выполнения задания 8 (списки поправок) *)
  63. (* поправка на тысячи года *)
  64. val thousandCorrection = [ 0, 1390000, 2770000, 1210000, 2590000
  65.                           , 1030000, 2420000 ]
  66. (* поправка на сотни года *)
  67. val hundredCorrection  = [ 0, 430000, 870000, 1300000, 1740000
  68.                           , 2170000, 2600000, 80000, 520000, 950000 ]
  69. (* поправка на десятки года *)
  70. val decadeCorrection   = [ 0, 930000, 1860000, 2790000, 760000
  71.                           , 1690000, 2620000, 600000, 1530000, 2460000 ]
  72. (* поправка на единицы года *)
  73. val yearCorrection     = [ 0, 1860000, 780000, 2640000, 1550000
  74.                           , 460000, 2330000, 1240000, 150000, 2020000 ]
  75. (* поправка на месяц *)
  76. val monthCorrection    = [ 1340000, 1190000, 2420000, 2260000, 2200000
  77.                           , 2060000, 2000000, 1840000, 1700000, 1660000
  78.                           , 1510000, 1480000 ]
  79. (* календарная поправка *)
  80. val calendarCorrection = [0, 20000, 50000, 80000]
  81. (* поправка для нормализации дня месяца *)
  82. val reductions = [2953059, 5906118, 8859177, 11812236, 14765295, 17718354]
  83.  
  84. (* Задание 7 firstNewMoonInt *)
  85.  
  86. fun firstNewMoonInt (d : int, m : int, y : int) : int option =
  87.   let
  88.     val tmpY : int = if (m = 2 orelse m = 1) then y - 1 else y
  89.     val delta : int = newStyleCorrection(d, m, tmpY) * 100000
  90.     val thousand : int = getNthInt(thousandCorrection, tmpY div 1000)
  91.     val hundred : int = getNthInt(hundredCorrection, (tmpY mod 1000) div 100)
  92.     val decade : int = getNthInt(decadeCorrection, (tmpY mod 100) div 10)
  93.     val year : int = getNthInt(yearCorrection, tmpY mod 10);
  94.     val month : int = getNthInt(monthCorrection, m - 1)
  95.     val calendar : int = getNthInt(calendarCorrection, tmpY mod 4)
  96.     val sum : int = delta + thousand + hundred + decade + year + month + calendar
  97.     val lastSmall : int = lastSmaller(sum - 100000, reductions);
  98.     val res : int = sum - lastSmall;
  99.     val wholeRes : int = res div 100000;
  100.   in
  101.     if m = 2
  102.     then if wholeRes <= 28 orelse
  103.          isLeapYear y andalso wholeRes = 29
  104.          then SOME res else NONE
  105.     else if wholeRes <= 30
  106.     then SOME res
  107.     else NONE
  108.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement