平衡三进制字符串之和

时间:2019-01-09 17:31:19

标签: scheme racket

我必须编写一个代码,将两个以平衡三进制(https://en.wikipedia.org/wiki/Balanced_ternary)编写的数字字符串相加。 而不是使用-1 / 0 / + 1,我必须使用-/。/ + 由于我无法将字符串三进制平衡数转换为整数十进制数,因此我必须手动求和输入中两个字符串的字符,但我不知道如何处理各种字符求和产生的进位。

编辑:这是我到目前为止提出的-https://pastebin.com/R98RDHTQ 但是,如果我将-和-相加,代码将给出。++而不是-++。 这是因为它将部分结果.- +与第一个进位..相加。它无法跟踪第二个进位...该如何解决?

(define btr-sum  
  (lambda(a b)

    (cond
      ((< (string-length a)(string-length b))(btr-sum (normalize a b) b))
      ((> (string-length a)(string-length b))(btr-sum a (normalize b a)))
      ((and(char=? (string-ref a 0) #\. )(char=?(string-ref b 0)#\b))(btr-sum (substring a 1 )(substring b 1)))
      (else (real (normalize(real a b ".") (create-rip a b ".")) (create-rip a b ".") "."));; (+(+ a b) c)
      )    
    )
  )



(define real ;;sums strings a and b without carry
  (lambda(a b r);;strings
     (if (>(string-length a)1)
         (string-append

          (real ;;recursive
           (substring a 0 (-(string-length a)1))
           (substring b 0 (- (string-length b)1))
           r)


          (string(somma ;;sums last chars of strings
                 (string-ref a (-(string-length a)1))
                 (string-ref b (-(string-length b)1))
                 )
                 )
          )

         (string(somma (string-ref a 0)(string-ref b 0)))
         )


    )
  )




(define normalize ;;if the length of subj < length of obj, normalize fills stacks up subj with "."
  (lambda(subj obj)
    (if (not(= (string-length subj)(string-length obj)))
        (normalize (string-append "." subj) obj)
        subj
        )
    )
  )

(define create-rip ;;generates the carry of the sum of a and b
  (lambda (a b c)
    (if (> (string-length a) 1)
        (cond ;; if length>1 then do it again
          (
           (and (char=? #\+(string-ref a (-(string-length a)1)))(char=? #\+ (string-ref b (-(string-length b)1))));;if +/+, add one + in carry
           (create-rip (substring a 0 (-(string-length a)1))(substring b 0 (-(string-length b)1))(string-append "+" c))
           )

          (
           (and (char=? #\-(string-ref a (-(string-length a)1)))(char=? #\- (string-ref b (-(string-length b)1))))if -/-, add one - in carry
           (create-rip (substring a 0 (-(string-length a)1))(substring b 0 (-(string-length b)1))(string-append "-" c))
           )
          (else  (create-rip (substring a 0 (-(string-length a)1))(substring b 0 (-(string-length b)1))(string-append "." c)))
          )

        (cond ;;if it's the last char do it only once
          (
           (and (char=? #\+(string-ref a (-(string-length a)1)))(char=? #\+ (string-ref b (-(string-length b)1))))
           (string-append "+" c)
           )

          (
           (and (char=? #\-(string-ref a (-(string-length a)1)))(char=? #\- (string-ref b (-(string-length b)1))))
           (string-append "-" c)
           )
          (else  (string-append "." c))
          )
        )
    )
  )


(define somma ;;general procedure to sum two char without taking care of carry
  (lambda(a b)
    (cond
      ((and (char=? a  #\-)(char=? b #\-))#\+)
      ((and (char=? a  #\.) (char=? b #\-))#\-)
      ((and (char=? a  #\+)(char=? b #\-))#\.)

      ((and (char=? a  #\-)(char=? b #\+))#\.)
      ((and (char=? a  #\.)(char=? b #\+))#\+)
      ((and (char=? a  #\+)(char=? b #\+))#\-)

      ((and (char=? a  #\-)(char=? b #\.))#\-)
      ((and (char=? a  #\.)(char=? b #\.))#\.)
      ((and (char=? a  #\+)(char=? b #\.)) #\+)
        )
    )
  )




(btr-sum "-""--")

2 个答案:

答案 0 :(得分:0)

使用char->integerinteger->char在字符和整数之间来回转换。

答案 1 :(得分:0)

首先计算出三元加法表;您引用的Wikipedia文章为表格提供了大约三分之二的答案:

       -1      0      1
     ----   ----   ----
-1   -1 1     -1      0
0      -1      0      1
1       0      1   1 -1

然后三元加法只是一种小学算法,用于将两个数字相加,从右到左进行运算,并在必要时携带。

编辑::原始海报询问如何跟踪进位。我不会编写代码,但是您会像在手工将两个数字加到一个临时变量中一样,跟踪进位。将其称为carry并将其初始化为零。要添加两个数字,请使用以下过程从右到左进行操作:使用上面显示的三进制加号表在当前位置添加两个三叉戟,然后将carry添加到结果中。结果的低阶Trit移到当前位置,而高阶Trit替换carry。确保不要在最后遗失最后的进位。