sample/biorhythm.rb
DEFINITIONS
This source file includes following functions.
1 #!/usr/local/bin/ruby
2 #
3 # biorhythm.rb -
4 # $Release Version: $
5 # $Revision: 1.7 $
6 # $Date: 2002/06/11 07:02:23 $
7 # by Yasuo OHBA(STAFS Development Room)
8 #
9 # --
10 #
11 #
12 #
13
14 # probably based on:
15 #
16 # Newsgroups: comp.sources.misc,de.comp.sources.os9
17 # From: fkk@stasys.sta.sub.org (Frank Kaefer)
18 # Subject: v41i126: br - Biorhythm v3.0, Part01/01
19 # Message-ID: <1994Feb1.070616.15982@sparky.sterling.com>
20 # Sender: kent@sparky.sterling.com (Kent Landfield)
21 # Organization: Sterling Software
22 # Date: Tue, 1 Feb 1994 07:06:16 GMT
23 #
24 # Posting-number: Volume 41, Issue 126
25 # Archive-name: br/part01
26 # Environment: basic, dos, os9
27
28 include Math
29 require "date.rb"
30 require "parsearg.rb"
31 require "parsedate.rb"
32
33 def usage()
34 print "Usage:\n"
35 print "biorhythm.rb [options]\n"
36 print " options...\n"
37 print " -D YYYYMMDD(birthday) : use default values.\n"
38 print " --sdate | --date YYYYMMDD : use system date; use specified date.\n"
39 print " --birthday YYYYMMDD : specifies your birthday.\n"
40 print " -v | -g : show values or graph.\n"
41 print " --days DAYS : graph range (only in effect for graphs).\n"
42 print " --help : help\n"
43 end
44 $USAGE = 'usage'
45
46 def printHeader(y, m, d, p, w)
47 print "\n>>> Biorhythm <<<\n"
48 printf "The birthday %04d.%02d.%02d is a %s\n", y, m, d, w
49 printf "Age in days: [%d]\n\n", p
50 end
51
52 def getPosition(z)
53 pi = Math::PI
54 z = Integer(z)
55 phys = (50.0 * (1.0 + sin((z / 23.0 - (z / 23)) * 360.0 * pi / 180.0))).to_i
56 emot = (50.0 * (1.0 + sin((z / 28.0 - (z / 28)) * 360.0 * pi / 180.0))).to_i
57 geist =(50.0 * (1.0 + sin((z / 33.0 - (z / 33)) * 360.0 * pi / 180.0))).to_i
58 return phys, emot, geist
59 end
60
61 def parsedate(s)
62 ParseDate::parsedate(s).select(0, 1, 2)
63 end
64
65 def name_of_week(date)
66 Date::DAYNAMES[date.wday]
67 end
68
69 #
70 # main program
71 #
72 parseArgs(0, nil, "vg", "D:", "sdate", "date:", "birthday:", "days:")
73
74 if $OPT_D
75 dd = Date.today
76 bd = Date.new(*parsedate($OPT_D))
77 ausgabeart = "g"
78 else
79 if $OPT_birthday
80 bd = Date.new(*parsedate($OPT_birthday))
81 else
82 printf(STDERR, "Birthday (YYYYMMDD) : ")
83 if (si = STDIN.gets.chop) != ""
84 bd = Date.new(*parsedate(si))
85 end
86 end
87 if !bd
88 printf STDERR, "BAD Input Birthday!!\n"
89 exit()
90 end
91
92 if $OPT_sdate
93 dd = Date.today
94 elsif $OPT_date
95 dd = Date.new(*parsedate($OPT_date))
96 else
97 printf(STDERR, "Date [<RETURN> for Systemdate] (YYYYMMDD) : ")
98 if (si = STDIN.gets.chop) != ""
99 dd = Date.new(*parsedate(si))
100 end
101 end
102 if !dd
103 dd = Date.today
104 end
105
106 if $OPT_v
107 ausgabeart = "v"
108 elsif $OPT_g
109 ausgabeart = "g"
110 else
111 printf(STDERR, "Values for today or Graph (v/g) [default g] : ")
112 ausgabeart = STDIN.gets.chop
113 end
114 end
115 if (ausgabeart == "v")
116 printHeader(bd.year, bd.month, bd.day, dd - bd, name_of_week(bd))
117 print "\n"
118
119 phys, emot, geist = getPosition(dd - bd)
120 printf "Biorhythm: %04d.%02d.%02d\n", dd.year, dd.month, dd.day
121 printf "Physical: %d%%\n", phys
122 printf "Emotional: %d%%\n", emot
123 printf "Mental: %d%%\n", geist
124 print "\n"
125 else
126 if $OPT_days
127 display_period = $OPT_days.to_i
128 elsif $OPT_D
129 display_period = 9
130 else
131 printf(STDERR, "Graph for how many days [default 10] : ")
132 display_period = STDIN.gets.chop
133 if (display_period == "")
134 display_period = 9
135 else
136 display_period = display_period.to_i - 1
137 end
138 end
139
140 printHeader(bd.year, bd.month, bd.day, dd - bd, name_of_week(bd))
141 print " P=physical, E=emotional, M=mental\n"
142 print " -------------------------+-------------------------\n"
143 print " Bad Condition | Good Condition\n"
144 print " -------------------------+-------------------------\n"
145
146 (dd - bd).step(dd - bd + display_period) do |z|
147 phys, emot, geist = getPosition(z)
148
149 printf "%04d.%02d.%02d : ", dd.year, dd.month, dd.day
150 p = (phys / 2.0 + 0.5).to_i
151 e = (emot / 2.0 + 0.5).to_i
152 g = (geist / 2.0 + 0.5).to_i
153 graph = "." * 51
154 graph[25] = ?|
155 graph[p] = ?P
156 graph[e] = ?E
157 graph[g] = ?M
158 print graph, "\n"
159 dd = dd + 1
160 end
161 print " -------------------------+-------------------------\n\n"
162 end