Advertisement
markoczy

CatYears WIP...

May 15th, 2023
1,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.08 KB | None | 0 0
  1. // You can edit this code!
  2. // Click here and start typing.
  3. //https://herz-fuer-tiere.de/haustiere/katzen/katzenjahre-in-menschenjahren-umrechnen#tabelle
  4. package main
  5.  
  6. import "fmt"
  7.  
  8. type yearCount struct {
  9.     years  int
  10.     months int
  11. }
  12.  
  13. func (yc *yearCount) Add(years, months int) {
  14.     addYrs := years + months/12
  15.     addMonths := months % 12
  16.     yc.years += addYrs
  17.     if yc.months+addMonths > 12 {
  18.         yc.years += 1
  19.         yc.months -= 12
  20.     }
  21.     yc.months += addMonths
  22. }
  23.  
  24. func (yc *yearCount) String() string {
  25.     return fmt.Sprintf("%d year(s) and %d month(s)", yc.years, yc.months)
  26. }
  27.  
  28. func catYears(humanYears float32) float32 {
  29.     if humanYears <= 1.0 {
  30.         return 15.0 * humanYears
  31.     }
  32.     if humanYears <= 2.0 {
  33.         return 15.0 + (10 * (1 - humanYears))
  34.     }
  35.     return 25.0 + (humanYears-2.0)*4
  36. }
  37.  
  38. func catYears2(years, months int) yearCount {
  39.     yc := yearCount{0, 0}
  40.     if years == 0 {
  41.         if months <= 1 {
  42.             yc.Add(0, months*6)
  43.         } else if months <= 3 {
  44.             yc.Add(0, 6+54.0*((months-1)/2.0))
  45.         }
  46.     }
  47.     return yc
  48. }
  49.  
  50. func main() {
  51.     yc := catYears2(0, 2)
  52.     fmt.Println("CatYears:", yc.String())
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement