#APPTYPE CONSOLE
FOR DIM i = 0 TO 100
PRINT octal(i), " ";
NEXT
PAUSE
FUNCTION octal(n AS INTEGER)
DIM m AS INTEGER = n
DIM r AS STRING = ""
DO
r = (m BAND 7) & r
m = m >> 3
LOOP UNTIL m = 0
RETURN r
END FUNCTION
FUNCTION asbase(n AS INTEGER, base AS INTEGER)
IF n < 2 OR n > 36 THEN
EXIT FUNCTION
END IF
DIM m AS INTEGER = n
DIM d AS INTEGER
CONST digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
DIM r AS STRING = ""
DO
d = m MOD base
r = digits{d + 1} & r
m = m \ base
LOOP UNTIL m = 0
RETURN r
END FUNCTION
r = 7
m MOD (7 & r) <== concatenate to 77 and compute the final MOD; or
(m MOD 7) & r <== compute MOD then concatenate it to r
Mike Lobanovsky wrote:As for the precedence, there is no such notion applicable to mixed cases of arithmetic expressions concatenated to string variables. Everything depends on the programmer's intention which action should be the first to take, and which one, the second.
#apptype console
print 2 + 3 * 5
print (2 + 3) * 5
print 2 + (3 * 5)
pause
#APPTYPE CONSOLE
FOR DIM i = 1 TO 10
PRINT i;
IF i < 10 THEN PRINT ", ";
NEXT
PAUSE
#APPTYPE CONSOLE
FOR DIM i = 10 DOWNTO 1
PRINT i;
IF i > 1 THEN PRINT ", ";
NEXT
PAUSE
Mike Lobanovsky wrote:Hello Bruce,
I have no objections to your solution in principle though it looks too structured for what we usually see in our everyday practice. Using volatiles and "ForEach Dim" could have spared you a few intermediate variables. And of course, COUNT(array) - 1 = UBOUND(array).
DIM dict[] = Split("123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0", ",")
#APPTYPE CONSOLE
DIM dict[] = Split("123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0", ",")
DIM num AS INTEGER
'DIM i, j
FOREACH DIM i IN 0 TO UBOUND(dict)
num = ABS(dict[i])
IF num < 100 THEN
display(dict[i], "is too small")
ELSE
FOREACH DIM j IN 9 DOWNTO 1
IF num >= (10 ^ j) THEN
IF j MOD 2 = 1 THEN
display(dict[i], "has even number of digits")
ELSE
display(dict[i], middle3(num, j))
END IF
EXIT FOREACH
END IF
NEXT
END IF
NEXT
PAUSE
FUNCTION display(num, msg)
PRINT LPAD(num, 11, " "), " --> ", msg
END FUNCTION
FUNCTION middle3(n, pwr)
DIM power AS INTEGER = (pwr \ 2) - 1
DIM m AS INTEGER = n
m = m \ (10 ^ power)
m = m MOD 1000
IF m = 0 THEN
RETURN "000"
ELSE
RETURN m
END IF
END FUNCTION
FOREACH DIM j IN 9 DOWNTO 1
[00012] [middle3.fbs] Error: internal error
Press any key to continue...
Return to FBSL v3 Discussion Board
Users browsing this forum: No registered users and 1 guest