Qbasicnews.com
February 23, 2020, 06:41:58 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 [2] 3
  Print  
Author Topic: Batter up!  (Read 9284 times)
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #15 on: September 11, 2003, 07:36:07 PM »

Quote from: "relsoft"
Quote from: "Agamemnus"
This looks like it would be very complicated to get 100% correct.

Good luck.


Its been done. Ask Flexi. ;*)


Now now, that wouldn't be his code then, would it ;*)

(ps: Flexi=Flexibal, click on "search" up the top of your screen at these forums and put that in the "search for users" thing).
Logged

relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #16 on: September 12, 2003, 03:02:44 AM »

It was his code and I believe works pretty darn well. But the code I saved was on my now dead 486 so you should ask Flexibal for that. ;*)
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Moneo
Na_th_an
*****
Posts: 1971


« Reply #17 on: September 12, 2003, 01:51:08 PM »

PVERA,

Can't tell your country from your profile, but I'll assume it's USA. You should have a pretty good idea how the conversion from numeric amount to textual amount works. A few hints:

1) You'll need a table or array containing the text for the values in the units position of the amount, like: one, two, three, ... nine. This table/array can also be used for the hundreds and thousands digit positions.

2) You'll need a table/aray for the text for the values in the units and tens positions when these are less than 20, like: ten, eleven, twelve, thirteen, .... nineteen.

3) Youˇll need a table/array for the text for the value of the tens position when this is 2 or greater, like: dummy, twenty, thirty, forty, ... ninety. Note that the "dummy" is not used because we don't access this table for tens (10-19).

You'll need special handling for amounts with no units like: twenty, thirty, ..., as well those with nothing in the tens position like: one hundred, two hundred, ... Ditto for thousands.

The pennies should be printer as 00/100 through 99/100. When the pennies are zero, it also acceptable to print "no/100."

So,
first you isolate the pennies.
Then look at the units ant tens positions (if any) to see which of the above tables to use. If less than 20 use the "teens" table.
If 20 or over, isolate the units and get this text from units table.
Then get tens position text from "tens" table.
If any hundreds or thousands, convert using units table.

You should do this on your own. It's a very good excercise.

P.S. Watch out for a zero input amount. You can't print a zero check. Also watch out for a pennies only check. Ask your teacher if this is valid.

Good Luck!
*****
Logged
Flexibal
Forum Regular
**
Posts: 128



WWW
« Reply #18 on: September 12, 2003, 06:24:33 PM »

here pal... this will spell out any integral number between 1 and 999,999,999.


Code:

DECLARE FUNCTION Spell$ (n AS LONG)

CLS
PRINT Spell(4165261)


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

   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 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$ + "fourty "
            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




i think i once made a shorten one... but who cares.


[Flexibal>
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #19 on: September 12, 2003, 07:10:35 PM »

recursive .................
Logged

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.
Flexibal
Forum Regular
**
Posts: 128



WWW
« Reply #20 on: September 12, 2003, 07:19:04 PM »

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>
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #21 on: September 12, 2003, 11:07:23 PM »

nothing... it just feels like cheating....
Logged

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.
pvera11
New Member

Posts: 8


heh
« Reply #22 on: September 13, 2003, 12:05:00 AM »

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 :Huh:
Logged
Flexibal
Forum Regular
**
Posts: 128



WWW
« Reply #23 on: September 13, 2003, 09:34:14 AM »

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 Smiley






[Flexibal>
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #24 on: September 13, 2003, 10:26:49 PM »

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.
*****
Logged
Flexibal
Forum Regular
**
Posts: 128



WWW
« Reply #25 on: September 14, 2003, 05:42:22 PM »

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>
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #26 on: September 16, 2003, 01:57:32 PM »

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.
*****
Logged
Flexibal
Forum Regular
**
Posts: 128



WWW
« Reply #27 on: September 16, 2003, 04:00:36 PM »

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 Smiley




[Flexibal>
Logged
pvera11
New Member

Posts: 8


« Reply #28 on: September 16, 2003, 10:18:01 PM »

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]
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #29 on: September 16, 2003, 10:19:34 PM »

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.
Logged

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.
Pages: 1 [2] 3
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!