home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rbemx144.zip / ruby-1.4.4 / sample / biorhythm.rb next >
Text File  |  1999-08-13  |  4KB  |  139 lines

  1. #!/usr/local/bin/ruby
  2. #
  3. #               biorhythm.rb - 
  4. #                       $Release Version: $
  5. #                       $Revision: 1.2 $
  6. #                       $Date: 1999/08/13 05:45:19 $
  7. #                       by Yasuo OHBA(STAFS Development Room)
  8. #
  9. # --
  10. #
  11. #       
  12. #
  13.  
  14. include Math
  15. require "date.rb"
  16. require "parsearg.rb"
  17.  
  18. def usage()
  19.   print "Usage:\n"
  20.   print "biorhythm.rb [options]\n"
  21.   print "  options...\n"
  22.   print "    -D YYYYMMDD(birthday)     : use default values.\n"
  23.   print "    --sdate | --date YYYYMMDD : use system date; use specified date.\n"
  24.   print "    --birthday YYYYMMDD       : specifies your birthday.\n"
  25.   print "    -v | -g                   : show values or graph.\n"
  26.   print "    --days DAYS               : graph range (only in effect for graphs).\n"
  27.   print "    --help                    : help\n"
  28. end
  29. $USAGE = 'usage'
  30.  
  31. def printHeader(y, m, d, p, w)
  32.   print "\n>>> Biorhythm <<<\n"
  33.   printf "The birthday %04d.%02d.%02d is a %s\n", y, m, d, w
  34.   printf "Age in days: [%d]\n\n", p
  35. end
  36.  
  37. def getPosition(z)
  38.   pi = 3.14159265
  39.   phys = (50.0 * (1.0 + sin((z / 23.0 - (z / 23)) * 360.0 * pi / 180.0))).to_i
  40.   emot = (50.0 * (1.0 + sin((z / 28.0 - (z / 28)) * 360.0 * pi / 180.0))).to_i
  41.   geist =(50.0 * (1.0 + sin((z / 33.0 - (z / 33)) * 360.0 * pi / 180.0))).to_i
  42.   return phys, emot, geist
  43. end
  44.  
  45. #
  46. # main program
  47. #
  48. parseArgs(0, nil, "vg", "D:", "sdate", "date:", "birthday:", "days:")
  49.  
  50. if $OPT_D
  51.   dd = Date.new(Time.now.strftime("%Y%m%d"))
  52.   bd = Date.new($OPT_D)
  53.   ausgabeart = "g"
  54. else
  55.   if $OPT_birthday
  56.     bd = Date.new($OPT_birthday)
  57.   else
  58.     printf(STDERR, "Birthday                      (YYYYMMDD) : ")
  59.     if (si = STDIN.gets.chop) != ""
  60.       bd = Date.new(si)
  61.     end
  62.   end
  63.   if !bd
  64.     printf STDERR, "BAD Input Birthday!!\n"
  65.     exit()
  66.   end
  67.   
  68.   if $OPT_sdate
  69.     dd = Date.new(Time.now.strftime("%Y%m%d"))
  70.   elsif $OPT_date
  71.     dd = Date.new($OPT_date)
  72.   else
  73.     printf(STDERR, "Date        [<RETURN> for Systemdate] (YYYYMMDD) : ")
  74.     if (si = STDIN.gets.chop) != ""
  75.       dd = Date.new(si)
  76.     end
  77.   end
  78.   if !dd
  79.     dd = Date.new(Time.now.strftime("%Y%m%d"))
  80.   end
  81.  
  82.   if $OPT_v
  83.     ausgabeart = "v"
  84.   elsif $OPT_g
  85.     ausgabeart = "g"
  86.   else
  87.     printf(STDERR, "Values for today or Graph  (v/g) [default g] : ")
  88.     ausgabeart = STDIN.gets.chop
  89.   end
  90. end
  91. if (ausgabeart == "v")
  92.   printHeader(bd.year, bd.month, bd.day, dd.period - bd.period, bd.name_of_week)
  93.   print "\n"
  94.   
  95.   phys, emot, geist = getPosition(dd.period - bd.period)
  96.   printf "Biorhythm:   %04d.%02d.%02d\n", dd.year, dd.month, dd.day
  97.   printf "Physical:    %d%%\n", phys
  98.   printf "Emotional:   %d%%\n", emot
  99.   printf "Mental:      %d%%\n", geist
  100.   print "\n"
  101. else
  102.   if $OPT_days
  103.     display_period = $OPT_days.to_i
  104.   elsif $OPT_D
  105.     display_period = 9
  106.   else
  107.     printf(STDERR, "Graph for how many days     [default 10] : ")
  108.     display_period = STDIN.gets.chop
  109.     if (display_period == "")
  110.       display_period = 9
  111.     else
  112.       display_period = display_period.to_i - 1
  113.     end
  114.   end
  115.  
  116.   printHeader(bd.year, bd.month, bd.day, dd.period - bd.period, bd.name_of_week)
  117.   print "                     P=physical, E=emotional, M=mental\n"
  118.   print "             -------------------------+-------------------------\n"
  119.   print "                     Bad Condition    |    Good Condition\n"
  120.   print "             -------------------------+-------------------------\n"
  121.   
  122.   for z in (dd.period - bd.period)..(dd.period - bd.period + display_period)
  123.     phys, emot, geist = getPosition(z)
  124.     
  125.     printf "%04d.%02d.%02d : ", dd.year, dd.month, dd.day
  126.     p = (phys / 2.0 + 0.5).to_i
  127.     e = (emot / 2.0 + 0.5).to_i
  128.     g = (geist / 2.0 + 0.5).to_i
  129.     graph = "." * 51
  130.     graph[25] = ?|
  131.     graph[p] = ?P
  132.     graph[e] = ?E
  133.     graph[g] = ?M
  134.     print graph, "\n"
  135.     dd = dd + 1
  136.   end
  137.   print "             -------------------------+-------------------------\n\n"
  138. end
  139.