Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Unique Solutions
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00109528
Message ID:
00109791
Vues:
45
>>I have a 5 X 5 grid with each cell having a value of 1 or 0 depending on whether the cell is 'active' or 'inactive'. If 5 cells are active consecutively then it forms a row or column whichever the case may be. Under these conditioins there are 13 possible situations to take into account: 5 rows, 5 columns, 2 diagonals and the four corners plus the center square. I need to find a solution to determine which two are active or three or four for that matter without having to check something like if A = 1 and B =1, if A = 1 and C = 1, and so on....any ideas?
>>
>>Thanks.
>
>
>You can try to align all zeros and ones in a single row and bitmask the result to test each situations...
>
>HTH :0)

After my reply, i go to VFP and try to change a string of 0 and 1 to a number and i don't find any fox function to do the job!

I decided to build it and do some test with your problem in mind...

Here is the results:
*-----------------------------------------------
* Build a string with each 0 and 1 of your grid
* and test your string with each values.
*-----------------------------------------------
*	Sample:
*	grid:	10011
*			01101
*			11111
*			00110
*			00001
*
*	test string:'1001101101111110011000001'
*
*	? CompareBits(TestString,line1) return .F.
*	? CompareBits(TestString,diag1) return .T.
*------------------------------------------------
*
*!*	line1=  '1111100000000000000000000' || 32505856
*!*	line2=  '0000011111000000000000000' ||  1015808
*!*	line3=  '0000000000111110000000000' ||    31744
*!*	line4=  '0000000000000001111100000' ||      922
*!*	line5=  '0000000000000000000011111' ||       31
*!*	col1=   '1000010000100001000010000' || 17318416
*!*	col2=   '0100001000010000100001000' ||  8659208
*!*	col3=   '0010000100001000010000100' ||  4329604
*!*	col4=   '0001000010000100001000010' ||  2164802
*!*	col5=   '0000100001000010000100001' ||  1082401
*!*	diag1=  '1000001000001000001000001' || 17043521
*!*	diag2=  '0000100010001000100010000' ||  1118480
*!*	corners='1000100000001000000010001' || 17829905
*
********************

********************
*
* On my Pentium 150
*	numeric test take 13 secs
*	character test take 22 sec
* 	for 10,000 calls
*
********************
FUNCTION BenchMark
test='1111110000101010010010101'
*Character test
corners='1000100000001000000010001'
nsec1=SECONDS()
FOR iii = 1 TO 10000
	CompareBits(test,corners)
ENDFOR
nEnd1=SECONDS()
*Numeric test
corners=17829905
nsec2=SECONDS()
FOR iii = 1 TO 10000
	CompareBits(test,corners)
ENDFOR
nEnd2=SECONDS()
*Show results
WAIT WIND 'Character:'+STR(nEnd1-nsec1)+'  Numeric:'+STR(nEnd2-nsec2)
RETURN
*************

FUNCTION StrBitToNumber
* Gérald Santerre / june 1998
*----------------------------
* param1: string of 0 and 1 to transform to number
* return: the number
*----------------------------
LPARAM tcBinString
IF TYPE('tcBinString') <> 'C'
	RETURN 0
ENDIF
LOCAL lnNumber,lnLen,lnBit
lnNumber=0

lnLen=LEN(tcBinString)

FOR i = 1 TO lnLen
	lnBit=VAL(SUBSTR(tcBinString,i,1))
	IF lnBit=1
		lnNumber=BITSET(lnNumber,lnLen-i)
	ENDIF
ENDFOR

RETURN lnNumber

*********************************
FUNCTION CompareBits
* Gérald Santerre / june 1998
*-----------------------------
* param1: string of 0 and 1 to test
* param2: value or string to compare (bitmask) with param1
* return: .T. if all bits set to 1 in param2 are set to 1 in param1
*-----------------------------
LPARAM tcStrBitToTest,tuStrBitMask
LOCAL lnTest,lnMask
IF TYPE('tcStrBitToTest') <> 'C'
	RETURN .F.
ENDIF
DO CASE
	CASE TYPE('tuStrBitMask') = 'C'
		lnMask=StrBitToNumber(tuStrBitMask)
	CASE TYPE('tuStrBitMask') = 'N'
		lnMask=tuStrBitMask
	OTHERWISE
		RETURN .F.
ENDCASE
lnTest=StrBitToNumber(tcStrBitToTest)
RETURN lnMask = BITAND(lnTest,lnMask)
HTH :0)
If we exchange an apple, we both get an apple.
But if we exchange an idea, we both get 2 ideas, cool...


Gérald Santerre
Independant programmer - internet or intranet stuff - always looking for contracts big or small :)
http://www.siteintranet.qc.ca
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform