Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. ;;Edmund Horsch & Alex Russo
  2. (require 2htdp/image)
  3. (require 2htdp/universe)
  4.  
  5. ; A MonthFormat is one of:
  6. ; - "long"
  7. ; - "short"
  8.  
  9. ; A DateOrder is one of:
  10. ; - "MDY"
  11. ; - "DMY"
  12.  
  13. ;;#3
  14. ; calendar : Year Month Day -> Image
  15. ; returns an image of a date on a background
  16.  
  17. (define (calendar y m d)
  18. (overlay (beside (text (number->string d) 20 "red") (text " " 20 "red") (text m 20 "red") (text " " 20 "red") (text (number->string y) 20 "red"))
  19. (rectangle 400 200 "outline" "white")))
  20.  
  21. ; year-month-day->date : Year Month Day DateOrder MonthFormat -> String
  22. ; produces a date as a string
  23. (define (year-month-day->date y m d o f)
  24. (cond [(string=? o "DMY") (beside (text (number->string d) 20 "red") (text " " 20 "red") (month-format m f) (text " " 20 "red") (text (number->string y) 20 "red"))]
  25. [(string=? o "MDY") (beside (month-format m f) (text " " 20 "red") (text (number->string d) 20 "red") (text " " 20 "red") (text (number->string y) 20 "red"))]))
  26. ;(check-expect (year-month-day->date 1936 "November" 12 "MDY" "long") "November 12, 1936")
  27.  
  28.  
  29. ; month-format : Month MonthFormat -> String
  30. ; abbreviates Month to three letters or not
  31. (define (month-format m f)
  32. (cond [(string=? f "long") (text m 20 "red")]
  33. [(string=? f "short") (text (substring m 0 3) 20 "red")]))
  34. ;(check-expect (month-format "November" "long") "November")
  35. ;(check-expect (month-format "November" "short") "Nov")
  36.  
  37. ;;#4
  38. ; days-between : Year Month Day Year Month Day -> Number
  39. ; Determines th enumber of days between two dates
  40. (define (days-between y1 m1 d1 y2 m2 d2)
  41. (abs(- (year-month-day->days y1 m1 d1) (year-month-day->days y2 m2 d2))))
  42.  
  43. (check-expect (days-between 2010 "September" 28 2012 "September" 28) (* 365 2))
  44. (check-expect (days-between 2010 "September" 28 2013 "September" 28) (* 365 3))
  45.  
  46. ; year-month-day->days : Year Month Day -> Number
  47. ; returns the number of days elapsed since January 1, 0
  48. (define (year-month-day->days y m d)
  49. (- (+ (* y 365) (month->day-in-year m) d) 1))
  50. (check-expect (year-month-day->days 0 "January" 1) 0)
  51. (check-expect (year-month-day->days 2017 "August" 28) 736444)
  52.  
  53. ; month->day-in-year : Month -> Number
  54. ; returns the days elapsed in the year
  55. (define (month->day-in-year m)
  56. (cond[(string=? m "January") 0]
  57. [(string=? m "February") 31]
  58. [(string=? m "March") 59]
  59. [(string=? m "April") 90]
  60. [(string=? m "May") 120]
  61. [(string=? m "June") 151]
  62. [(string=? m "July") 181]
  63. [(string=? m "August") 212]
  64. [(string=? m "September") 243]
  65. [(string=? m "October") 273]
  66. [(string=? m "November") 304]
  67. [(string=? m "December") 334]))
  68. (check-expect (month->day-in-year "January") 0)
  69. (check-expect (month->day-in-year "September") 243)
  70.  
  71. ;;#5
  72. ; days->year : Number -> Year
  73. ; takes days since 1 Jan 0 and returns the year
  74. (define (days->year d)
  75. (quotient d 365))
  76. (check-expect (days->year 364) 0)
  77.  
  78. ; days-in-year->month : DaysInYear -> Month
  79. ; takes days since the first of the year and returns the month
  80. (define (days-in-year->month d)
  81. (cond[(and (>= d 0) (< d 32)) "January"]
  82. [(and (>= d 31) (< d 59)) "February"]
  83. [(and (> d 58) (< d 91)) "March"]
  84. [(and (>= d 90) (< d 121)) "April"]
  85. [(and (>= d 120) (< d 152)) "May"]
  86. [(and (>= d 151) (< d 182)) "June"]
  87. [(and (>= d 181) (< d 213)) "July"]
  88. [(and (>= d 212) (< d 244)) "August"]
  89. [(and (>= d 243) (< d 274)) "September"]
  90. [(and (>= d 273) (< d 305)) "October"]
  91. [(and (>= d 304) (< d 335)) "November"]
  92. [(and (>= d 334) (< d 366)) "December"]))
  93. (check-expect (days-in-year->month 0) "January")
  94.  
  95. ; days->month : Number -> Month
  96. ; takes days since 1 Jan 0 and returns the month
  97. (define (days->month d)
  98. (days-in-year->month(- d (* (days->year d) 365))))
  99. (check-expect (days->month 59) "March")
  100.  
  101. ; days-in-year->days-in-month : DaysInYear -> DaysInMonth
  102. ; takes days since the first of the year
  103. ; and returns days since the first of the month
  104. (define (days-in-year->days-in-month d)
  105. (- d (month->day-in-year(days-in-year->month d))))
  106.  
  107. ; days->day : Number -> Day
  108. ; takes days since 1 Jan 0 and returns the day of the month
  109. (define (days->day d)
  110. (days-in-year->days-in-month(+ 1 (- d (* (days->year d) 365)))))
  111. (check-expect (days->day 0) 1)
  112.  
  113. ;;init-year is the starting year
  114. (define init-year 0)
  115. ;;init-month is the starting month
  116. (define init-month "January")
  117. ;;init-day is the starting day
  118. (define init-day 28)
  119.  
  120. ; init-time : Number
  121. ; days since init-month init-day, init-year
  122. (define init-time (year-month-day->days init-year init-month init-day))
  123.  
  124. ; time-passing : Number -> Image
  125. ; takes days t since 1 Jan 0, advances t by init-time
  126. ; and returns a calendar image of the corresponding date
  127. (define (time-passing t)
  128. (calendar (days->year t) (days->month t) (days->day t)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement