' zabawki do konwersji liczb ' copyleft 2007, JJ ' testowane na OpenOffice.org Basic ' żadnych gwarancji, żadnych zastrzeżeń ' Konwersja do postaci pozycyjnej o podstawie N=nbase; funkcja dec_to_base const maxbase = 16 dim cyfra(0 to maxbase-1) as string dim cyfry_ok as boolean function inicjuj_cyfry as boolean if not (cyfry_ok) then cyfra(0) = "0" cyfra(1) = "1" cyfra(2) = "2" cyfra(3) = "3" cyfra(4) = "4" cyfra(5) = "5" cyfra(6) = "6" cyfra(7) = "7" cyfra(8) = "8" cyfra(9) = "9" cyfra(10) = "a" cyfra(11) = "b" cyfra(12) = "c" cyfra(13) = "d" cyfra(14) = "e" cyfra(15) = "f" endif inicjuj_cyfry = true end function const nbase = 2 function dec_to_base(byval x as long) as string dim s as string dim m as integer if (nbase > maxbase) then exit function endif cyfry_ok = inicjuj_cyfry() s = "" while (x>0) m = x mod nbase s = cyfra(m) & s x = (x-m)/nbase wend if (s="") then s = "0" endif dec_to_base = s end function ' Konwersja do postaci słownej: funkcja dec_to_words const base1 = 10 const base2 = 100 const base3 = 1000 const minus = "minus" const sep = " " const jednostka_zero = "zero" dim jednostka(0 to 19, 0 to 4) as string dim rzad(0 to 5, 0 to 2) as string dim jest as boolean function trzycyfry(byval x as integer, byval r as integer) as string dim i as integer dim j as integer dim s as string dim ss as string dim m as integer dim mm as integer if (x>0) then m = x mod base2 j = 2 if (x=1) then j = 0 else mm = x mod base1 if (mm>1) and (mm<5) and (m<10 or m>20) then j = 1 endif endif s = "" ss = "" x = (x-m)/base2 if (x>0) then ss = jednostka(x,3) & sep endif x = m if (x<20) then s = jednostka(x mod base1, x/20) else i = 0 while (x>0) m = x mod base1 if (left(s,1) <> sep) then s = sep & s endif s = jednostka(m, i) & s x = (x-m)/base1 i = i+2 wend endif s = ss & s if (right(s,1) <> sep) then s = s & sep endif trzycyfry = s & rzad(r, j) & sep else trzycyfry = "" endif end function function inicjuj_nazwy as boolean if not(jest) then jednostka(0,0) = "" jednostka(1,0) = "jeden" jednostka(2,0) = "dwa" jednostka(3,0) = "trzy" jednostka(4,0) = "cztery" jednostka(5,0) = "pięć" jednostka(6,0) = "sześć" jednostka(7,0) = "siedem" jednostka(8,0) = "osiem" jednostka(9,0) = "dziewięć" jednostka(0,1) = "dziesięć" jednostka(1,1) = "jedenaście" jednostka(2,1) = "dwanaście" jednostka(3,1) = "trzynaście" jednostka(4,1) = "czternaście" jednostka(5,1) = "piętnaście" jednostka(6,1) = "szesnaście" jednostka(7,1) = "siedemnaście" jednostka(8,1) = "osiemnaście" jednostka(9,1) = "dziewiętnaście" jednostka(0,2) = "" jednostka(1,2) = "dziesięć" jednostka(2,2) = "dwadzieścia" jednostka(3,2) = "trzydzieści" jednostka(4,2) = "czterdzieści" jednostka(5,2) = "pięćdziesiąt" jednostka(6,2) = "sześćdziesiąt" jednostka(7,2) = "siedemdziesiąt" jednostka(8,2) = "osiemdziesiąt" jednostka(9,2) = "dziewięćdziesiąt" jednostka(0,3) = "" jednostka(1,3) = "sto" jednostka(2,3) = "dwieście" jednostka(3,3) = "trzysta" jednostka(4,3) = "czterysta" jednostka(5,3) = "pięćset" jednostka(6,3) = "sześćset" jednostka(7,3) = "siedemset" jednostka(8,3) = "osiemset" jednostka(9,3) = "dziewięćset" rzad(0,0) = "" rzad(1,0) = "tysiąc" rzad(2,0) = "milion" rzad(3,0) = "miliard" rzad(4,0) = "bilion" rzad(5,0) = "biliard" rzad(0,1) = "" rzad(1,1) = "tysiące" rzad(2,1) = "miliony" rzad(3,1) = "miliardy" rzad(4,1) = "biliony" rzad(5,0) = "biliardy" rzad(0,2) = "" rzad(1,2) = "tysięcy" rzad(2,2) = "milionów" rzad(3,2) = "miliardów" rzad(4,2) = "bilionów" rzad(5,0) = "biliardów" endif inicjuj_nazwy = true end function function dec_to_words(byval x as long) as string jest = inicjuj_nazwy() dim s as string dim m as integer dim i as integer i = 0 s = "" if (x<0) then x = -x prefix = minus & sep else prefix = "" if (x=0) then s = jednostka_zero endif endif while (x>0) m = x mod base3 s = trzycyfry(m, i) & s x = (x-m)/base3 i = i+1 wend dec_to_words = prefix & s end function