注册 登录
明经CAD社区 返回首页

mahuan1279的个人空间 http://bbs.mjtd.com/?7303115 [收藏] [复制] [分享] [RSS]

日志

lisp实现两大数相乘代码

热度 1已有 1413 次阅读2014-10-29 21:49 |个人分类:LISP|系统分类:应用| 大数相乘, lisp

(defun add (lst1 lst2)
  (setq k1 (length lst1))
  (setq k2 (length lst2))
  (if (> k1 k2)
          (repeat (- k1 k2)  (setq lst2 (cons 0 lst2)) )
          (repeat (- k2 k1)  (setq lst1 (cons 0 lst1)) )
   )
  (setq addlst (mapcar '(lambda (x y) (+ x y)) lst1 lst2 ))
  addlst
)
(defun lnum (lst n n0)
   (setq lst (mapcar '(lambda (x) (* x n))  lst))
   (setq zlst (reverse lst))
   (repeat n0 (setq zlst (cons 0 zlst) ) )
 (reverse zlst)
)  
(defun xc(lst1 lst2)
   (setq n1 (length lst1))
   (setq n2 (length lst2))
   (setq j 1 fflst '(0))
   (if (> n1 n2)
          (while (<= j n2)
              (setq fflst (add (lnum lst1 (nth (- j 1) lst2) (- n2 j) ) fflst))
              (setq j (+ 1 j))
           )
          (while (<= j n1)
              (setq fflst (add (lnum lst2 (nth (- j 1) lst1) (- n1 j) ) fflst)) 
              (setq j (+ 1 j))
           ) 
    )
 fflst
)
(defun jinw (blst)
    (while (not (apply 'and (mapcar '(lambda (x) (<= x 9)) blst )))
         (setq hlst (mapcar '(lambda (x) (rem x 10)) blst))
         (setq qlst (mapcar '(lambda (x) (/ x 10)) blst))
         (setq qlst (append qlst (list 0)))
         (setq blst (add hlst qlst))
    )
  blst
)
(defun sr(a)
   (setq n (strlen a) i 1 lst nil)
   (while (<= i n)
        (setq lst (cons (atoi (substr a i 1)) lst))
        (setq i (+ 1 i))
   )
   (reverse lst)
)
(defun c:dc()
  (setq a (getstring "请输入a="))
  (setq b (getstring "请输入b="))
  (setq lst1 (sr a))
  (setq lst2 (sr b))
  (setq tenlst (jinw (xc lst1 lst2)))
  (setq str (apply 'strcat (mapcar '(lambda (x) (itoa x)) tenlst ) ) )
  (if (= (car tenlst) 0)
      (setq str (vl-string-left-trim "0" str))
  )
  (princ (strcat a "*" b "=" str))
  (princ)
)
 已同步至 mahuan1279的微博
 已同步至 mahuan1279的微博
 已同步至 mahuan1279的微博

路过

雷人
1

握手

鲜花

鸡蛋

刚表态过的朋友 (1 人)

全部作者的其他最新日志

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-3-29 17:06 , Processed in 0.215578 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部