本帖最后由 cabinsummer 于 2011-11-13 07:13 编辑
继续提供思路。
定义有d个数值且为0的表
- (defun ZeroList(d / a)
- (repeat d
- (setq a (append a (list 0)))
- )
- a
- )
两表相加
- (defun AddList(xlist ylist / a b c)
- (setq a (mapcar '+ xlist ylist))
- (setq b (append (cdr (mapcar '(lambda(x)(/ x 1000000)) a))(list 0)))
- (setq c (mapcar '(lambda(x)(rem x 1000000)) a))
- (mapcar '+ b c)
- )
两表相减
- (defun MinusList(xlist ylist / n a tt tag)
- (setq n (1- (length xlist)) a nil tag 0)
- (repeat (length xlist)
- (setq tt (- (nth n xlist)(nth n ylist) tag))
- (if (< tt 0)
- (progn
- (setq tt (+ tt 1000000))
- (setq tag 1)
- )
- (setq tag 0)
- )
- (setq a (cons tt a))
- (setq n (1- n))
- )
- a
- )
表乘以常数d并整理
- (defun Multi(xlist d / a b c)
- (setq a (mapcar '(lambda(x)(* d x)) xlist))
- (setq b (append (cdr (mapcar '(lambda(x)(/ x 1000000)) a))(list 0)))
- (setq c (mapcar '(lambda(x)(rem x 1000000)) a))
- (mapcar '+ b c)
- )
表除以常数d并整理
- (defun Divide(xlist d / n v r g a)
- (setq n 0 r 0 a nil)
- (repeat (length xlist)
- (setq v (+ (nth n xlist) (* r 1000000)))
- (setq g (/ v d))
- (setq r (rem v d))
- (setq a (append a (list g)))
- (setq n (1+ n))
- )
- a
- )
精度d位u倒数并整理
- (defun ReverseNumber(u d / n v r g a)
- (setq xlist (zerolist d))
- (setq n 0 r 1)
- (repeat d
- (setq v (+ (nth n xlist) (* r 1000000)))
- (setq g (/ v u))
- (setq r (rem v u))
- (setq a (append a (list g)))
- (setq n (1+ n))
- )
- a
- )
表中数值输出为连接的长字符串,注意LISP只能处理4096个以下的字符,如果计算位数过长,必须改用其它输出方式。
- (defun ToString(xlist / a b n fn)
- (setq a "\npi=" n 0)
- (setq fn (open "c:\\pi.txt" "w"))
- (princ a fn)
- (repeat (1- (length xlist))
- (setq b (itoa (nth n xlist)))
- (setq a (strcat (substr "00000" (strlen b)) b " ") n (1+ n))
- (princ a fn)
- )
- (close fn)
- (princ)
- )
|