乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 97|回复: 7

[编程交流] 列出元素

[复制链接]
T2L

4

主题

27

帖子

27

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 17:31:16 | 显示全部楼层 |阅读模式
大家好!
  1. (setq mylist '(1 2 3 4 5 6 7 8 9))
  2. (setq key#1 (getint "\nEnter first key number >>>>>: "))
  3. ;(setq key#2 (getint "\nEnter second key number >>>>>: "))
  4. (setq no 0)
  5. (repeat (length mylist)
  6. (setq keyno (list key#1))
  7. (setq combo (reverse (cons (nth no mylist) keyno)))
  8. (princ combo)
  9. (princ "\n")
  10. (setq no (1+ no))
  11. )
  12. (princ)

请在以下条件下改进上述代码:
a) 如果键号不在列表中,请重新输入数字。
b) 如果列表未按顺序排列,则结果应按顺序排列。如有必要,首先添加代码重新排列列表。
c) 删除结果中的重复项。
d) 为第二个元素询问另一个键号。如果没有输入第二个keynumber,只需执行一个keynumber。
同样没有重复结果。
 
代码的结果应如下所示:
具有一个键号的两个元素
  1. (8 1)
  2. (8 2)
  3. (8 3)
  4. (8 4)
  5. (8 5)
  6. (8 6)
  7. (8 7)
  8. (8  [color=red]No duplicate[/color]
  9. (8 9)
  10. Number of combination:>>>> ?
具有两个键号的三元素
  1. (8 2 1)
  2. (8 2 2) [color=red]No duplicate[/color]
  3. (8 2 3)
  4. (8 2 4)
  5. (8 2 5)
  6. (8 2 6)
  7. (8 2 7)
  8. (8 2  [color=red]No duplicate[/color]
  9. (8 2 9)
  10. Number of combination:>>>> ?

 
提前感谢所有愿意捐款的人。
如果您对这件事有任何想法,请随时分享,或者如果您有任何关于列表的示例代码可以分享。
再次感谢你。
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 17:42:48 | 显示全部楼层
似乎在为我工作
 
  1. (setq mylist '(1 2 3 4 5 6 7 8 9))
  2. (setq key#1 (getint "\nEnter first key number >>>>>: ")
  3.      key#2 (getint "\nEnter second key number >>>>>: ")
  4.      )
  5. (setq comb_list
  6.       (vl-remove-if
  7. (function
  8.    (lambda (a)
  9.      (or
  10.        (equal (cadr a) (caddr a) 0.00001)
  11.        (equal (car a) (caddr a) 0.00001))))
  12. (mapcar (function (lambda (x)
  13.                      (list key#1 key#2 x)))
  14.          mylist)
  15. )
  16.      )
  17. (setq num_of_combinations (length comb_list))
  18. (alert (strcat "Number of combinations is "
  19.        (itoa num_of_combinations)))

 
~'J'~
回复

使用道具 举报

5

主题

194

帖子

193

银币

初来乍到

Rank: 1

铜币
24
发表于 2022-7-6 17:55:11 | 显示全部楼层
嘿T2L
 
这是我的版本,采取了稍微不同的方法。我相信我可以被裁减一点,但这似乎符合你的要求
 
Fixo公司
 
它非常有用的函数MAPCR和LAMBDA
 
无论如何,给你
 
 
  1. ;>>>>>>>>>>>>>>>>>>>>>>>        QL V0                <<<<<<<<<<<<<<<<<<<<<<<<;
  2. ;        BY        :                Jammie                                        ;
  3. ;        DATE        :                2008-08-11                                ;
  4. ;                                MAIN FUNCTION                                ;
  5. (defun QL-V0 (/ mylist temp_list final_list test str key_nr count)
  6. ;                                local variables                                ;
  7. (setq mylist '(1 2 3 4 5 6 7 8 9))         ;>        add or remove intergers as required from mylist eg (1 2 3 4 5 6 7 8 9 10 11 n....)
  8. ;                                                                        ;
  9.      
  10. (setq temp_list  nil        ;>used to store valid user inputs
  11.      final_list nil
  12.      test       t          ;Test expression
  13.      )
  14.      
  15. ;                                local variables                                ;
  16. (setq  key_nr     nil  ) ;>        Change key_nr variable nil to restrict number of inputs, default nil means no restriction
  17.                                  ;>eg          to restrict it to two key numbers change from nil to 2
  18.                          ;>    (setq key_nr 2)
  19.    
  20. (while test ;:>        Function will loop while test is true
  21. ;                                Get Input                                ;
  22.    (setq str (getint "\n\tType number or Enter to continue >>>>>: "))
  23. ;                                First check                                ;
  24.    (if
  25.      (member str mylist)        ;:>        If the user input is a member of the mylist
  26.      (if        ;:>> &
  27. (not (member str temp_list))        ;>>>        If the input it has not been stored in a temp_list
  28. (progn
  29.   (setq temp_list  (append temp_list (list str)))        ;>>> Append it & store it in the temp_list
  30.   (princ temp_list)        ;>>> Print the stored values
  31.    );progn
  32. (princ "\n\tNumber has already been entered >>>>>: ")        ;>>> If it has been stored promt the user
  33. );if
  34.      
  35.      (princ "\n\tNot a valid entry >>>>>: ") ;:>>        If the input cannot be found in mylist promt user
  36.      );if
  37. ;                                Key Number                                ;
  38. (if key_nr ;> If the user changed the key_nr variable from nil
  39.    (if
  40.      (= (length temp_list) key_nr)        ;:>> Check the number of valid inputs againts those allowed
  41.      (setq test nil)        ;>>If the maximum is reached set the test variable to nil to end loop
  42.      )
  43.    );if
  44. (if
  45.    (= str nil)        ;>if user hits enter, getint returns nil
  46.    (setq test nil)        ;>>Set test variable to nil to end loop
  47.    );if
  48. );while
  49. ;                                Start of list sort                                ;
  50. (if temp_list        ;> If any valid inputs have been saved in temp_list
  51.   (progn;>do the following
  52.    
  53.     (setq nr     (- (length mylist) (length temp_list)));>Number of combinations
  54.    
  55. ;                         compare mylist with temp_list                        ;
  56.     (foreach n mylist
  57.       (if
  58. (not (member n temp_list));>> if anyitem in the mylist has not been entered
  59. (setq final_list (append final_list (list n))) ;>> List
  60. );if
  61.       );foreach
  62.    
  63.     (if final_list ;>If any diffenences have been found
  64.       (progn
  65. (foreach n final_list
  66.    (progn
  67.      (terpri);force new line
  68.      (princ (append temp_list (list n))))
  69.    );foreach
  70. (PRINC (STRCAT "\n\tNumber of combination :>>>> \t" (rtos (length final_list) 2 0)))
  71. (princ)
  72. );progn
  73.       (progn
  74. (princ "\n\tNothing to print as all possible numbers have been entered... : >>>>>: ")
  75. (princ)
  76. );progn
  77.       );if
  78.     );progn
  79.   (progn
  80.   (princ "\n\tExiting programme... : >>>>>: ")
  81.   (princ)
  82.   )
  83. );if
  84. )
  85. (defun c:ql ()
  86. (QL-V0)
  87. )
  88. (progn
  89. (princ (strcat "\t\nQL V0 LOADED... \t\nType QL to begin..."))
  90. (princ)
  91. )
  92.        
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 18:05:01 | 显示全部楼层
代码的最终结果应打印如下:
  1. (defun c:testCAB (/ mylist key#1 key#2 cnt)
  2. (setq mylist '(1 2 3 4 5 6 7 8 9))
  3. (setq key#1 (getint "\nEnter first key number >>>>>: ")
  4.        key#2 (getint "\nEnter second key number >>>>>: ")
  5. )
  6. (and (= key#1 key#2) (setq key#2 nil))
  7. (setq cnt 0)
  8. (mapcar '(lambda (key)
  9.             (mapcar '(lambda (x)
  10.                        (if (equal key x 0.00001)(setq cnt (1+ cnt)))) mylist))
  11.          (list key#1 key#2))
  12. (or (and (zerop cnt) (not(alert "No combonations.")))
  13.      (alert (strcat "Number of combinations is " (itoa (- (length mylist) cnt))))
  14. )
  15. (princ)
  16. )

或者像这样:
  1. (8 1)
  2. (8 2)
  3. (8 3)
  4. (8 4)
  5. (8 5)
  6. (8 6)
  7. (8 7)
  8. (8 9)
  9. Number of combination:>>>> 8

mylist应该是全局变量。
 
谢谢
回复

使用道具 举报

T2L

4

主题

27

帖子

27

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 18:06:05 | 显示全部楼层
再试一次。
  1. ((8 1) (8 2) (8 3) (8 4) (8 5) (8 6) (8 7) (8 9))
  2. Number of combination:>>>> 8
回复

使用道具 举报

CAB

29

主题

781

帖子

430

银币

中流砥柱

Rank: 25

铜币
526
发表于 2022-7-6 18:21:11 | 显示全部楼层
回复

使用道具 举报

T2L

4

主题

27

帖子

27

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 18:27:43 | 显示全部楼层
The end result of the code should print out like this:
  1. (8 1)(8 2)(8 3)(8 4)(8 5)(8 6)(8 7)(8 9)Number of combination:>>>> 8
or like this:
  1. ((8 1) (8 2) (8 3) (8 4) (8 5) (8 6) (8 7) (8 9))Number of combination:>>>> 8
mylist should be global variable.
 
Thanks.
回复

使用道具 举报

CAB

29

主题

781

帖子

430

银币

中流砥柱

Rank: 25

铜币
526
发表于 2022-7-6 18:38:45 | 显示全部楼层
Trying again.
  1. (defun c:combos (/ key#1 key#2 result) (setq key#1 (getint "\nEnter first key number >>>>>: ")       key#2 (getint "\nEnter second key number >>>>>: ") ) (setq cnt 0) (mapcar '(lambda (x)             (if (not(or (equal key#1 x 0.00001)                         (equal key#2 x 0.00001)))               (setq result (cons x result))           ))         mylist) (or (and (null result) (not(alert "No combonations.")))     (and (princ (strcat "\nNumber of combonations is " (itoa (length result))"\n"))          (princ (reverse result))) ) (princ))
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-4 13:56 , Processed in 0.712684 second(s), 68 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表