明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1934|回复: 7

求冒泡程序(ALISP或VLISP)

[复制链接]
发表于 2007-9-11 14:30:00 | 显示全部楼层 |阅读模式
请各位大虾赐教!谢谢
发表于 2007-9-11 15:31:00 | 显示全部楼层

Here you are

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 2007-9-11 22:39:00 | 显示全部楼层

求高手解决

打开后是乱码,整理\运行后没啥效果.。。。

图上有N个点。。。取得这些点后,存在表lst里,怎么将里面的点坐标按给定的顺序排列???请高手赐教!谢谢!!!

比如顺序是:纵坐标小的(先) 横坐标小(后)的 排在表前面。。。

发表于 2007-9-11 22:48:00 | 显示全部楼层
; SORTS.LSP
; A comparison of two of the 'classic' sort routines - the Bubble and Shell
: from http://ourworld.compuserve.com/homepages/pshan/acptrick.htm
;
; In each case the sort is performed on the 2nd member of a three member list
; in the following format   ("string" REAL REAL)
;
; it Returns the sorted list.
  1. (defun Shell ( ALIST / BEGSORT CTR EXCH ELAPSE ENDSORT HEAD N NEWLIST TAIL)
  2. ;;
  3. ;; The Shell Sort (ascending)
  4. ;;
  5.    (defun EXCH ( / EX_FLAG HEAD MID NEW1ST NEW2ND TAIL)
  6.       (setq EX_FLAG 1)
  7.          (while EX_FLAG
  8.             (setq
  9.                   NEW1st (list (nth (+ CTR D ) NEWLIST))                                   ; Switch value at D interval to CURRENT
  10.                   NEW2nd (list (nth CTR NEWLIST))                                          ; Switch CURRENT value to D interval
  11.                   HEAD (reverse (cdr (member (nth CTR NEWLIST) (reverse NEWLIST))))        ; First part of list
  12.                   MID (member (nth (1+ CTR) NEWLIST) (reverse
  13.                       (member (nth (1- (+ CTR D)) NEWLIST) (reverse NEWLIST))))            ; Between CURRENT and D interval
  14.                   TAIL (cdr (member (nth (+ CTR D) NEWLIST) NEWLIST))                      ; Remainder of list
  15.                   NEWLIST (append HEAD NEW1st MID NEW2nd TAIL)                             ; Revised list
  16.                   CTR (- CTR D)
  17.              )
  18.              (if (>= CTR 0)
  19.                 (progn
  20.                    (if (<= (cadr (nth CTR NEWLIST)) (cadr (nth (+ CTR D) NEWLIST)))        ; Add Jump Dist to Current location
  21.                        (setq EX_FLAG nil))
  22.                 );progn
  23.                 (setq EX_FLAG nil)
  24.             )
  25.          );WHILE
  26.    )
  27.   (if (or (null ALIST)
  28.           (not (= (type ALIST) 'LIST ))
  29.       )
  30.     (progn
  31.        (princ "Argument is not a valid list")(print)
  32.     )
  33.     (progn
  34.        (setq
  35.              NEWLIST ALIST                                                                 ; Make a copy to work on
  36.              D (abs (/ (length NEWLIST) 2))                                                ; D will be an integer
  37.              N (1- (length NEWLIST))                                                       ; first member of list is 0
  38.              BEGSORT (getvar "CDATE")                                                      ; Get starting time
  39.        )
  40.        (repeat 3 (print))
  41.        (princ "\nSorting...please wait")
  42.        (while (> D 0)                                                                      ; Do the sorting
  43.           (setq L (- N D)
  44.                 J 0
  45.           )
  46.           (while (<= J L)
  47.              (if (> (cadr (nth J NEWLIST)) (cadr (nth (+ J D) NEWLIST)))
  48.                  (progn
  49.                       (setq CTR J)
  50.                       (EXCH)
  51.                  )
  52.              )
  53.              (setq J (1+ J))
  54.           )
  55.           (setq D (abs (/ D 2)))
  56.        )
  57.        (setq ENDSORT (getvar "CDATE"))                                                     ; Get ending time
  58.        (RPT_TIME)                                                                          ; Calculate and report elapsed time
  59.        (textpage)                                                                          ; Clear screen
  60.        NEWLIST                                                                             ; Return sorted list
  61.     );progn
  62.   );if
  63. );Shell
  64. (defun BubSort ( ALIST / BEGSORT CTR ELAPSE ENDSORT EXCHNG HEAD N NEWLIST TAIL)
  65. ;;;
  66. ; Bubble sort (ascending)
  67. ;;;
  68.    (defun EXCHNG ( / NEW1ST NEW2ND HEAD TAIL)
  69.       (repeat (length NEWLIST)
  70.          (repeat (- (length NEWLIST) 1)
  71.             (if (> (cadr (nth CTR NEWLIST)) (cadr (nth (1+ CTR) NEWLIST)))                ; Compare CURRENT to NEXT
  72.               (progn
  73.                  (setq NEW1st (list (nth (1+ CTR) NEWLIST))                               ; Switch NEXT to CURRENT
  74.                        NEW2nd (list (nth CTR NEWLIST))                                    ; Switch CURRENT to NEXT
  75.                        HEAD (reverse (cdr (member (nth CTR NEWLIST) (reverse NEWLIST))))  ; First part of list
  76.                        TAIL (cdr (member (nth (1+ CTR) NEWLIST) NEWLIST))                 ; Remainder of list
  77.                        NEWLIST (append HEAD NEW1st NEW2nd TAIL))                          ; Revised list
  78.               );progn
  79.            );if
  80.            (setq CTR (1+ CTR))
  81.          );repeat
  82.          (setq CTR 0)
  83.        );repeat
  84.    );EXCHNG
  85.   (if (or (null ALIST)
  86.          (not (= (type ALIST) 'LIST ))
  87.       )
  88.     (progn
  89.        (princ "Argument is not a valid list")(print)
  90.     )
  91.     (progn
  92.        (repeat 3 (print))
  93.        (princ "\nSorting...please wait")
  94.        (setq CTR 0                                                                        ; Initialize Counter
  95.              N (length ALIST)                                                             ; Number of records (sublists)
  96.              NEWLIST ALIST                                                                ; Make a copy to work on
  97.              BEGSORT (getvar "CDATE")                                                     ; Get starting time
  98.        )
  99.        (EXCHNG)                                                                           ; Do the sorting
  100.        (setq ENDSORT (getvar "CDATE"))                                                    ; Get ending time
  101.        (RPT_TIME)                                                                         ; Calculate and report elapsed time
  102.        (textpage)                                                                         ; Clear screen
  103.        NEWLIST                                                                            ; Return sorted list
  104.     );progn
  105.   );if
  106. );BubSort
  107. (defun C:SORT ()
  108.    (SEL-OPT "Short Long"
  109.             "\nSelect List  TYPE "
  110.             "Short"
  111.             "Short Long<")
  112.    (cond ((= OPTION "Short")
  113.          (setq LST '(
  114.              ("string1" 5.41 11.05)
  115.              ("string2" 2.42 45.09)
  116.              ("string3" 4.13 19.25)
  117.              ("string4" 9.24 34.75)
  118.              ("string5" 5.15 42.05)
  119.              ("string6" 2.36 13.35)
  120.              ("string7" 0.67 21.05)
  121.              ("string8" 7.68 11.55)
  122.              ("string8a" 7.68 11.55)
  123.              ("string9" 5.09 11.05)
  124.              ("string10" 2.71 45.09)
  125.              ("string11" 4.92 19.25)
  126.              ("string12" 9.33 34.75)
  127.              ("string13" 5.04 42.05)
  128.              ("string14" 2.25 13.35)
  129.              ("string15" 0.66 21.05)
  130.              ("string16" 7.57 11.55)
  131.              ("string17" 3.78 45.09)
  132.              ("string18" 7.99 19.25)
  133.              ("string19" 1.31 34.75)
  134.              ("string20" 9.02 42.05)
  135.              ("string21" 5.23 13.35)
  136.              ("string22" 3.64 21.05)
  137.              ("string23" 0.55 11.55)
  138.              ("string24" 32.76 45.09)
  139.              ("string25" 44.97 19.25)
  140.              )
  141.           );setq
  142.          );option
  143.          ((= OPTION "Long")
  144.            (setq LST '(("string1" 5.41 11.05)("string2" 2.42 45.09)("string3" 4.13 19.25)("string4" 9.24 34.75)("string5" 5.15 42.05)("string6" 2.36 13.35)("string7" 0.67 21.05)("string8" 7.68 11.55)("string9" 5.09 11.05)("string10" 2.71 45.09)("string11" 4.92 19.25)("string12" 9.33 34.75)("string13" 5.04 42.05)("string14" 2.25 13.35)("string15" 0.66 21.05)("string16" 7.57 11.55)("string17" 3.78 45.09)("string18" 7.99 19.25)("string19" 1.31 34.75)("string20" 7.68 11.55)("string21" 4.93 19.25)("string22" 9.3
  145. 4 34.75)("string23" 5.05 42.05)("string24" 2.26 13.35)("string25" 7.68 21.05)("string26" 7.58 11.55)("string27" 3.79 45.09)("string28" 7.10 19.25)("string29" 1.32 34.75)("string30" 7.69 11.55)("string31" 4.94 19.25)("string32" 9.35 34.75)("string33" 5.06 42.05)("string34" 2.27 13.35)("string35" 7.69 21.05)("string36" 7.59 11.55)("string37" 3.80 45.09)("string38" 7.11 19.25)("string39" 1.33 34.75)("string40" 7.70 11.55)("string41" 4.95 19.25)("string42" 9.36 34.75)("string43" 5.07 42.05)("string44" 2.28 13.3
  146. 5)("string45" 7.70 21.05)("string46" 7.60 11.55)("string47" 3.81 45.09)("string48" 7.12 19.25)("string49" 1.34 34.75)("string40" 7.71 11.55)("string51" 4.96 19.25)("string52" 9.37 34.75)
  147.            )))
  148.           (t)
  149.    );cond
  150.    (SEL-OPT "Bubble Shell X"
  151.             "\nSelect Sort TYPE... "
  152.             "Bubble"
  153.             "Bubble/Shell/eXit<")
  154.    (cond ((= OPTION "Bubble")(BubSort lst))
  155.          ((= OPTION "Shell")(Shell lst))
  156.          (t)
  157.    )
  158. )
  159. (defun CONVERT ( MOMENT / HR MN SEC DIFF )
  160.       (setq HR  (/ (atof (substr MOMENT 1 2)) 24.0)
  161.             MN (/ (/ (atof (substr MOMENT 3 2)) 60.0) 24.0)
  162.             SEC (/ (/ (/ (atof (substr MOMENT 5 2)) 60.0) 60.0) 24.0)
  163.             DIFF (+ HR MN SEC))
  164. );CONVERT
  165. (defun RPT_TIME ( / START STOP )
  166.      (setq
  167.            START (CONVERT (substr (rtos BEGSORT 2 10) 10 8))
  168.            STOP  (CONVERT (substr (rtos ENDSORT 2 10) 10 8))
  169.            ELAPSE (* 100000 (- STOP START)))
  170.           (repeat 3 (print))
  171.           (getstring (strcat "\n" (itoa N) " sublists sorted in "
  172.                              (rtos ELAPSE 2 2)
  173.                              " seconds. "
  174.                              "\nPress any key to continue."))
  175. );RPT_TIME
  176. (defun SEL-OPT (IG PS DF OL / HOLD);InitGet, PromptString,DeFault, OptionLine
  177.    (initget IG)
  178.    (repeat 24 (print))(princ PS)(print)(princ "\n")
  179.    (setq OPTION DF
  180.          HOLD OPTION
  181.          OPTION (getkword (strcat OL OPTION ">: ")))
  182.    (if (null OPTION)
  183.        (setq OPTION HOLD))
  184.    (repeat 5 (print))(prin1)
  185. );SEL-OPT
  186. (princ "\nSORTS loaded. Enter SORT to begin.")(print)(princ)
发表于 2007-9-12 06:17:00 | 显示全部楼层
http://www.mjtd.com/object/autolisp/ALR.default.301.htm
 楼主| 发表于 2007-9-12 09:47:00 | 显示全部楼层

谢谢大家。。。

谢谢Andyhon,程序已调试好。。谢谢。

发表于 2021-3-27 14:32:20 | 显示全部楼层

文件损坏打不开
发表于 2021-4-27 15:04:12 | 显示全部楼层
@明27662

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-5-16 21:18 , Processed in 0.203889 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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