(SCHEME)号码->英文清单 [英] (SCHEME) Number -> English List

查看:173
本文介绍了(SCHEME)号码->英文清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.因此,我想知道如何创建将随机数转换为其英语单词成分的函数.

Okay. So I'm wondering how to create a function that will turn a random number into its english word component.

例如(1001->'(千零一)或0->'(零) 和(数字名称阶乘20->’(二quatillion 342 quadrillion九百零二 万亿八千一百七十六亿一千六百 四万))

Such as (1001 -> '(one thousand one) or 0 -> '(zero) and (number-name factorial 20 -> ’(two quintillion four hundred thirty two quadrillion nine hundred two trillion eight billion one hundred seventy six million six hundred forty thousand))

我与以前的用户一起进行stackoverflow的工作,是将长整数转换成3位数字(1,341,100是一百万,34.1万个1100 0十个0)

I worked with a previous user on stackoverflow to get something that turned a long number into 3 part digits (1,341,100 is one million, 341 thousand 1 hundred 0 tens 0 ones)

 #lang r5rs
 (define (three-names n)
   (if (zero? n)
  "zero"
  (let loop ((n n)
             (units '((10 one) (10 ten) (10 hundred) (1000 thousand) (1000 million)  (1000 billion) (1000 trillion) (1000 quadrillion) (1000 quintillion)
                             (1000 sextillion) (1000 septillion) (1000 octillion) (1000 nonillion)
                             (1000 decillion) (1000 undecillion) (1000 duodecillion) (1000 tredecillion)
                             (1000 quatturodecillion) (1000 sexdillion) (1000 septendecillion) (1000 octodecillion)
                             (1000 novemdecillion) (1000 vigintillion)))
             (ARRAY '()))

    (if
     (or (zero? n) (null? units)) 
        ARRAY
        (let* ((unit (car units)) (div (car unit)) (english (cadr unit)))
          (let
              ((q (quotient n div)) (r (remainder n div)))
            (loop q
                  (cdr units)
                  (cons r (cons english ARRAY)))))))))    

我现在唯一了解的就是将其设置为具有0到20的值:

The only thing I understand right now is to make it so that there are values for 0-20:

 (= x 0) zero
 (= x 1) one
 ...
 (> x 1000) thousand
 (> x 1000000) million

但是,对于其中一个,这些将不会输出到列表中,而对于两个,则不确定还要做什么?

But for one, these won't be outputted into a list and two, not sure what else to do?

推荐答案

正如Sylwester所说,您需要能够

As Sylwester explains you need to be able to

  1. 正确打印1000以下(3位数字)以下每个数字的文本
  2. 将数字分成3位数字组,使用上面的代码进行打印,然后附加千"即可.

以下是与Racket的r5rs语言兼容的示例实现:

Here's an example implementation that is compatible with Racket's r5rs language:

(define (n2t n)
  (define 1to19     '(one two three four five six seven eight nine ten eleven twelve
                          thirteen fourteen fifteen sixteen seventeen eighteen nineteen))
  (define multof10  '(twenty thirty forty fifty sixty seventy eighty ninety))
  (define thousands '(thousand million billion trillion quadrillion quintillion sextillion septillion octillion nonillion decillion undecillion))
  (cond
    ((= n 0) '(zero))  ; zero is a special case since from now on all 0 will be suppressed
    ((< n 0) (cons 'minus (n2t (- n))))
    (else
     (let loop ((n n) (units thousands) (res '()))
       (cond
         ; --- below 1000
         ((= n 0)    res)
         ((< 0 n 20) (cons (list-ref 1to19    (- n 1)) res))
         ((< n 100)  (cons (list-ref multof10 (- (quotient n 10) 2))
                           (loop (remainder n 10) '() res)))
         ((< n 1000) (loop (quotient n 100)
                           '()
                           (cons 'hundred (loop (remainder n 100) '() res))))
         (else
          ; --- 1000 and above
          (let ((q   (quotient n 1000))
                (res (loop (remainder n 1000) thousands res)))
            (if (zero? q)
                res
                (loop q (cdr units) (cons (car units) res))))))))))

测试:

> (n2t 0)
'(zero)
> (n2t 1001)
'(one thousand one)
> (n2t 132219)
'(one hundred thirty two thousand two hundred nineteen)
> (n2t -132219)
'(minus one hundred thirty two thousand two hundred nineteen)
> (n2t 2345678213)
'(two billion three hundred forty five million six hundred seventy eight thousand two hundred thirteen)
> (n2t 2432902008176640000)
'(two quintillion four hundred thirty two quadrillion nine hundred two trillion eight billion one hundred seventy six million six hundred forty thousand)

这篇关于(SCHEME)号码-&gt;英文清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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