Hi fdelucio,
There are various different styles of pseudocode. If you have a
specific style in mind, please post a sample of it (as a clarification
request) and I'll adapt this algorithm to your style.
This algorithm converts integers from 1 to 999,999 into words. Phrases
starting with "//" are comments. The "DIV" operator represents integer
division with the remainder discarded. The "+" operator represents
string concatenation.
The general approach is to break down the problem into a number of
simpler conversion tasks, then use recursive calls to this same
function to perform the subtasks, then concatenate (join) the
fragments into the final answer.
There are a lot of "special cases" to watch out for, to make sure that
(for example) 5000 doesn't get formatted as "five thousand zero".
integer2words(i)
// Returns a string representing the integer 'i' spelled as words.
// Local variables used: thousands, hundreds, tens, units, result
if i < 1 or i > 999999 then
report_error_and_exit("out of range")
end if
thousands := i DIV 1000
units := i - (thousands * 1000)
if thousands > 0 then
result := integer2words(thousands) + " thousand"
if units > 0 then
result := result + " " + integer2words(units)
end if
else // The range is from 1 to 999.
hundreds := units DIV 100
units := units - (hundreds * 100)
if hundreds > 0 then
result := integer2words(hundreds) + " hundred"
if units > 0 then
result := result + " " + integer2words(units)
end if
else // The range is from 1 to 99.
if units = 1 then result := "one"
else if units = 2 then result := "two"
else if units = 3 then result := "three"
else if units = 4 then result := "four"
else if units = 5 then result := "five"
else if units = 6 then result := "six"
else if units = 7 then result := "seven"
else if units = 8 then result := "eight"
else if units = 9 then result := "nine"
else if units = 10 then result := "ten"
else if units = 11 then result := "eleven"
else if units = 12 then result := "twelve"
else if units = 13 then result := "thirteen"
else if units = 14 then result := "fourteen"
else if units = 15 then result := "fifteen"
else if units = 16 then result := "sixteen"
else if units = 17 then result := "seventeen"
else if units = 18 then result := "eighteen"
else if units = 19 then result := "nineteen"
else // The range is from 20 to 99
tens := units DIV 10
units := units - (tens * 10)
if tens = 2 then result := "twenty"
elseif tens = 3 then result := "thirty"
elseif tens = 4 then result := "forty"
elseif tens = 5 then result := "fifty"
elseif tens = 6 then result := "sixty"
elseif tens = 7 then result := "seventy"
elseif tens = 8 then result := "eighty"
elseif tens = 9 then result := "ninety"
end if
if units > 0 then
result := result + " " + integer2words(units)
end if
end if
end if
end if
return(result)
Additional links:
"Numbers to words" in PL/SQL
http://anlab.hufs.ac.kr/~yjkim/BOOK/books/oracle/prog2/ch17_08.htm
"Numbers to words" in RPG
http://www.as400pro.com/nbrtowords.htm
"Numbers to words" in C
http://akbani.20m.com/codes/numword.txt
"Numbers to words" in Delphi
http://www.howtodothings.com/showarticle.asp?article=306
"Numbers to words" in Visual Basic
http://www.vbce.com/code/functions/convert_numbers_to_words/index.asp
"Numbers to words" in Java
http://mindprod.com/inwords.html
"Numbers to words" in C++
http://rossm.net/Electronics/Computers/Software/C++/Algorithms/index.htm#itow
Google search strategy:
"numbers to words" algorithm
://www.google.com/search?q=%22numbers%20to%20words%22%20algorithm
(unsuccessful)
"numbers to english" algorithm
://www.google.com/search?q=%22numbers+to+english%22+algorithm
(unsuccessful)
"numbers to words"
://www.google.com/search?client=googlet&q=%22numbers%20to%20words%22
Regards,
eiffel-ga |