Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;Edmund Horsch & Alex Russo
- (require 2htdp/image)
- (require 2htdp/universe)
- ; A MonthFormat is one of:
- ; - "long"
- ; - "short"
- ; A DateOrder is one of:
- ; - "MDY"
- ; - "DMY"
- ;;#3
- ; calendar : Year Month Day -> Image
- ; returns an image of a date on a background
- (define (calendar y m d)
- (overlay (beside (text (number->string d) 20 "red") (text " " 20 "red") (text m 20 "red") (text " " 20 "red") (text (number->string y) 20 "red"))
- (rectangle 400 200 "outline" "white")))
- ; year-month-day->date : Year Month Day DateOrder MonthFormat -> String
- ; produces a date as a string
- (define (year-month-day->date y m d o f)
- (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"))]
- [(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"))]))
- ;(check-expect (year-month-day->date 1936 "November" 12 "MDY" "long") "November 12, 1936")
- ; month-format : Month MonthFormat -> String
- ; abbreviates Month to three letters or not
- (define (month-format m f)
- (cond [(string=? f "long") (text m 20 "red")]
- [(string=? f "short") (text (substring m 0 3) 20 "red")]))
- ;(check-expect (month-format "November" "long") "November")
- ;(check-expect (month-format "November" "short") "Nov")
- ;;#4
- ; days-between : Year Month Day Year Month Day -> Number
- ; Determines th enumber of days between two dates
- (define (days-between y1 m1 d1 y2 m2 d2)
- (abs(- (year-month-day->days y1 m1 d1) (year-month-day->days y2 m2 d2))))
- (check-expect (days-between 2010 "September" 28 2012 "September" 28) (* 365 2))
- (check-expect (days-between 2010 "September" 28 2013 "September" 28) (* 365 3))
- ; year-month-day->days : Year Month Day -> Number
- ; returns the number of days elapsed since January 1, 0
- (define (year-month-day->days y m d)
- (- (+ (* y 365) (month->day-in-year m) d) 1))
- (check-expect (year-month-day->days 0 "January" 1) 0)
- (check-expect (year-month-day->days 2017 "August" 28) 736444)
- ; month->day-in-year : Month -> Number
- ; returns the days elapsed in the year
- (define (month->day-in-year m)
- (cond[(string=? m "January") 0]
- [(string=? m "February") 31]
- [(string=? m "March") 59]
- [(string=? m "April") 90]
- [(string=? m "May") 120]
- [(string=? m "June") 151]
- [(string=? m "July") 181]
- [(string=? m "August") 212]
- [(string=? m "September") 243]
- [(string=? m "October") 273]
- [(string=? m "November") 304]
- [(string=? m "December") 334]))
- (check-expect (month->day-in-year "January") 0)
- (check-expect (month->day-in-year "September") 243)
- ;;#5
- ; days->year : Number -> Year
- ; takes days since 1 Jan 0 and returns the year
- (define (days->year d)
- (quotient d 365))
- (check-expect (days->year 364) 0)
- ; days-in-year->month : DaysInYear -> Month
- ; takes days since the first of the year and returns the month
- (define (days-in-year->month d)
- (cond[(and (>= d 0) (< d 32)) "January"]
- [(and (>= d 31) (< d 59)) "February"]
- [(and (> d 58) (< d 91)) "March"]
- [(and (>= d 90) (< d 121)) "April"]
- [(and (>= d 120) (< d 152)) "May"]
- [(and (>= d 151) (< d 182)) "June"]
- [(and (>= d 181) (< d 213)) "July"]
- [(and (>= d 212) (< d 244)) "August"]
- [(and (>= d 243) (< d 274)) "September"]
- [(and (>= d 273) (< d 305)) "October"]
- [(and (>= d 304) (< d 335)) "November"]
- [(and (>= d 334) (< d 366)) "December"]))
- (check-expect (days-in-year->month 0) "January")
- ; days->month : Number -> Month
- ; takes days since 1 Jan 0 and returns the month
- (define (days->month d)
- (days-in-year->month(- d (* (days->year d) 365))))
- (check-expect (days->month 59) "March")
- ; days-in-year->days-in-month : DaysInYear -> DaysInMonth
- ; takes days since the first of the year
- ; and returns days since the first of the month
- (define (days-in-year->days-in-month d)
- (- d (month->day-in-year(days-in-year->month d))))
- ; days->day : Number -> Day
- ; takes days since 1 Jan 0 and returns the day of the month
- (define (days->day d)
- (days-in-year->days-in-month(+ 1 (- d (* (days->year d) 365)))))
- (check-expect (days->day 0) 1)
- ;;init-year is the starting year
- (define init-year 0)
- ;;init-month is the starting month
- (define init-month "January")
- ;;init-day is the starting day
- (define init-day 28)
- ; init-time : Number
- ; days since init-month init-day, init-year
- (define init-time (year-month-day->days init-year init-month init-day))
- ; time-passing : Number -> Image
- ; takes days t since 1 Jan 0, advances t by init-time
- ; and returns a calendar image of the corresponding date
- (define (time-passing t)
- (calendar (days->year t) (days->month t) (days->day t)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement