Qbasicnews.com
December 05, 2019, 04:15:53 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 2 [3] 4
  Print  
Author Topic: Write a bulletproof date validation routine.  (Read 36066 times)
Neo
Na_th_an
*****
Posts: 2150



« Reply #30 on: October 29, 2004, 06:28:32 AM »

Quote from: "Moneo"
Looks interesting. Give me some time to run a test on it (more than 20 minutes) like I did with Meg's solution. I'll get back to you.

So, how's it getting along? I hope it works (although it might be a bit slower than Meg's solution, due to the fact I implemented more checks) Wink I'm glad the solution looked interesting Smiley

Quote from: "The Oracle"
Orthoganality is like the concept of independence in statistics, as well as a concept from gemotry I think . Object orientated libraries are a prime example of this - plug it in, read the API documentation, code your use of it. Performance gains using this method are huge

Exactly Wink I'm not experienced in statistics, but in geometry e.g. most axis systems are orthogonal.
Code:
y
 ^
 |
 |
 |
 |
 +---->
       x

The common Carthesian coordinate system is orthogonal as well. When you have coordinate in this coordinate system, like (1,1), changing the x-value of this coordinate (i.e. sliding it left/right), doesn't affect the y-value of the coordinate. And vice versa changing the y-coordinate doesn't affect the x-coordinate. Carthesian is orthogonal.

Now you'd say, aren't all axis systems orthogonal? No, like this para-carthesian:
Code:
      y
      _
     /|
    /
   /
  /
 +---->
       x

In this coordinate system, the y-coordinate is the intersect y-value of the perpendicular line through the y-axis. In this case, if you change x, y changes as well, and vice versa.
Wink
Of course, OOP was invented to increase orthogonality Wink
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #31 on: October 30, 2004, 03:13:39 PM »

NEO:
Ok, I ran the same test on your version as with Meg's, and it ran without any errors. Congrats!
I don't like your code for computing days in month. It works, but I consider it to be extremely complex. What's wrong with a table of days per month like in Meg's final version? Even a Cobol programmer could understand that. :wink:


MEG:
Yes, this thread has become like a programmers' cocktail party --- who can impress who.

*****
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #32 on: October 31, 2004, 05:21:41 PM »

Moneo:
Thanks for testing! Wink I'm glad it worked perfectly!
I know you don't like my super-binary-logic-days-in-month-computation, I already expected it Smiley
Ok, to make you happy... Wink
[syntax="QBasic"]SELECT CASE Month
   CASE 1, 3, 5, 7, 8, 10, 12: MaxDays = 31
   CASE 2: MaxDays = 28 - IsLeapYear
   CASE 4, 6, 9, 11: MaxDays = 30
END SELECT[/syntax]
Everyone can indeed understand this... Wink Perhaps even a Cobol programmer...

Quote from: "Moneo"
Yes, this thread has become like a programmers' cocktail party --- who can impress who.

Sorry. I just had to bring in something Wink
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #33 on: October 31, 2004, 08:29:12 PM »

Neo, I really like your CASE MONTH solution. Can I borrow it next time I need it? Thanks.
*****
Logged
Z!re
*/-\*
*****
Posts: 4599


« Reply #34 on: October 31, 2004, 08:43:46 PM »

Quote from: "Moneo"
Neo, I really like your CASE MONTH solution. Can I borrow it next time I need it? Thanks.
*****



Only in the qmmunity would you see such a thing... Publicly posted code, and people asking for permission to use it. Only in the Qmmunity... Smiley
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #35 on: November 01, 2004, 04:13:27 AM »

Quote from: "Moneo"
Neo, I really like your CASE MONTH solution. Can I borrow it next time I need it? Thanks.
*****

Of course, go ahead Cheesy But make sure the IsLeapYear variable is either -1 (TRUE) or 0 (FALSE). If you don't know this sure, you can use this line instead:
[syntax="QBasic"]   CASE 2: MaxDays = 28 + ABS(IsLeapYear)[/syntax]
Use the code when you deem necessary Wink (Woohoo! Moneo wants some of my code! Cheesy)

