06-20-2005, 10:56 PM
Enough time has been given to ShiftLynx to fix his entry, and for any others to submit solutions. So, this brings this challenge to a close.
The only solutions that worked were those of Deleter and Neo. During the testing phase, they each enhanced their solutions to a point where even the most stringent testing would not make them fail.
I have to acknowledge both their efforts, and see no other way than to consider them both as equal winners of this challenge. Congratulations Deleter and Neo, and thanks for participating in the challenge and sharing your programming know-how with others.
BTW, here's my own solution for the challenge's problem. You will note that 99% of the program is getting and validating the dates. The actual computation of the age turns out to be 2 lines of code.
*****
The only solutions that worked were those of Deleter and Neo. During the testing phase, they each enhanced their solutions to a point where even the most stringent testing would not make them fail.
I have to acknowledge both their efforts, and see no other way than to consider them both as equal winners of this challenge. Congratulations Deleter and Neo, and thanks for participating in the challenge and sharing your programming know-how with others.
BTW, here's my own solution for the challenge's problem. You will note that 99% of the program is getting and validating the dates. The actual computation of the age turns out to be 2 lines of code.
Code:
DEFINT A-Z
DECLARE FUNCTION NumStrict (Z$)
DECLARE FUNCTION IsLeapYear% (Z)
rem Setup days-per-month table.
DIM ZMO(1 TO 12) AS INTEGER
DATA 31,28,31,30,31,30,31,31,30,31,30,31
FOR ZMM=1 TO 12:READ ZMO(ZMM):NEXT
REM ***********************************************************************
cdate$=date$ 'Get current system date as mm-dd-yyyy
if left$(time$,2)="00" then cdate$=date$ 'make sure date didn't just roll over
rem System date is assumed to be valid.
cyear = val(mid$(cdate$,7,4))
cmmdd$=left$(cdate$,2)+mid$(cdate$,4,2)
cymd$=mid$(cdate$,7,4)+cmmdd$
input "Enter Person's Date of Birth as YYYYMMDD ", birth$
z$=birth$:gosub date.check
if date.ok=0 or birth$>cymd$ then print "Invalid Birth Date=";birth$:system
byear = val(mid$(birth$,1,4))
bmmdd$ = mid$(birth$,5,4)
rem Compute the person's age in years.
age=cyear-byear
if bmmdd$ > cmmdd$ then age=age-1
print "The person's age today is ";age
system
REM **************** DATE.CHECK SUBROUTINE **************************
REM *
REM *** VALIDATE A DATE IN YYYYMMDD FORMAT.
REM *
REM * INPUT: Z$ = Given date in format YYYYMMDD.
REM *
REM * OUTPUT: DATE.OK = -1 if input date is VALID. (true)
REM * 0 if input date is INVALID. (false)
REM *
DATE.CHECK:
DATE.OK = 0 'preset to false
IF NOT NUMSTRICT(Z$) THEN RETURN
ZDD=VAL(RIGHT$(Z$,2)) 'Set day
ZMM=VAL(MID$(Z$,5,2)) 'Set month.
ZYY=VAL(LEFT$(Z$,4)) 'Set year.
IF ZMM<1 OR ZMM>12 OR ZDD<1 OR ZDD>31 THEN RETURN
IF ZMO(ZMM)+1*(-(ZMM=2 AND ISLEAPYEAR(ZYY))) < ZDD THEN RETURN
' If expression (month=2 and is leapyear) is TRUE which is -1, then
' taking the negative of this issues a plus 1. Conversely, FALSE
' always gives a zero. Multiplying the +1 by this result of 1 or 0
' will either add 1 or not to the number of days in the month.
' The logic wants to add 1 only when it is February and leap year.
DATE.OK = -1 '-1=valid (true)
RETURN
END
REM ***** F U N C T I O N S *****
' ====================== ISLEAPYEAR ==========================
' Determines if a year is a leap year or not.
' ============================================================
'
FUNCTION IsLeapYear (Z) STATIC
' If the year is evenly divisible by 4 and not divisible
' by 100, or if the year is evenly divisible by 400, then
' it's a leap year:
IsLeapYear = (Z MOD 4 = 0 AND Z MOD 100 <> 0) OR (Z MOD 400 = 0)
END FUNCTION
' =================================================================
FUNCTION NumStrict (Z$)
REM *
REM *** CHECK FOR STRICTLY NUMERIC (NO NULL, NO NEGATIVE, NO DECIMAL)
REM *
NumStrict=0 'Init to False
IF Z$="" THEN EXIT FUNCTION
FOR X = 1 TO LEN(Z$)
A=ASC(MID$(Z$,X,1))
IF A<48 OR A>57 THEN EXIT FUNCTION
NEXT X
NumStrict = -1 'True
END FUNCTION