Pogledajte određenu poruku
Staro 25. 10. 2007.   #2
LiquidBrain
Milan Cvejic
Wrote a book
 
Avatar LiquidBrain
 
Datum učlanjenja: 05.09.2006
Lokacija: Beograd
Poruke: 1.241
Hvala: 32
73 "Hvala" u 56 poruka
LiquidBrain će postati "faca" uskoro
Pošaljite poruku preko Yahoo za LiquidBrain
Wink

Citat:
Originalno napisao dootzky Pogledajte poruku
probao sam da pretrazim forum, ali ne znam ni kako bi ukucao search string!

elem, zadatak je (relativno) jednostavan:

1) ako imas neku cifru na racunu, recimo 452,318.00 dinara
2) to trebas da napises slovima, za isti primer bi trebalo:
cetiristopedesetdvehiljadetristaosamnaestdinara

e sad, ja krenuo da razlazem obicne cifre, uradio jedinice, desetice, stotine, hiljade, desetine hiljada.. i onda video nepravilnosti, pa probao da se pravim pametan i da optimizujem kod uz funckije, pa ispalo jos gore...

PITANJE: - ima li neko gotovo resenje za ovo? sigurno jos neko mora da koristi ove stvari?? help? :P

tip: 60 != sestdeset

resenja u smalltalku...

Kôd:
PrivateNumberToWords: aNumber 
    | rez imeBr celi cbroj tric trojka cs cd cj sl1 |
    aNumber isNilOrZero ifTrue: [rez := 'nula'].
    imeBr := (ItcLookupTable new)
                at: 1 put: 'jedan';
                at: 2 put: 'dva';
                at: 3 put: 'tri';
                at: 4 put: 'četiri';
                at: 5 put: 'pet';
                at: 6 put: 'šest';
                at: 7 put: 'sedam';
                at: 8 put: 'osam';
                at: 9 put: 'devet';
                yourself.
    rez := ''.
    celi := aNumber integerPart.
    cbroj := celi itcAsInteger asLeadingZerosStringOfSize: 15.
    1 to: 15
        by: 3
        do: 
            [:each | 
            tric := cbroj midString: 3 from: each.
            trojka := tric itcAsInteger.
            tric ~= '000' 
                ifTrue: 
                    [cs := tric first itcAsInteger.
                    cd := (tric at: 2) itcAsInteger.
                    cj := (tric at: 3) itcAsInteger.
                    cs = 2 ifTrue: [rez := rez , 'dve'].
                    cs > 2 ifTrue: [rez := rez , (imeBr at: cs)].
                    cs = 1 ifTrue: [rez := rez , 'stotinu'].
                    (#(2 3 4) includes: cs) ifTrue: [rez := rez , 'stotine'].
                    cs > 4 ifTrue: [rez := rez , 'stotina'].
                    cj isZero ifTrue: [sl1 := ''] ifFalse: [sl1 := imeBr at: cj].
                    cd = 4 ifTrue: [rez := rez , 'četr'].
                    cd = 6 ifTrue: [rez := rez , 'šez'].
                    cd = 5 ifTrue: [rez := rez , 'pe'].
                    cd = 9 ifTrue: [rez := rez , 'deve'].
                    (#(2 3 7 8) includes: cd) ifTrue: [rez := rez , (imeBr at: cd)].
                    cd = 1 
                        ifTrue: 
                            [sl1 := ''.
                            cj isZero 
                                ifTrue: [rez := rez , 'deset']
                                ifFalse: 
                                    [cj = 1 
                                        ifTrue: [rez := rez , 'jeda']
                                        ifFalse: [cj = 4 ifTrue: [rez := rez , 'četr'] ifFalse: [rez := rez , (imeBr at: cj)]]].
                            cj > 0 ifTrue: [rez := rez , 'naest']].
                    cd > 1 ifTrue: [rez := rez , 'deset'].
                    ((#(4 10) includes: each) and: [cd ~= 1]) 
                        ifTrue: 
                            [cj = 1 ifTrue: [sl1 := 'jedna'].
                            cj = 2 ifTrue: [sl1 := 'dve']].
                    rez := rez , sl1.
                    each = 1 
                        ifTrue: 
                            [rez := rez , 'bilion'.
                            (cj > 1 or: [cd = 1]) ifTrue: [rez := rez , 'a']].
                    each = 4 
                        ifTrue: 
                            [rez := rez , 'milijard'.
                            (trojka \\ 100 > 11 and: [trojka \\ 100 < 19]) 
                                ifTrue: [rez := rez , 'i']
                                ifFalse: 
                                    [cj = 1 
                                        ifTrue: [rez := rez , 'a']
                                        ifFalse: 
                                            [(cj > 4 or: [cj isZero]) ifTrue: [rez := rez , 'i'] ifFalse: [cj > 1 ifTrue: [rez := rez , 'e']]]]].
                    each = 7 
                        ifTrue: 
                            [rez := rez , 'milion'.
                            ((trojka \\ 100 > 11 and: [trojka \\ 100 < 19]) or: [cj ~= 1]) ifTrue: [rez := rez , 'a']].
                    each = 10 
                        ifTrue: 
                            [rez := rez , 'hiljad'.
                            (trojka \\ 100 > 11 and: [trojka \\ 100 < 19]) 
                                ifTrue: [rez := rez , 'a']
                                ifFalse: 
                                    [(trojka = 1 and: [cj ~= 1]) 
                                        ifTrue: [rez := rez , 'u']
                                        ifFalse: 
                                            [(cj > 4 or: [#(0 1) includes: cj]) 
                                                ifTrue: [rez := rez , 'a']
                                                ifFalse: [cj > 1 ifTrue: [rez := rez , 'e']]]]]]].
    ^rez
__________________
http://weevify.com
LiquidBrain je offline   Odgovorite uz citat
"Hvala" LiquidBrain za poruku: