Number names: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎[[#ALGOL 68]]: misc reindent)
m (→‎[[#ALGOL 68]]: brief form with sample input/output)
Line 1: Line 1:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<!-- # From: www.codecodex.com/wiki/index.php%3Ftitle%3DConvert_an_integer_into_words - site states it is GPL # -->
<!-- # From: www.codecodex.com/wiki/index.php%3Ftitle%3DConvert_an_integer_into_words - site states it is GPL # -->
PROC number words = (INT n) STRING :
PROC number words = (INT n) STRING:(
# returns a string representation of n in words. Currently
# returns a string representation of n in words. Currently
deals with anything from 0 to 999 999 999. #
deals with anything from 0 to 999 999 999. #
[]STRING digits = []STRING
BEGIN
("zero","one","two","three","four","five","six","seven","eight","nine")[@0];
[] STRING digits =
[] STRING (
[]STRING teens = []STRING
("ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen")[@0];
"zero",
"one",
[]STRING decades = []STRING
("twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")[@2];
"two",
"three",
"four",
PROC three digits = (INT n) STRING: (
"five",
# does the conversion for n from 0 to 999. #
"six",
INT tens = n MOD 100 OVER 10;
"seven",
INT units = n MOD 10;
(n >= 100|digits[n OVER 100] + " " + "hundred" + (n MOD 100 /= 0|" and "|"")|"") +
"eight",
"nine"
(tens /= 0|(tens = 1|teens[units]|decades[tens] + (units /= 0|"-"|""))) +
)[@0];
(units /= 0 AND tens /= 1 OR n = 0|digits[units]|"")
[] STRING teens =
);
[] STRING (
INT m = n OVER 1 000 000;
"ten",
INT k = n MOD 1 000 000 OVER 1000;
"eleven",
INT u = n MOD 1000;
"twelve",
(m /= 0|three digits(m) + " million"|"") +
"thirteen",
(m /= 0 AND (k /= 0 OR u >= 100)|", "|"") +
"fourteen",
(k /= 0|three digits(k) + " thousand"|"") +
"fifteen",
((m /= 0 OR k /= 0) AND u > 0 AND u < 100|" and " |: k /= 0 AND u /= 0|", "|"") +
"sixteen",
(u /= 0 OR n = 0|three digits(u)|"")
);
"seventeen",
"eighteen",
"nineteen"
)[@0];
[] STRING decades =
[] STRING (
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
)[@2];
PROC three digits =
(
INT n
) STRING :
# does the conversion for n from 0 to 999. #
BEGIN
INT tens = n MOD 100 OVER 10;
INT units = n MOD 10;
IF n >= 100 THEN
digits[n OVER 100]
+
" "
+
"hundred"
+
IF n MOD 100 /= 0 THEN
" and "
ELSE
""
FI
ELSE
""
FI
+
IF tens /= 0 THEN
IF tens = 1 THEN
teens[units]
ELSE
decades[tens]
+
IF units /= 0 THEN
"-"
ELSE
""
FI
FI
FI
+
IF units /= 0 AND tens /= 1 OR n = 0 THEN
digits[units]
ELSE
""
FI
END;
INT n3 = n OVER 1 000 000;
INT n2 = n MOD 1 000 000 OVER 1000;
INT n1 = n MOD 1000;
IF n3 /= 0 THEN
three digits(n3) + " million"
ELSE
""
FI
+
IF n3 /= 0 AND (n2 /= 0 OR n1 >= 100) THEN
", "
ELSE
""
FI
+
IF n2 /= 0 THEN
three digits(n2) + " thousand"
ELSE
""
FI
+
IF (n3 /= 0 OR n2 /= 0) AND n1 > 0 AND n1 < 100 THEN
" and "
ELIF n2 /= 0 AND n1 /= 0 THEN
", "
ELSE
""
FI
+
IF n1 /= 0 OR n = 0 THEN
three digits(n1)
ELSE
""
FI
END;

DO # until user hits EOF #
DO # until user hits EOF #
on logical file end(standin, (REF FILE f) BOOL : GOTO finish);
on logical file end(standin, (REF FILE f) BOOL: finish);
on value error(standin, (REF FILE f) BOOL : GOTO finish);
on value error(standin, (REF FILE f) BOOL: finish);
INT n;
INT n;
print("n? ");
print("n? ");
read((n, new line));
read((n, new line));
print((number words(n), new line))
print((number words(n), new line))
OD;
OD;
finish:
finish:
SKIP
SKIP
Example input with output:
n? 43112609
forty-three million, one hundred and twelve thousand, six hundred and nine

Revision as of 12:58, 27 September 2008

ALGOL 68

PROC number words = (INT n) STRING:(
  # returns a string representation of n in words. Currently
  deals with anything from 0 to 999 999 999. #
    []STRING digits = []STRING
      ("zero","one","two","three","four","five","six","seven","eight","nine")[@0];
    []STRING teens = []STRING
      ("ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen")[@0];
    []STRING decades = []STRING
      ("twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")[@2];

    PROC three digits = (INT n) STRING: (
      # does the conversion for n from 0 to 999. #
        INT tens = n MOD 100 OVER 10;
        INT units = n MOD 10;
        (n >= 100|digits[n OVER 100] + " " + "hundred" + (n MOD 100 /= 0|" and "|"")|"") +
        (tens /= 0|(tens = 1|teens[units]|decades[tens] + (units /= 0|"-"|""))) +
        (units /= 0 AND tens /= 1 OR n = 0|digits[units]|"")
      );
    INT m = n OVER 1 000 000;
    INT k = n MOD 1 000 000 OVER 1000;
    INT u = n MOD 1000;
    (m /= 0|three digits(m) + " million"|"") +
    (m /= 0 AND (k /= 0 OR u >= 100)|", "|"") +
    (k /= 0|three digits(k) + " thousand"|"") +
    ((m /= 0 OR k /= 0) AND u > 0 AND u < 100|" and " |: k /= 0 AND u /= 0|", "|"") +
    (u /= 0 OR n = 0|three digits(u)|"")
  );

DO # until user hits EOF #
  on logical file end(standin, (REF FILE f) BOOL: finish);
  on value error(standin, (REF FILE f) BOOL: finish);
  INT n;
  print("n? ");
  read((n, new line));
  print((number words(n), new line))
OD;
finish:
  SKIP

Example input with output:

n? 43112609
forty-three million, one hundred and twelve thousand, six hundred and nine