Qbasicnews.com
August 26, 2019, 12:21:19 AM *
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 5
  Print  
Author Topic: Challenge: Validate a code  (Read 16860 times)
Moneo
Na_th_an
*****
Posts: 1971


« Reply #30 on: July 28, 2003, 01:28:35 PM »

BLITZ AND ANTONI,
I compiled your original programs using BC with /ah option, and now they run fine.

BLITZ: Excellent, yours runs perfectly. Later you can explain what the 2 dimeensional array is all about.

ANTONI: Very good, also runs well. One very minor problem, you do not alllow the user to specify a code or zero nor a code of 99999, which are within the defined limits.
*****
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #31 on: July 28, 2003, 01:36:03 PM »

ANTONI,
Your "bit array" is exactly my preferred solution. See my program below with a slightly simpler bit handling than yours, that is, no need for a pwrsof2 array.
Code:

REM Code validation program by Edward F. Moneo
DEFINT A-Z
CONST BT.MAXSIZE = 99999
DIM BT.TAB ( 0 TO ((BT.MAXSIZE+1)\16) ) AS INTEGER
DIM BT.BIT  AS LONG
DIM BT.LOC  AS INTEGER
DIM BT.ARG  AS LONG
DIM BT.TRUE AS INTEGER

open "valid.txt" for input as #1
do while not eof(1)
   line input #1,d$
   bt.arg=val(d$)
   gosub bt.set
loop

do
  input "Enter code to be validated (X=exit)";z$
  if ucase$(z$)="X" then system
  gosub validate
  if valid=0 then
     print "ERROR: Non-numeric or out or range input code"
  else
     bt.arg=val(z$)
     gosub bt.test
     if bt.true=0 then print "ERROR: code invalid" else print "ok, code valid"
  end if
loop

'******************************************************************************

BT.SET:
  GOSUB BT.GET
  BT.TAB(BT.LOC) = BT.TAB(BT.LOC) OR BT.BIT
RETURN

BT.TEST:
  GOSUB BT.GET
  BT.TRUE = ( BT.TAB(BT.LOC) AND BT.BIT )    
RETURN

BT.GET:
  BT.BIT = BT.ARG MOD 16
  BT.BIT = 2^BT.BIT
  BT.LOC = BT.ARG\16
RETURN

'BT.RESET:  '(Not used by this program)
'  GOSUB BT.GET
'  BT.TAB(BT.LOC) = BT.TAB(BT.LOC) AND (NOT(BT.BIT))
'RETURN
'  *************************************************************************

'*** CHECK FOR STRICTLY NUMERIC AND LESS/EQUAL TO BT.MAXSIZE.
VALIDATE:
  VALID=0         'Init to False
  IF Z$="" THEN RETURN '-------------------->
  FOR X = 1 TO LEN(Z$)
      A=ASC(MID$(Z$,X,1))
      IF A<48 OR A>57 THEN RETURN '--------->
  NEXT X
  IF VAL(Z$)<=BT.MAXSIZE THEN VALID=-1
RETURN

*****
Logged
Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #32 on: July 28, 2003, 01:52:29 PM »

Blitz:
OK, you won!

Moneo:
I use the array of Pwrsof2 just for speed. In this case this is not an issue but when doing real time graphics (p.e. scaling a font) it is.
Logged

Antoni
Moneo
Na_th_an
*****
Posts: 1971


« Reply #33 on: July 28, 2003, 02:15:21 PM »

Ok, Antoni, what you say makes sense for speed. Once you learn to do something considering speed, you get in the habit of always doing it that way.

P.S.: It's hard to consider Blitz as having won. His last posted program is incomplete. Also, he abandoned his original approach and adopted yours.
*****
Logged
Blitz
I hold this place together
*****
Posts: 853



WWW
« Reply #34 on: July 28, 2003, 02:55:53 PM »

Eh, that was my orginal aproatch. My original one used a huge array of integers which had 100,000 elements. The one antoni wrote the second time was the same as mine but with a bit array instead. My second one same as my first, except with a bit array. So i don't see how it is ME who has abboned their original aproach.

The two dimensional array is bascially the same principal as real mode memory. It's split up into segments. In this case each segment is 16384 integers. That times 13 gives us over 100,000 elements to work with.  To calculate the element index as a two dimensional array we do this indxa = element \ 16384, indxb = element mod 16384.
Logged

oship me and i will give you lots of guurrls and beeea
Moneo
Na_th_an
*****
Posts: 1971


« Reply #35 on: July 28, 2003, 03:01:38 PM »

