Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Batter up!
#21
aga: whats wrong with recursion? sure, qb might suck at it, but in theory?

rel: i PMed you

pvera: if you want to spell out pennies as well, no problem:

Code:
FUNCTION CurrencySpell$ (n AS DOUBLE)
   DIM i&, d&, s$
  
   s$ = ""
   i& = INT(n)
   d& = (n - i&) * 100
   IF d& = 100 THEN d& = 0: i& = i& + 1
   s$ = Spell$(i&) + "dollars "
   IF d& <> 0 THEN s$ = s$ + "and " + Spell$(d&) + "cents"

   CurrencySpell$ = s$
END FUNCTION

example:

Code:
PRINT CurrencySpell(99.799)

gives you "ninety nine dollars and eighty cents"






[Flexibal>
Reply
#22
nothing... it just feels like cheating....
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#23
thats kool flexi,...long but otherwise kool

right now we are looking at the shortest possible coding.....

my codeing revised

Code:
cls

dim num$(30)
dim digit(4)

for count = 1 to 29
read num$(count)

data "one","two", "three","four" (and so fourth)

next count

amt$ = "9876.54

dp=instr(1, amt$, ".")

dollars$=mid$(amt$,1,(dp-1)
cents$="dolars " + mid$(amt$, (dp+1))+" cents"
num.digits=len(dollars$)

for count =1to num.digits
digit(count)=val(mid$(dollars$, count, 1)
next count


this should print dollars and 54 cents, now all thats left is to the left of the decimal place.....ahhhhhhh....heh :???:
Reply
#24
perva: "thats kool flexi,...long but otherwise kool"

well, i could just use an prepopulated array and a for-loop, and then load the array from DATA statements.... but... my code is optimized for speed Smile






[Flexibal>
Reply
#25
FLEXIBAL,

Very nice piece of code. The use of recursion actually makes it very straightforward and easy to understand. I did a similar routine years ago, and compared to yours it's a rat's nest.

A few minor points:
1) 40 is spelled forty not fourty.
2) all amounts from 21 to 99 must be hyphenated, like twenty-one, forty-five, ninety-nine.
3) The program which invokes your function must check for a zero amount. You return a null, correctly, 'cuase that's not your problem.
4) The invoking program must also make sure that the number is not negative, which hangs the program. Non-numeric amounts must also be checked by the invoking program.
*****
Reply
#26
i think i'll let pvera do his cosmetics, moneo. i've done my part, which is the ragged algorithm. let whomever wants to use it customize it on his own. and add makeup or lipstick.





