明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: kblh

[已解答] 字符 1-6 转换成 “1” “2” “3” “4” “5” “6”

[复制链接]
发表于 2014-6-22 15:35:05 | 显示全部楼层
本帖最后由 llsheng_73 于 2014-6-22 19:02 编辑

  1. (defun tt(str / a b m n op)
  2.   (setq a(mapcar'(lambda(x)(if(>(length(setq a(vl-remove""(strsplit x'("-")))))1)
  3.                       (reverse(setq m(if(>(atoi(car a))0)(atoi(car a))(apply'+(vl-string->list(car a))))
  4.                                     n(if(>(atoi(last a))0)(atoi(last a))(apply'+(vl-string->list(last a))))
  5.                                     a(list(car a))op(if(< m n)1+ 1-)
  6.                                     b(repeat(abs(- m n))
  7.                                        (setq m(if(>(atoi(car a))0)(atoi(car a))(vl-string->list(car a)))
  8.                                              a(cons(if(=(type m)'list)
  9.                                                      (if(>(length m)1)(vl-list->string(list(car m)(op(last m))))
  10.                                                        (chr(op(last m))))
  11.                                                      (itoa(op m)))a)))))
  12.                       (if a(LIST(car a))'(""))))(vl-remove""(strsplit str'("+"))))
  13.         b(car a)a(cdr a)
  14.         b(if a(foreach x a(setq b(append b x)))b)))
  15. (defun strsplit(str splits / i a b)
  16.   (while(<""str)
  17.     (if(vl-remove'nil(mapcar'(lambda(x)(vl-string-search x str))splits))
  18.       (setq i(car(vl-sort(vl-remove'nil(mapcar'(lambda(x)(if(setq l(vl-string-search x str))(cons l x)))splits))
  19.                          '(lambda(s1 s2)(<(car s1)(car s2)))))
  20.             a(cons(substr str 1(car i))a)b(cons(cdr i)b)
  21.             str(substr str(+(car i)(strlen(cdr i))1)))
  22.       (setq a(cons str a)b(cons "" b)str"")))
  23.   (reverse a))

(tt"-5")->("5")
(tt"50")->("50")
(tt"5-")->("5")
(tt"+5-")->("5")
(tt"5+")->("5")
(tt"-5+")->("5")
(tt"5-21("5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21")
(tt"31-15")->("31" "30" "29" "28" "27" "26" "25" "24" "23" "22" "21" "20" "19" "18" "17" "16" "15")
(tt"31-15+A")->("31" "30" "29" "28" "27" "26" "25" "24" "23" "22" "21" "20" "19" "18" "17" "16" "15" "A")
(tt"21-8+A-H")->("21" "20" "19" "18" "17" "16" "15" "14" "13" "12" "11" "10" "9" "8" "A" "B" "C" "D" "E" "F" "G" "H")
(TT"+14-8+A-F+h-e")->("14" "13" "12" "11" "10" "9" "8" "A" "B" "C" "D" "E" "F" "h" "g" "f" "e")

点评

能(TT"8-14+A8-a14)不?  发表于 2014-6-22 21:06
发表于 2014-6-22 15:52:05 | 显示全部楼层
kblh 发表于 2014-6-22 14:27
edada  在没有“-” 和 “+”时 不对  
如 1 不能得到 “1”

来源都是字符串"1"运行后返回自身。
  1. (defun parse9 (str delim / I S STR1 STRLST)
  2.   (setq        i 0 str1 "")
  3.   (while (/= "" (setq s (substr str (setq i (1+ i)) 1)))
  4.     (cond ((/= delim s) (setq str1 (strcat str1 s)))
  5.           (T (setq strlst (append strlst (list str1)) str1 ""))         
  6.     )
  7.   )
  8.   (if (/= str1 "")
  9.     (append strlst (list str1))
  10.     strlst
  11.   )
  12. )

  13. (defun sk_str->delim(txt / a nlst B C D K)
  14.    (vl-load-com)  
  15.   (if (or (wcmatch txt "*-*")(wcmatch txt "*+*"))
  16.     (progn
  17.       (setq txt(parse9 txt "+") nlst '())
  18.       (while(setq a(car txt))
  19.         (if (wcmatch a "*-*")
  20.           (progn
  21.             (setq b(parse9 a "-"))
  22.             (mapcar 'set '(c d)(list (atoi(car b)) (atoi(cadr b))))
  23.             (setq k (- (min c d) 1))
  24.             (repeat(if(< c d)(1+(- d c))(1+(- c d)))
  25.               (setq nlst(cons (setq k(1+ k)) nlst))             
  26.               )
  27.             )
  28.           (setq nlst(cons (atoi a) nlst))
  29.           )
  30.         (setq txt (cdr txt))
  31.         )
  32.       (setq nlst(vl-sort nlst '<)
  33.             nlst(mapcar 'itoa nlst))
  34.       )
  35.     txt
  36.     )
  37.   )
  38. ;;测试程序
  39. (defun c:tt()  
  40. (setq txt "1")
  41.   (sk_str->delim txt)
  42.   )

点评

(sk_str->delim "+18-8+A-F")-->("0" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18")?   发表于 2014-6-22 18:59
发表于 2014-6-22 17:27:28 | 显示全部楼层
是不是列举全部情况...比如1-6....这样的会不会出现多个,如果有多个怎么表示??
发表于 2014-6-22 17:44:58 | 显示全部楼层
q3_2006 发表于 2014-6-22 17:27
是不是列举全部情况...比如1-6....这样的会不会出现多个,如果有多个怎么表示??

1-6+8-19+25-40
发表于 2014-6-22 18:25:37 | 显示全部楼层
edata 发表于 2014-6-22 17:44
1-6+8-19+25-40

这样就好办了....

(defun c:tt ( / del i l lst lx pos str x)
(defun str2lst ( str del / pos lst )
    (while (setq pos (vl-string-search del str))
        (setq lst (cons (substr str 1 pos) lst)
              str (substr str (+ pos 1 (strlen del)))
        )
    )
    (reverse (cons str lst))
)
        (setq str "99+1-6+8-19+25-40+120"
                l (str2lst str "+")
                l (mapcar '(lambda(x)
                (if (null (vl-string-search "-" x)) x
                        (progn
                                (setq i -1 lx nil)
                                (mapcar 'itoa (reverse
                                (repeat (1+ (- (- (atoi (substr x (1+ (vl-string-search "-" x))))) (atoi x)))
                                (setq lx (cons (+ (setq i (1+ i)) (atoi x)) lx)))))))) l)
                l (apply 'append (mapcar '(lambda(x) (if (listp x) x (list x))) l))
        )
)

点评

"+18-8+A-F"应为("18" "17" "16" "15" "14" "13" "12" "11" "10" "9" "8" "A" "B" "C" "D" "E"  发表于 2014-6-22 18:58
发表于 2014-6-22 20:14:56 | 显示全部楼层
q3_2006 发表于 2014-6-22 18:25
这样就好办了....

(defun c:tt ( / del i l lst lx pos str x)

还带字母呀...73哥考虑这么多...
发表于 2014-6-22 20:37:07 | 显示全部楼层
本帖最后由 llsheng_73 于 2014-6-22 20:39 编辑
q3_2006 发表于 2014-6-22 20:14
还带字母呀...73哥考虑这么多...


我本来还考虑过汉字的,但那结果太扯蛋最后给去掉了
但是"9-5"应该允许("9" "8" "7" "6" "5")
发表于 2014-6-22 21:07:55 | 显示全部楼层
画蛇添足=0分
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-5-24 13:44 , Processed in 0.273981 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表