07-28-2003, 09:36 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.*****
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