Quote from: "Z!re"
Only in the qmmunity would you see such a thing... Publicly posted code, and people asking for permission to use it. Only in the Qmmunity...

Only here...
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #36 on: November 01, 2004, 08:14:47 AM »

Just a note, ToohTooh posted a similar solution in the 14th post on the first page of this thread:
[syntax="QBasic"]SELECT CASE Mo
CASE 1, 3, 5, 7, 8, 10, 12: MaxDayForMonth = 31
CASE 4, 6, 9, 11: MaxDayForMonth = 30
'-- Sniff the leap year case for February.
CASE 2: IF (IsALeapYear(Ye)) THEN MaxDayForMonth = 29 ELSE MaxDayForMonth = 28
'-- Think about below:
'    Q: Why 100 but not 99?  A: Tokenize() will have trapped it.
'    Q: Do we need it?       A: Both yes and no. Think.
CASE ELSE: MaxDayForMonth = 100  '>> Just being safe...
END SELECT[/syntax]

I just found out a few minutes ago. The URL is http://forum.qbasicnews.com/viewtopic.php?p=80848#80848

ToohTooh: I hope you understand.
Logged
ToohTooh
New Member

Posts: 24



« Reply #37 on: November 01, 2004, 08:54:40 AM »

Your watchful eye, enthusiasm on computing, and deep knowledge should be not called a divine model, but what?..

"Keep walking..."
Logged

Don't interrupt me while I'm interrupting." - Winston S. Churchill
Neo
Na_th_an
*****
Posts: 2150



« Reply #38 on: November 01, 2004, 08:57:45 AM »

Thanks ToohTooh for your inspiring information. Wink

Btw, wasn't "Keep walking" from "Johnny Walker"? Or how was he called? Tongue
Logged
dark ninja
New Member

Posts: 12



« Reply #39 on: November 19, 2004, 10:10:03 PM »

I have no idea what you all are talking about now, but my program's worked on everything I've tried so far.
Code:

SUB DateVerify(FullDate$)
IF LEN(FullDate$) <> 8 THEN PRINT "Invalid": END
year$ = LEFT$(FullDate$, 4)
month$ = MID$(FullDate$, 5, 2)
day$ = RIGHT$(FullDate$, 2)
year = VAL(year$)
month = VAL(month$)
day = VAL(day$)
IF year <> INT(year) OR month <> INT(month) OR day <> INT(day) THEN PRINT "Invalid": END

IF year > 3999 OR year < 1600 THEN PRINT "Invalid": END
IF month < 1 OR month > 12 THEN PRINT "Invalid": END
IF day < 28 OR day > 31 THEN PRINT "Invalid": END
IF month = 2 THEN
  IF year / 4 = INT(year / 4) THEN
    IF day > 29 THEN PRINT "Invalid": END
  ELSE
    IF day > 28 THEN PRINT "Invalid": END
  END IF
END IF

IF month = 4 OR month = 6 OR month = 9 OR month = 11 THEN
  IF day > 30 THEN PRINT "Invalid": END
END IF
PRINT "Valid": END
END SUB


I wrote the whole thing as one program, so the SUB commands might not have perfect syntax.  I also made a point to not look at anybody else's work before I made mine, so any similarities in design are coincidental.
Logged

'm boycotting signatures.
Neo
Na_th_an
*****
Posts: 2150



« Reply #40 on: November 20, 2004, 09:50:51 AM »

Quote from: "dark ninja"
Code:
IF year / 4 = INT(year / 4) THEN

