Moneo
|
 |
« on: July 16, 2003, 04:08:18 PM » |
|
Given: 1) Two date strings, D1$ and D2$. 2) They should be in the format: YYYYMMDD. 3) You must check them for validity, e.g., 20030229 and 20030931 and 1999019 are all invalid. 4) Once they're valid, figure out how to compute the number of days (positive or negative) of D2$ minus D1$. *****
|
|
|
Logged
|
|
|
|
Ninkazu
|
 |
« Reply #1 on: July 16, 2003, 04:09:47 PM » |
|
Come on, let's do something more fun than parsing. Blehh.
|
|
|
Logged
|
am an asshole. Get used to it.
|
|
|
Moneo
|
 |
« Reply #2 on: July 16, 2003, 04:13:33 PM » |
|
NINKAZU, Why do you consider this as a parsing problem? Only a small part the date validation could be considered parsing. The rest is computational. *****
|
|
|
Logged
|
|
|
|
Ninkazu
|
 |
« Reply #3 on: July 16, 2003, 04:14:13 PM » |
|
|
|
|
Logged
|
am an asshole. Get used to it.
|
|
|
Moneo
|
 |
« Reply #4 on: July 16, 2003, 04:16:13 PM » |
|
Sorry 'bout that --- I fixed it. *****
|
|
|
Logged
|
|
|
|
Meg
|
 |
« Reply #5 on: July 16, 2003, 08:40:42 PM » |
|
I am becoming way too addicted to these programming challenges. I have so much other stuff I should be doing..... :-? Anyways, here's my entry for the Days-Between-Two-Dates challenge. I hope it works alright! *peace* Meg. DECLARE FUNCTION TotDays& (Start$, End$) DECLARE FUNCTION EnterDate$ (Prompt$) DECLARE FUNCTION DInM% (Month%, Year%)
'********************************************************************** ' Days between two dates calculator. Written by Meg Berry, July 2003. '**********************************************************************
CLS D1$ = EnterDate$("First Date") D2$ = EnterDate$("Second Date")
PRINT : PRINT "There are "; IF D1$ > D2$ THEN PRINT -TotDays&(D2$, D1$); ELSEIF D2$ > D1$ THEN PRINT TotDays&(D1$, D2$); ELSE PRINT 0; END IF PRINT "days between those dates."
SYSTEM
FUNCTION DInM% (m%, y%)
'*************************** OBJECTIVE ******************************** 'This function determines the number of days in a given month and year. '**********************************************************************
'************************* ARGUMENT LIST ****************************** 'm% month to check 'y% year to check '**********************************************************************
'*** DETERMINE USUAL DAYS IN THE MONTH *** DInM% = 31 + (m% MOD 2) * (m% > 7) + (m% MOD 2 XOR 1) * (m% < 8) '*** IF FEB. THEN ADJUST DAYS DEPENDING ON LEAP YEAR *** IF m% = 2 THEN DInM% = 28 - ((y% MOD 4 = 0) + (y% MOD 100 = 0) * NOT (y% MOD 400 = 0)) END FUNCTION
FUNCTION EnterDate$ (Prompt$)
'*************************** OBJECTIVE ******************************** 'This function returns a valid date string '********************************************************************** '************************* ARGUMENT LIST ****************************** 'Prompt$ Message passed to be displayed before the INPUT command '**********************************************************************
'************************* VARIABLE LIST ****************************** 'EM$ Standard error message 'd$ Temporary storage of date 'check$ String used to check for non-numeric characters 'y% year, extracted from d$ 'm% month, extracted from d$ 'd% day, extracted from d$ '**********************************************************************
Em$ = " Please enter a date between 00010101 and 99991231." DO PRINT PRINT Prompt$ LINE INPUT "Enter Date (yyyymmdd): ", d$ FOR Temp% = 1 TO 1 IF LEN(d$) <> 8 THEN PRINT "Invalid Length." + Em$: EXIT FOR check$ = RIGHT$(STR$(INT(VAL(d$))), LEN(STR$(INT(VAL(d$)))) - 1) check$ = LEFT$("00000000", LEN(d$) - LEN(check$)) + check$ IF check$ <> d$ THEN PRINT "Invalid Character." + Em$: EXIT FOR y% = VAL(LEFT$(d$, 4)) m% = VAL(MID$(d$, 5, 2)) d% = VAL(RIGHT$(d$, 2)) IF y% < 1 THEN PRINT "Invalid Year." + Em$: EXIT FOR IF m% < 1 OR m% > 12 THEN PRINT "Invalid Month." + Em$: EXIT FOR IF d% < 1 OR d% > DInM%(m%, y%) THEN PRINT "Invalid Day." + Em$: EXIT FOR EXIT DO NEXT Temp% LOOP EnterDate$ = d$
END FUNCTION
FUNCTION TotDays& (S$, E$) '*************************** OBJECTIVE ******************************** 'This function returns the number of days between a start and end date. '**********************************************************************
'************************* ARGUMENT LIST ****************************** 'S$ starting date 'E$ ending date '**********************************************************************
'************************* VARIABLE LIST ****************************** 'Sy% Sm% Sd% starting year, month, and day 'Ey% Em% Ed% ending year, month, and day 'Count& day counter '********************************************************************** Sy% = VAL(LEFT$(S$, 4)) Ey% = VAL(LEFT$(E$, 4)) Sm% = VAL(MID$(S$, 5, 2)) Em% = VAL(MID$(E$, 5, 2)) Sd% = VAL(RIGHT$(S$, 2)) Ed% = VAL(RIGHT$(E$, 2))
IF Sy% = Ey% AND Sm% = Em% THEN Count& = Ed% - Sd% ELSE Count& = DInM%(Sm%, Sy%) - Sd% + Ed% DO UNTIL ((Sy% = Ey%) AND (Sm% = Em% - 1)) OR ((Sy% = Ey% - 1) AND (Sm% = 12) AND (Em% = 1)) Sm% = Sm% + 1 IF Sm% = 13 THEN Sm% = 1: Sy% = Sy% + 1 Count& = Count& + DInM%(Sm%, Sy%) LOOP END IF
TotDays& = Count& END FUNCTION
|
|
|
Logged
|
|
|
|
Meg
|
 |