[Flexibal>
Reply
#27
Flexibal,

Well, that's one way of looking at it, and I respect your opinion.

My policy is that if I'm going to provide someone with code, versus an explanation or some pseudo-code, then I feel obligated in giving him as complete and accurate a piece of code as I can produce. Code doesn't come in flavors, or with or without cosmetics. It either works 100% or it doesn't. But, like I said, that's my opinion and style, and you may not agree.
*****
Reply
#28
okay... but still, if i missplled some word or something... it has nothing to do with the code. the numbers could have been spelled in any other language as well. of course the could would require some adjustments, but i provided him with the algorithm, and since it's his assignment, he should do at least the final settings.

-----

but because gave me a challenge there... here's the code, optimized for speed:

Code:
FUNCTION Spell$ (n AS LONG)
   w$ = ""

   IF n = 0 THEN
      w$ = "zero"
   ELSEIF n < 0 THEN
      w$ = "negative "
      n = -n
   END IF

   DO
      SELECT CASE n
         CASE 0
            EXIT DO
         CASE 1
            w$ = w$ + "one "
            EXIT DO
         CASE 2
            w$ = w$ + "two "
            EXIT DO
         CASE 3
            w$ = w$ + "three "
            EXIT DO
         CASE 4
            w$ = w$ + "four "
            EXIT DO
         CASE 5
            w$ = w$ + "five "
            EXIT DO
         CASE 6
            w$ = w$ + "six "
            EXIT DO
         CASE 7
            w$ = w$ + "seven "
            EXIT DO
         CASE 8
            w$ = w$ + "eight "
            EXIT DO
         CASE 9
            w$ = w$ + "nine "
            EXIT DO
         CASE 10
            w$ = w$ + "ten "
            EXIT DO
         CASE 11
            w$ = w$ + "eleven "
            EXIT DO
         CASE 12
            w$ = w$ + "twelve "
            EXIT DO
         CASE 13
            w$ = w$ + "thirteen "
            EXIT DO
         CASE 14
            w$ = w$ + "fourteen "
            EXIT DO
         CASE 15
            w$ = w$ + "fifteen "
            EXIT DO
         CASE 16
            w$ = w$ + "sixteen "
            EXIT DO
         CASE 17
            w$ = w$ + "seventeen "
            EXIT DO
         CASE 18
            w$ = w$ + "eighteen "
            EXIT DO
         CASE 19
            w$ = w$ + "nineteen "
            EXIT DO
         CASE 20
            w$ = w$ + "twenty "
            EXIT DO
         CASE 30
            w$ = w$ + "thrity "
            EXIT DO
         CASE 40
            w$ = w$ + "forty "
            EXIT DO
         CASE 50
            w$ = w$ + "fifty "
            EXIT DO
         CASE 60
            w$ = w$ + "sixty "
            EXIT DO
         CASE 70
            w$ = w$ + "seventy "
            EXIT DO
         CASE 80
            w$ = w$ + "eighty  "
            EXIT DO
         CASE 90
            w$ = w$ + "ninety "
            EXIT DO

         CASE IS >= 1000000
            w$ = w$ + Spell(n \ 1000000) + "million "
            n = n - (n \ 1000000) * 1000000
         CASE IS >= 1000
            w$ = w$ + Spell(n \ 1000) + "thousand "
            n = n - (n \ 1000) * 1000
         CASE IS >= 100
            w$ = w$ + Spell(n \ 100) + "hundred "
            n = n - (n \ 100) * 100
         CASE IS > 90
            w$ = w$ + "ninety-"
            n = n - 90
         CASE IS > 80
            w$ = w$ + "eighty-"
            n = n - 80
         CASE IS > 70
            w$ = w$ + "seventy-"
            n = n - 70
         CASE IS > 60
            w$ = w$ + "sixty-"
            n = n - 60
         CASE IS > 50
            w$ = w$ + "fifty-"
            n = n - 50
         CASE IS > 40
            w$ = w$ + "forty-"
            n = n - 40
         CASE IS > 30
            w$ = w$ + "thirty-"
            n = n - 30
         CASE IS > 20
            w$ = w$ + "twenty-"
            n = n - 20
      END SELECT
   LOOP

   Spell$ = w$
END FUNCTION


here, now we're cool Smile




[Flexibal>
Reply
#29
again it is a good piece of code and it works....err mind you wer still working on it in class.....this is really a trail and error assignment....and i do mean error with reassurance

we worked at it and sorta stop short of finishing it with minimal flaws, and it is a lot shorter than my last one...heh

Code:
cls
dim amount$(30)
dim digit(4)

for count=1to28
read amount$(count)
data "one","two" and so forth
next count

value$="9999.99"

dp=instr(1,value$, ".")
dollars$=mid$(value$, 1(dp-1))
cents$="dollars$ " +mid$(value$,(dp+1))+ " cents"
num.digits=len(dollars$)

for count=1tonum.digits
digit(count)val(mid$(dollars$, count,1))
'print digit(count)
next count

if num.digits=4then
if digits(1) >0 then
print amount$(digit(1))="thousand ";

and the above is repeated with the hundred only it digit 2

so now im working on it not coming up with a thousand wen someone enters 0,999.99........dont want someone earing zero thousand dollars hahahah
[/code]
Reply
#30
Just remember this:

Glenn: Nitpicks logic in sentences. (er, he's been gone for a while though... I think that's what he does)
Moneo: Nitpicks input/ouput possibilities in code.
Agamemnus: Nitpicks spelling.

And BTW, fourty is perfectly alright logically, which is why I didn't say anything.
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)