~ubuntu-branches/ubuntu/vivid/inform/vivid

« back to all changes in this revision

Viewing changes to html/answers2/answer72.html

  • Committer: Bazaar Package Importer
  • Author(s): Jan Christoph Nordholz
  • Date: 2008-05-26 22:09:44 UTC
  • mfrom: (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526220944-ba7phz0d1k4vo7wx
Tags: 6.31.1+dfsg-1
* Remove a considerable number of files from the package
  due to unacceptable licensing terms.
* Repair library symlinks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<HTML><HEAD><TITLE>Answer to Exercise 72</TITLE></HEAD>
2
 
<BODY BGCOLOR="#FFFFFF">
3
 
<TABLE><TR><TD Valign="top"><IMG SRC="../icons/exercise.gif" ALT="??"><TD bgcolor="#FAA89B"><B>ANSWER TO EXERCISE 72</B><TR><TD><TD>
4
 
<P>
5
 
 
6
 
 
7
 
The time of day will
8
 
be returned as a number in the usual Inform time format:
9
 
as hours times 60 plus minutes (on the 24-hour clock, so
10
 
that the 'hour' part is between 0 and 23).
11
 
<PRE>
12
 
Constant TWELVE_HOURS = 720;
13
 
[ NumericTime hr mn word x;
14
 
  if (hr&#62;=24) return -1;
15
 
  if (mn&#62;=60) return -1;
16
 
  x=hr*60+mn; if (hr&#62;=13) return x;
17
 
  x=x%TWELVE_HOURS; if (word=='pm') x=x+TWELVE_HOURS;
18
 
  if (word~='am' or 'pm' &#38;&#38; hr==12) x=x+TWELVE_HOURS;
19
 
  return x;
20
 
];
21
 
[ MyTryNumber wordnum i j;
22
 
  i=wn; wn=wordnum; j=NextWordStopped(); wn=i;
23
 
  switch(j)
24
 
  {   'twenty-five': return 25;
25
 
      'thirty': return 30;
26
 
      default: return TryNumber(wordnum);
27
 
  }
28
 
];
29
 
[ TimeOfDay i j k flag loop ch hr mn;
30
 
  i=NextWord();
31
 
  switch(i)
32
 
  {  'midnight': parsed_number=0; return 1;
33
 
     'midday', 'noon': parsed_number=TWELVE_HOURS; return 1;
34
 
  }
35
 
  !   Next try the format 12:02
36
 
  j=WordAddress(wn-1); k=WordLength(wn-1);
37
 
  flag=0;
38
 
  for (loop=0:loop&#60;k:loop++)
39
 
  {   ch=j-&#62;loop;
40
 
      if (ch==':' &#38;&#38; flag==0 &#38;&#38; loop~=0 &#38;&#38; loop~=k-1) flag=1;
41
 
      else { if (ch&#60;'0') flag=-1; if (ch&#62;'9') flag=-1; }
42
 
  }
43
 
  if (k&#60;3) flag=0; if (k&#62;5) flag=0;
44
 
  if (flag==1)
45
 
  {   for (loop=0:j-&#62;loop~=':':loop++, hr=hr*10)
46
 
          hr=hr+j-&#62;loop-'0';
47
 
      hr=hr/10;
48
 
      for (loop++:loop&#60;k:loop++, mn=mn*10)
49
 
          mn=mn+j-&#62;loop-'0';
50
 
      mn=mn/10;
51
 
      j=NextWordStopped();
52
 
      parsed_number=NumericTime(hr, mn, j);
53
 
      if (parsed_number&#60;0) return -1;
54
 
      if (j~='pm' or 'am') wn--;
55
 
      return 1;
56
 
  }
57
 
  !   Next the format "half past 12"
58
 
  j=-1; if (i=='half') j=30; if (i=='quarter') j=15;
59
 
  if (j&#60;0) j=MyTryNumber(wn-1); if (j&#60;0) return -1;
60
 
  if (j&#62;=60) return -1;
61
 
  k=NextWordStopped();
62
 
  if (k==-1)
63
 
  {   hr=j; if (hr&#62;12) return -1; jump TimeFound; }
64
 
  if (k=='o^clock' or 'am' or 'pm')
65
 
  {   hr=j; if (hr&#62;12) return -1; jump TimeFound; }
66
 
  if (k=='to' or 'past')
67
 
  {   mn=j; hr=MyTryNumber(wn);
68
 
      if (hr&#60;=0)
69
 
      {   switch(NextWordStopped())
70
 
          {   'noon', 'midday': hr=12;
71
 
              'midnight': hr=0;
72
 
              default: return -1;
73
 
          }
74
 
      }
75
 
      if (hr&#62;=13) return -1;
76
 
      if (k=='to') { mn=60-mn; hr=hr-1; if (hr==-1) hr=23; }
77
 
      wn++; k=NextWordStopped();
78
 
      jump TimeFound;
79
 
  }
80
 
  hr=j; mn=MyTryNumber(--wn);
81
 
  if (mn&#60;0) return -1; if (mn&#62;=60) return -1;
82
 
  wn++; k=NextWordStopped();
83
 
 .TimeFound;
84
 
  parsed_number = NumericTime(hr, mn, k);
85
 
  if (parsed_number&#60;0) return -1;
86
 
  if (k~='pm' or 'am' or 'o^clock') wn--;
87
 
  return 1;
88
 
];
89
 
</PRE>
90
 
 
91
 
</TABLE>
92
 
<HR>Back to <A HREF="../section27.html#ex72">the exercise in section 27</A><HR>
93
 
<SMALL><I>Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright &#169; Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.</I></SMALL></BODY></HTML>