方案整数到字符串

时间:2018-04-16 01:47:44

标签: scheme racket

编写一个过程,NumtToStr接受一个正整数参数并返回一个包含用单词拼写的数字的句子:

(NumToStr 82) (八十二)

如果进入方法的两个论点是整数而不是列表,我不明白如何拆分方案中的列表?而且,一旦我得到其中一项,我如何记录这个数字是多少才能知道它是否在青少年,数百,数千,数百万等?

更新: 所以我查看了注释中给出的资源,但是这样的代码有多个定义在drRacket中不起作用并且给出了一个错误定义:期望函数体只有一个表达式,但是找到了5个额外的部分:

 (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))))

0 个答案:

没有答案