如何将输入50*3这类乘法数据转换为50+50+50这类的加法?
想了好长时间一直都没明天该具体如何转换,请高手指点指点。(defun abc(_str / LM:lst->str LM:str->lst _alst_xlst )
(defun LM:lst->str ( lst del )
(if (cdr lst)
(strcat (car lst) del (LM:lst->str (cdr lst) del))
(car lst)
)
)
(defun LM:str->lst ( str del / pos )
(if (setq pos (vl-string-search del str))
(cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
(list str)
)
)
(LM:lst->str
(apply 'append
(mapcar '(lambda(x)
(setq _alst nil)
(cond
((wcmatch x "*`**")
(setq _xlst (LM:str->lst x "*"))
((lambda(y z)(repeat (atoi z) (setq _alst (cons y _alst)))_alst)(car _xlst)(cadr _xlst))
)
(t (list x))
)
)(LM:str->lst _str " "))) " ")
)
命令: (abc "50 100*3 200*2 100*1")
"50 100 100 100 200 200 100"
本帖最后由 xyp1964 于 2024-8-19 11:19 编辑
大角牛 发表于 2024-8-17 15:51
我要输入一组数据,怎么转换呢?
如输入50 100*2 200*3后
转换为 50 100 100 200 200 200
;; (abc "50 100*2 200*3") → (50 100 100 200 200 200)
(defun abc (tx)
(defun aa (tx / nn t1 t2 lst)
(setq nn (vl-string-search "*" tx)
t1 (substr tx 1 nn)
t2 (substr tx (+ nn 2))
)
(if(and (setq a (atoi t1)) (setq n (atoi t2)))
(repeat n (setq lst (cons a lst)))
)
lst
)
(apply 'append(mapcar '(lambda (x)(i(vl-string-search "*" x)(aa x)(list (atoi x))))(xyp-StrSpr tx " ")))
)
(defun xyp-StrSpr (str sub / lst n)
"以指定分解符分解字符串"
(if (/= sub "")
(progn
(while (setq n (vl-string-search sub str))
(setq lst (cons (substr str 1 n) lst)
str (substr str (+ n (strlen sub) 1))
)
)
(vl-remove "" (reverse (cons str lst)))
)
)
)
分解出两个数据,一个用于重复数字,一个用于循环次数控制,反复strcat。。。 ;; (abc "50*3")
(defun abc (tx / nn t1 t2 t3)
(setq nn (vl-string-search "*" tx)
t1 (substr tx 1 nn)
t2 (substr tx (+ nn 2))
)
(if (and (distof t1) (setq n (atoi t2)))
(progn
(setq t3 "")
(repeat (1- n)(setq t3 (strcat t3 t1 "+")))
(strcat t3 t1)
)
)
) ;; (abc "50*3")→"50+50+50"
(defun abc (tx / nn t1 t2 t3)
(if (and (setq nn (vl-string-search "*" tx))
(distof (setq t1 (substr tx 1 nn)))
(setq n (atoi (setq t2 (substr tx (+ nn 2)))))
)
(progn
(setq t3 "")
(repeat (1- n)
(setq t3 (strcat t3 t1 "+"))
)
(strcat t3 t1)
)
)
) 谢谢院长和vectra。等我把理论吃透了,加到我现在工作的插件里面。 想把院长的程序添加到下面这段程序里面,搞了2天没搞定,郁闷。
(defun auto_detext()
(setq auto_textlen (strlen auto_text))
(setq text_t "")
(setq auto_ss nil)
(while (> auto_textlen 0)
(setq txet_c (substr auto_text 1 1))
(if (/= txet_c "+")
(setq text_t (strcat text_t txet_c))
(if (/= text_t"")
(progn
(setq auto_ss (cons text_t auto_ss))
(setq text_t ""))))
(setq auto_text (substr auto_text 2 (- (strlen auto_text) 1)))
(if (and (< auto_textlen 2) (/= text_t""))
(progn
(setq auto_ss (cons text_t auto_ss))
(setq text_t "")))
(setq auto_textlen (1- auto_textlen)))
(setq auto_ss (reverse auto_ss))
) 添加院长的程序后运行,每次会少一个,如果*3,会只画出2个,*5,会只画出4个。郁闷。 我也不知道是怎么回事,而且我把代码加进去后,直接输入不带乘号的数字,提示错误,无法工作。现在晕死了。
图1和图2的上传顺序反了。 xyp1964 发表于 2015-2-16 09:22
我要输入一组数据,怎么转换呢?
如输入50 100*2 200*3后
转换为 50 100 100 200 200 200
页:
[1]
2