本帖最后由 doremidai 于 2013-4-27 18:34 编辑
很久没有写代码,最近想写个“文本中数字提取并求和”的短程序,没有思路了,各位i老大帮帮忙。
下面的代码只能计算文本后面的文字,如何提取文本中部逗号前的文字,和过滤带括号的文本不参与计算? - (print "文本数字求和(型如:N=23,N23.12,N=23.12,M=50.5,多行文字先炸开,不支持负数),命令:txtsum")
- (defun c:txtsum(/ all endno aa bb cc wz tha tsum i)
- (if (null xzz) (setq xzz 0))
- (setq ALL (ssget(list (cons 0 "text"))))
- (if all (SETQ nn (SSLENGTH ALL)))
- (setq endno 0)
- (setq tsum 0 i 0)
- (WHILE (and (< endno nn) all)
- (setq aa (ssname all endno))
- (setq bb (entget aa))
- (setq cc(cdr(assoc 1 bb)))
- (setq wz(rdata cc))
- (setq tha(substr cc (+ wz 1) ( - (strlen cc) wz -1)))
- (if (= tha "") (setq tha "0"))
- (setq tsum(+ tsum (atof tha)))
- (setq i(1+ i))
- (print (strcat (itoa i) ":文本 <" cc ">---数字 <" tha ">"))
- (setq endno (+ endno 1))
- )
- (print (strcat "共" (itoa i) "个文本,和=" (rtos tsum)))
- (print)
- )
- ;;;
- (defun rdata(str / len i aa)
- (if (> (strlen str) 0)
- (progn
- (setq i (strlen str))
- (setq aa(substr str (strlen str) 1))
- (while (and (or (and (>= aa "0") (<= aa "9"))
- (= aa ".")
- )
- (>= i 1)
- )
- (setq i(1- i))
- (if (>= i 1) (setq aa(substr str i 1)))
- )
- )
- )
- (setq i i)
- )
|