乐筑天下

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

[编程交流] 构造列表,总和

[复制链接]

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 17:54:14 | 显示全部楼层 |阅读模式
大家好,
 
昨天我在试验读取条形码值。
但在这个例子中,我会尽量保持简单:
  1. ; Example input: 0011 0012 0033... 00XX
  2. ; split 0011 to 00 and 11 (the first and 2nd character are separated from 3rd and 4th)
  3. ; store 00 value and calculate 11 value (first and 2nd character is stored, and 3rd with 4rth are used for calculations)
  4. ; list is constructed with the calculation values
  5. ; sum the total of the calculation values in the while loop
  6. ; when loop is exited display the grand total of calculation values
  7. (defun C:test (/ lst calc-val-list )
  8. (setq lst '( 0 ))
  9. (while
  10. (setq get-val (getstring t "\nInput value: ")) ; example 0011
  11. (setq code-val (strcat (substr get-val 1 2 ))) ; stored 00
  12. (setq calc-val (strcat (substr get-val 3 2 ))) ; stored 11 string
  13. (setq calc-val-num (strcat (rtos (atof calc-val) 2 5) )) ; stored 11 number
  14. (princ (strcat "\nCode-val: " code-val " " )) ; display 00 for check
  15. (princ (strcat "\nCalc-val: " calc-val-num " " )) ; display 11 number for check
  16. (setq calc-val-list (append '(calc-val-num) lst)) ; constructs a list with the stored values (calc-val-num) ; <---- problem here?
  17. (princ (strcat "\nCalc-val-list: " calc-val-list " " )) ; this doesn't print, problem?, cannot check
  18. );while
  19. (setq grand-total  (apply '+ (calc-val-list)) ) ; sums the items in the list ; <---- problem here?
  20. (princ (strcat "\nGrand-total: " grand-total " " )) ; this doesn't print, problem?, cannot check
  21. (princ)
  22. );defun

 
 
为此:
 
  1. (setq calc-val-list (append '(calc-val-num) lst)) ; constructs a list with the stored values (calc-val-num) ; <---- problem here?
  2. (princ (strcat "\nCalc-val-list: " calc-val-list " " )) ; this doesn't print, problem?, cannot check
回复

使用道具 举报

26

主题

1495

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-5 18:04:10 | 显示全部楼层
你好
 
变量calc val num既不能像函数一样使用,也不能作为符号使用,因此请尝试以下方法:;
 
  1. (setq grand-total  (apply '+ (calc-val-list)) ) ; sums the items in the list ; <---- problem here?
  2. (princ (strcat "\nGrand-total: " grand-total " " )) ; this doesn't print, problem?, cannot check
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 18:11:59 | 显示全部楼层
我没有阅读完整的例程,从第一次遇到你有其他问题,所以这必须是一个完整的遵循你的用户输入指令;
 
  1. (setq calc-val-list (append '(calc-val-num) lst))
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 18:20:57 | 显示全部楼层
谢谢你的帮助,大卫和塔瓦!
 
看来
重新定义列表,查看它为“calc val list”返回的内容:
  1. (setq calc-val-list (append (list calc-val-num) lst))

我还试图找到一种退出循环并显示总计的方法,所以我做到了:
  1. (setq calc-val-list (append (list calc-val-num) lst))   
  2. (princ (strcat "\nCalc-val-list: " (vl-princ-to-string calc-val-list) " "))                              
  3.    ) ;; end of While
  4. (setq grand-total (apply '+ (mapcar '(lambda (n) (if (= (type n) 'STR) (atoi n) n)) calc-val-list)))
  5. (princ (strcat "\nGrand-total: " (itoa grand-total) " "))

但对于上述问题,我得到(在0011和0022之后)输入:
  1. (setq calc-val-list (append (list calc-val-num) lst))

在这种情况下,它应该显示总计33。
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 18:36:28 | 显示全部楼层
这就是你想要的吗?
  1. 8
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 18:45:01 | 显示全部楼层
没错,李!
现在我将学习你们的例子,继续我的实验。
非常感谢!
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 18:52:15 | 显示全部楼层
不客气-如果有任何问题,请随时提问。
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 18:56:59 | 显示全部楼层
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-13 01:10 , Processed in 0.690966 second(s), 68 queries .

© 2020-2025 乐筑天下

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