Ok, sorry, Blitz. It's just that you've posted several versions including benchmark versions, so I'm a little confused as to which is your lastest working version. Would you please post your latest, running version so I can try it again.
*****
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #36 on: July 28, 2003, 06:21:36 PM »

i'm gonna try this, now that Moneo has explained what it's all about.

And, stop spamming, Blitzilla.
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #37 on: July 28, 2003, 06:35:02 PM »

Aga,

Your right, I didn't explain everything up front this time, like I usually do. If I do too much explaining, then I get accused of posting a challenge with too many "constraints". I'm damned if I do, and damned if I don't.

Ok, so go ahead and try this one out, now that you know.
*****
Logged
Meg
Ancient QBer
****
Posts: 483


« Reply #38 on: July 28, 2003, 07:01:42 PM »

How's this for cramming the codes into as little memory as possible?  No arrays Smiley  (this outta be good for a laugh, at least!)

Code:
DECLARE FUNCTION GetCode& ()

CLS
SCREEN 11

OPEN "VALID.TXT" FOR INPUT AS #1
     FOR i% = 1 TO 20000
          INPUT #1, n&
          PSET (n& MOD 640, n& \ 640), 15
     NEXT i%
CLOSE #1

DO
     UserCode& = GetCode&
     PRINT UserCode&; "is ";
     IF POINT(UserCode& MOD 640, UserCode& \ 640) THEN
          PRINT "Valid.              "
     ELSE
          PRINT "Invalid.            "
     END IF
LOOP

FUNCTION GetCode&
   
     LOCATE 12, 1: PRINT SPC(79);
     LOCATE 12, 1: PRINT "Enter Code: ";

     Code& = 0
     DO
          LOCATE 12, 13: PRINT Code&; " "
          DO
               c$ = INKEY$
          LOOP UNTIL c$ <> ""
          IF c$ = CHR$(27) THEN SYSTEM
          IF c$ = CHR$(13) THEN EXIT DO
          IF c$ = CHR$(8) THEN Code& = Code& \ 10
          IF c$ >= "0" AND c$ <= "9" THEN
               IF Code& <= 9999 THEN Code& = Code& * 10 + VAL(c$)
          END IF
     LOOP

     GetCode& = Code&

END FUNCTION



*peace*

Meg.
Logged
Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #39 on: July 28, 2003, 07:22:52 PM »

Fine idea, Meg!

This reminds me old times. I once used screen memory to run an assembler program in a Sinclair ZX Spectrum, back in 1985.... The idea was to leave all conventional memory to load a game (from an audio cassette), then my program made an (illegal) copy of it..
Logged

Antoni
Moneo
Na_th_an
*****
Posts: 1971


« Reply #40 on: July 28, 2003, 09:28:55 PM »

Ok, Meg, your approach sounds ingenious. I've never used PSET and POINT commands, so I don't really understand how it's supposed to work.

The fact is, however, that it doesn´t work. It only likes small code numbers less than 500. I made several versions of the VALID.TXT file but any valid number greater than 400 is marked as invalid by your program. Must be a minor coding error. Or maybe I need to add some switch to the BC compiler.
*****
Logged
Meg
Ancient QBer
****
Posts: 483


« Reply #41 on: July 28, 2003, 10:45:06 PM »

Yeah I had a "480" instead of a "640"; just a typo.  All fixed, now.  You don't need any fancy switches or anything.  It just uses the screen to store the information, which means you don't need to create any additional arrays.

The code is pretty straightforward.  Open up the data file, read in the values, and plot points on the screen:

i.e.

if the number read in = 0, plot a point at (0, 0)
if the number read in = 1, plot a point at (1, 0)
if the number read in = 639, plot a point at (639, 0)
if the number read in = 640, plot a point at (0, 1)


and so forth..

then, when the user enters codes, it just uses the POINT command to see whether the pixel which corresponds to that number has been lit up or not.

*peace*

Meg.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #42 on: July 28, 2003, 11:17:40 PM »

MEG:
Fantastic, It works 100%. Congratulations. Brilliant idea.

I think we'll wait a day to see if Aga comes up with a solution, and then we'll declare a winner, which will be tough.
*****
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #43 on: July 29, 2003, 01:09:06 PM »

Signed integers are so much nicer sometimes...

Blah.. I might not post anything... if i convert my array to LONG, it won't go as fast....
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #44 on: July 29, 2003, 02:32:09 PM »

Aga, don't be concerned with speed for this. We're looking for design and simplicity.

You must take into consideration that this program has a user interface. So, the user is not going to notice the difference of a few microseconds.
*****
Logged
Pages: 1 2 [3] 4 5
  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!