« Reply #6 on: July 16, 2003, 08:53:19 PM » |
|
is there a way to make posts so that they don't wrap in the post box? I thought I've seen posts that had horizontal scroll bars?
I hate when the forum word-wraps my code :p
*peace*
Meg.
|
|
|
Logged
|
|
|
|
oracle
|
 |
« Reply #7 on: July 16, 2003, 09:00:03 PM » |
|
Whenever I post a long line of code in [code] tags, it doesn't bother line-wrapping it. That's why you see scroll bars. But it doesn't really matter does it? All people to is copy and paste the code into notepad and it's correctly formatted.
|
|
|
Logged
|
|
|
|
Moneo
|
 |
« Reply #8 on: July 16, 2003, 09:09:47 PM » |
|
...Anyways, here's my entry for the Days-Between-Two-Dates challenge. I hope it works alright! Does that mean that you haven't checked it out yourself? :wink: MEG: You are incredible! I checked your program out and it works exactly to specifications. Did you do this unbelievable program from scratch in less than 4-5 hours, or did you "lift" some logic from some of your other programs? In any event, great job! *****
|
|
|
Logged
|
|
|
|
Meg
|
 |
« Reply #9 on: July 17, 2003, 01:01:37 AM » |
|
I coded the thing from scratch. It took about an hour and a half. I had to look up how to determine a leap year, but the logic line for it (the two lines in the DInM% function) are original.
There actually were a couple logic errors in the original post (notice it's been edited two times hehehe). I think they're worked out, now.
*peace*
Meg.
|
|
|
Logged
|
|
|
|
Meg
|
 |
« Reply #10 on: July 17, 2003, 01:07:54 AM » |
|
I considered adding a check for the 10-day drop (Oct. 4 to 15, in the year 1582) but this might have been going overboard  *peace* Meg.
|
|
|
Logged
|
|
|
|
oracle
|
 |
« Reply #11 on: July 17, 2003, 09:48:20 PM » |
|
I considered adding a check for the 10-day drop (Oct. 4 to 15, in the year 1582) but this might have been going overboard  *peace* Meg. Ahem.... maybe. But if you could that proggie would be no. 1 in QBasic for that (do you want to be the authority on something?  )
|
|
|
Logged
|
|
|
|
na_th_an
|
 |
« Reply #12 on: July 17, 2003, 10:35:00 PM » |
|
I considered adding a check for the 10-day drop (Oct. 4 to 15, in the year 1582) but this might have been going overboard  *peace* Meg. Are you saying that days from Oct.5 to Oct 14, in 1582, didn't happen? 
|
|
|
Logged
|
|
|
|
Meg
|
 |
« Reply #13 on: July 17, 2003, 10:58:37 PM » |
|
According to the Gregorian calendar, the day after October 4th, 1582 was October 15th, 1582 to compensate for the Year actually being slightly less than 365.25 days long (which is why now leap years DO occur on 1600, 2000, etc. but NOT on 1700, 1800, etc. They did before this, and it was throwing off the calendar little by little.) So they skipped 10 days in this year to line everything back up. Well, different countries adopting the Gregorian calendars shifted in different years, but the original Gregorian calendar had it in 1582. :lol: *peace* Meg. p.s. no, I don't know all this. I looked it up. 
|
|
|
Logged
|
|
|
|
toonski84
|
 |
« Reply #14 on: July 17, 2003, 11:05:23 PM » |
|
The way I always thought it was that the leap year is either skipped or extended every 128 years to adjust for the earth's funky rotation cycle. but it's pretty cool that people were smart enough to adjust dates based on the earth's rotation as early as 1582. for heretic-burning mummy drinkers they seem to have their head on straight  oh, and meg: that code might be a little smaller if you converted the gregorian day to julian and just subtracted 
|
|
|
Logged
|
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
|
|
|
|