Dark Ninja, your code for testing whether a year is a leap year is incorrect. The definition of a leap year (they added something to it, but I can't remember what) is:
Leapyear
#1. Factor of 4
#2. Not a factor of 100
#3. Rule 2 drops if year is factor of 400

So, the proper code would be:
Code:
LeapYear = ((Year MOD 4 = 0 AND Year MOD 100 <> 0) OR (Year MOD 400 = 0))
IF LeapYear THEN blablabla


Hope you can do something with it Wink

EDIT:
Oh and:
Code:
IF day < 28 OR

Why are days lower than 28 invalid? :lol: That means your program will output 20000101 (1st Jan. 2000) as an invalid date.
Logged
dark ninja
New Member

Posts: 12



« Reply #41 on: November 20, 2004, 10:29:25 AM »

Thanks, easily fixed.
Code:

SUB DateVerify(FullDate$)
IF LEN(FullDate$) <> 8 THEN PRINT "Invalid": END
year$ = LEFT$(FullDate$, 4)
month$ = MID$(FullDate$, 5, 2)
day$ = RIGHT$(FullDate$, 2)
year = VAL(year$)
month = VAL(month$)
day = VAL(day$)
IF year <> INT(year) OR month <> INT(month) OR day <> INT(day) THEN PRINT "Invalid": END

IF year > 3999 OR year < 1600 THEN PRINT "Invalid": END
IF month < 1 OR month > 12 THEN PRINT "Invalid": END
IF day > 31 THEN PRINT "Invalid": END
IF month = 2 THEN
  IF year / 4 = INT(year / 4)  THEN
     Leap$ = "yes"
     IF year / 100 = INT(year / 100) THEN
       Leap$ = "no"
       IF year / 400 = INT(year/400) THEN Leap$ = "yes"
     END IF
  END IF
  IF (day > 28 AND leap$ = "no") OR (day > 29 AND leap$ = "yes")   THEN
    PRINT "Invalid": END
  END IF
END IF
       
IF month = 4 OR month = 6 OR month = 9 OR month = 11 THEN
  IF day > 30 THEN PRINT "Invalid": END
END IF
PRINT "Valid": END
END SUB
Logged

'm boycotting signatures.
Neo
Na_th_an
*****
Posts: 2150



« Reply #42 on: November 20, 2004, 01:30:36 PM »

Erm... the code looks functional, but there are still some things that could be improved Wink

#1. I don't like my program to quit when I run your function (d'oh Cheesy)

#2. It can be made much faster if you use integers.

Moneo, Meg, add comments if you wish Smiley
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #43 on: November 20, 2004, 11:23:16 PM »

Quote from: "Neo"
Erm... the code looks functional, but there are still some things that could be improved Wink

#1. I don't like my program to quit when I run your function (d'oh Cheesy)

#2. It can be made much faster if you use integers.

Moneo, Meg, add comments if you wish Smiley

Neo,
"... the code looks functional" --- you're being so kind.

About #2 above: Speed is really not an issue, but it is nice to write the speediest code you can, just as a matter of habit.

The same applies to writing tight, straight-line code with as few if's as possible. The more code your program has, and especially the more if's, then the more chances of bugs. Just like our friend DarkNinja's code; his enhanced leap year logic has so many if's, that I don't even want to bother checking it.
*****
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #44 on: November 21, 2004, 09:12:46 AM »

Quote from: "Moneo"
Neo,
"... the code looks functional" --- you're being so kind.

I am always kind... Wink

Quote from: "Moneo"
About #2 above: Speed is really not an issue, but it is nice to write the speediest code you can, just as a matter of habit.

Yeah, that is true, but I implicitly referred to Dark Ninja using strings as temporary storage (leapyear$ = "yes", leapyear$ = "no"). It's much less efficient than just leapyear = 0 or leapyear = -1.
It doesn't need to be ultrafast, just clean code with an acceptable algorithm.

Quote from: "Moneo"
The same applies to writing tight, straight-line code with as few if's as possible. The more code your program has, and especially the more if's, then the more chances of bugs. Just like our friend DarkNinja's code; his enhanced leap year logic has so many if's, that I don't even want to bother checking it.

I had the same thoughts.
Logged
Pages: 1 2 [3] 4
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!