将数字转换为英文字母列表 [英] Converting numbers to english letter list

查看:426
本文介绍了将数字转换为英文字母列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有下面的函数,该函数将数字的输入转换为这些数字的部分翻译的单词输出.

I have the function below which converts an input of numbers into the partially translated word output of those numbers.

使用乘积和商,它在将数字分为几组的同时添加了数字的单词表示形式.

Using product and quotient, it adds the word representation of numbers while splitting the number into groups.

例如:

(number-name 87969087) -> '(87 million 969 thousand 87)
(number-name 1000000) -> '(1 million)

我正在尝试通过完全翻译小于1000的数字来解决我的问题.我试图实现一个小于1000的函数,该函数还将在构造列表时显示那些较小的数字.旁边:

Im trying to complete my problem by fully translating those numbers which are less than 1000 as well. Im trying to implement a function less-than-1000 which will display those smaller numbers as the list is being constructed as well. Alongside:

;; for less than 1000
; state words for 1-19
(define baseNumbers '(one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen))

; state words for multiples of ten
(define multiples '(twenty thirty forty fifty sixty seventy eighty ninety))

如此

(number-name 1110) -> '(one thousand one hundred ten)

很难找到一种将输入0显示为零的方法,即如果输入不是0,则不会显示零.

Its also been difficult coming up with a way to display an input of 0 to display as zero by do so in a way in which zero does not show up if the input is anything other than 0.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(define (number n)
  (define units '(thousand million billion trillion quadrillion))
  (define (nsplit n units acc lst)
    (define q (quotient n 1000))
    (define r (remainder n 1000))
    (if (zero? n) lst
        (cond [(zero? acc)
               (if (zero? r)
                   (nsplit q units (add1 acc) lst)
                   (nsplit q units (add1 acc) (cons r lst)))]
              [(zero? r)
               (nsplit q (cdr units) acc lst)]
              [else
               (nsplit q (cdr units) acc (cons r (cons (car units) lst)))])))
  (nsplit n units 0 empty))

推荐答案

您可以将整数分成3位数字组,将单位附加到每个组,然后再将3位平移,从而将整数转换为符号列表位数字组到符号列表.这是一个示例实现:

You can translate an integer into a list of symbols by breaking apart the number into 3-digit groups, attaching units to each group, and then further translating the 3-digit groups to list of symbols. Here is a sample implementation:

(define (num->lst num)
  (define bases '(one two three four five six seven eight nine ten eleven twelve
                  thirteen fourteen fifteen sixteen seventeen eighteen nineteen))
  (define multiples '(twenty thirty forty fifty sixty seventy eighty ninety))
  (define units '(empty thousand million billion trillion quadrillion quintillion
                        sextillion septillion octillion nonillion decillion))
  (define (below-1000 num bases mults)
    (cond [(zero? num) empty]
          [(< num 20) (list (list-ref bases (sub1 num)))]
          [(< num 100) (list* (list-ref mults (- (quotient num 10) 2))
                              (below-1000 (remainder num 10) bases mults))]
          [else (list* (list-ref bases (sub1 (quotient num 100))) 'hundred
                       (below-1000 (remainder num 100) bases mults))]))
  (define (nsplit num lst units)
    (define q (quotient num 1000))
    (define r (remainder num 1000))
    (if (zero? num) lst
        (cond [(zero? r) (nsplit q lst (cdr units))]
              [else (nsplit q (append (below-1000 r bases multiples)
                                      (cons (car units) lst)) (cdr units))])))
  (if (zero? num) '(zero)
      (remove 'empty (nsplit num empty units))))

below-1000将低于1000的数字转换为符号列表. nsplit将数字分为3位数字,并在每个组中附加单位,同时使用below-1000转换3位数字.

below-1000 converts numbers below 1000 into list of symbols. nsplit breaks the number into 3-digit groups and attaches units to each group, while converting the 3-digits using below-1000.

例如,

> (num->lst 0)
'(zero)
> (num->lst 1000000001)
'(one billion one)
> (num->lst 35579005)
'(thirty five million five hundred seventy nine thousand five)

这篇关于将数字转换为英文字母列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