Advertisement
Guest User

Untitled

a guest
Jan 9th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 3.31 KB | None | 0 0
  1. (define btr-sum  
  2.   (lambda(a b)
  3.    
  4.     (cond
  5.       ((< (string-length a)(string-length b))(btr-sum (normalize a b) b))
  6.       ((> (string-length a)(string-length b))(btr-sum a (normalize b a)))
  7.       ((and(char=? (string-ref a 0) #\. )(char=?(string-ref b 0)#\b))(btr-sum (substring a 1 )(substring b 1)))
  8.       (else (real (normalize(real a b ".") (create-rip a b ".")) (create-rip a b ".") "."));; (+(+ a b) c)
  9.       )    
  10.     )
  11.   )
  12.  
  13.  
  14.  
  15. (define real ;;sums strings a and b without carry
  16.   (lambda(a b r);;strings
  17.      (if (>(string-length a)1)
  18.          (string-append
  19.          
  20.           (real ;;recursive
  21.            (substring a 0 (-(string-length a)1))
  22.            (substring b 0 (- (string-length b)1))
  23.            r)
  24.          
  25.          
  26.           (string(somma ;;sums last chars of strings
  27.                  (string-ref a (-(string-length a)1))
  28.                  (string-ref b (-(string-length b)1))
  29.                  )
  30.                  )
  31.           )
  32.          
  33.          (string(somma (string-ref a 0)(string-ref b 0)))
  34.          )
  35.      
  36.    
  37.     )
  38.   )
  39.  
  40.  
  41.  
  42.  
  43. (define normalize ;;if the length of subj < length of obj, normalize fills stacks up subj with "."
  44.   (lambda(subj obj)
  45.     (if (not(= (string-length subj)(string-length obj)))
  46.         (normalize (string-append "." subj) obj)
  47.         subj
  48.         )
  49.     )
  50.   )
  51.  
  52. (define create-rip ;;generates the carry of the sum of a and b
  53.   (lambda (a b c)
  54.     (if (> (string-length a) 1)
  55.         (cond ;; if length>1 then do it again
  56.           (
  57.            (and (char=? #\+(string-ref a (-(string-length a)1)))(char=? #\+ (string-ref b (-(string-length b)1))));;if +/+, add one + in carry
  58.            (create-rip (substring a 0 (-(string-length a)1))(substring b 0 (-(string-length b)1))(string-append "+" c))
  59.            )
  60.          
  61.           (
  62.            (and (char=? #\-(string-ref a (-(string-length a)1)))(char=? #\- (string-ref b (-(string-length b)1))))if -/-, add one - in carry
  63.            (create-rip (substring a 0 (-(string-length a)1))(substring b 0 (-(string-length b)1))(string-append "-" c))
  64.            )
  65.           (else  (create-rip (substring a 0 (-(string-length a)1))(substring b 0 (-(string-length b)1))(string-append "." c)))
  66.           )
  67.        
  68.         (cond ;;if it's the last char do it only once
  69.           (
  70.            (and (char=? #\+(string-ref a (-(string-length a)1)))(char=? #\+ (string-ref b (-(string-length b)1))))
  71.            (string-append "+" c)
  72.            )
  73.          
  74.           (
  75.            (and (char=? #\-(string-ref a (-(string-length a)1)))(char=? #\- (string-ref b (-(string-length b)1))))
  76.            (string-append "-" c)
  77.            )
  78.           (else  (string-append "." c))
  79.           )
  80.         )
  81.     )
  82.   )
  83.  
  84.  
  85. (define somma ;;general procedure to sum two char without taking care of carry
  86.   (lambda(a b)
  87.     (cond
  88.       ((and (char=? a  #\-)(char=? b #\-))#\+)
  89.       ((and (char=? a  #\.) (char=? b #\-))#\-)
  90.       ((and (char=? a  #\+)(char=? b #\-))#\.)
  91.      
  92.       ((and (char=? a  #\-)(char=? b #\+))#\.)
  93.       ((and (char=? a  #\.)(char=? b #\+))#\+)
  94.       ((and (char=? a  #\+)(char=? b #\+))#\-)
  95.      
  96.       ((and (char=? a  #\-)(char=? b #\.))#\-)
  97.       ((and (char=? a  #\.)(char=? b #\.))#\.)
  98.       ((and (char=? a  #\+)(char=? b #\.)) #\+)
  99.         )
  100.     )
  101.   )
  102.  
  103.  
  104.  
  105.  
  106. (btr-sum "-""--")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement