Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1.  
  2. require 'ext.rb'
  3. require 'TestAufgabe1.rb'
  4.  
  5.  
  6. #Zeit in Minuten
  7. # specification
  8. #Purpose: converts years, days, hours and mins to mins
  9. #Contract: total_min(years, days, hours, mins): Natural x Natural x Natural x Natural -> Natural
  10. #Examples: (1, 2, 3, 4) -> 528664, (3, 247, 13, 23) -> 1933283
  11. def total_min(years, days, hours, mins)
  12. check_pre((nat?(years) and nat?(days) and nat?(hours) and nat?(mins)))
  13. hour_to_min(day_to_hour(year_to_day(years) + days) +hours) + mins
  14. end
  15.  
  16. # specification
  17. #Purpose: converts years to days
  18. #Contract: year_to_day(a): Natural -> Natural
  19. #Examples: (1) -> 365, (2) -> 730
  20. def year_to_day(a)
  21. check_pre(nat?(a))
  22. a * days_per_year
  23. end
  24.  
  25. # specification
  26. #Purpose: converts days to hours
  27. #Contract: day_to_hour(d): Natural -> Natural
  28. #Examples: (1) -> 24, (2) -> 48
  29. def day_to_hour(d)
  30. check_pre(nat?(d))
  31. d * hours_per_day
  32. end
  33.  
  34. # specification
  35. #Purpose: converts hours to mins
  36. #Contract: hour_to_min(h): Natural -> Natural
  37. #Examples: (1) -> 60, (2) -> 120
  38. def hour_to_min(h)
  39. check_pre(nat?(h))
  40. h * mins_per_hour
  41. end
  42.  
  43. def hours_per_day
  44. return 24
  45. end
  46. def mins_per_hour
  47. return 60
  48. end
  49. def days_per_year
  50. return 365
  51. end
  52. puts total_min(1, 67, 5, 59)
  53.  
  54. #umgekehrter Weg
  55. # specification
  56. #Purpose: converts mins to full years, days, hours and mins
  57. #Contract: min_to_ydhm(zahl): Natural -> Natural x Natural x Natural x Natural
  58. #Examples: (123456789) -> 234a 323d 21h 9m, (2413795) -> 4a 216d 5h 55m
  59. def min_to_ydhm(zahl)
  60. check_pre(nat?(zahl))
  61. return "#{full_years(zahl)}a, #{full_days(zahl) % 365}d, #{full_hours(zahl) % 24}h, #{zahl % 60}m"
  62. end
  63.  
  64. # specification
  65. #Purpose: converts mins to full years
  66. #Contract: full_years(mins): Natural -> Natural
  67. #Examples: (1362582) -> 2, (2849275) -> 5
  68. def full_years(mins)
  69. check_pre(nat?(mins))
  70. full_days(mins) / days_per_year
  71. end
  72.  
  73. # specification
  74. #Purpose: converts mins to full days
  75. #Contract: full_days(mins): Natural -> Natural
  76. #Examples: (1362582) -> 946, (2849275) -> 1978
  77. def full_days(mins)
  78. check_pre(nat?(mins))
  79. full_hours(mins) / hours_per_day
  80. end
  81.  
  82. # specification
  83. #Purpose: converts mins to full hours
  84. #Contract: full_hours(mins): Natural -> Natural
  85. #Examples: (3274) -> 54, (72632) -> 1210
  86. def full_hours(mins)
  87. check_pre(nat?(mins))
  88. mins / mins_per_hour
  89. end
  90.  
  91. puts min_to_ydhm(2413795)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement