Grrr 发表于 2022-7-5 17:54:14

构造列表,总和

大家好,
 
昨天我在试验读取条形码值。
但在这个例子中,我会尽量保持简单:
; Example input: 0011 0012 0033... 00XX
; split 0011 to 00 and 11 (the first and 2nd character are separated from 3rd and 4th)
; store 00 value and calculate 11 value (first and 2nd character is stored, and 3rd with 4rth are used for calculations)
; list is constructed with the calculation values
; sum the total of the calculation values in the while loop
; when loop is exited display the grand total of calculation values

(defun C:test (/ lst calc-val-list )

(setq lst '( 0 ))

(while

(setq get-val (getstring t "\nInput value: ")) ; example 0011
(setq code-val (strcat (substr get-val 1 2 ))) ; stored 00
(setq calc-val (strcat (substr get-val 3 2 ))) ; stored 11 string
(setq calc-val-num (strcat (rtos (atof calc-val) 2 5) )) ; stored 11 number
(princ (strcat "\nCode-val: " code-val " " )) ; display 00 for check
(princ (strcat "\nCalc-val: " calc-val-num " " )) ; display 11 number for check

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

);while

(setq grand-total(apply '+ (calc-val-list)) ) ; sums the items in the list ; <---- problem here?
(princ (strcat "\nGrand-total: " grand-total " " )) ; this doesn't print, problem?, cannot check

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

David Bethel 发表于 2022-7-5 18:04:10

你好
 
变量calc val num既不能像函数一样使用,也不能作为符号使用,因此请尝试以下方法:;
 
(setq grand-total(apply '+ (calc-val-list)) ) ; sums the items in the list ; <---- problem here?
(princ (strcat "\nGrand-total: " grand-total " " )) ; this doesn't print, problem?, cannot check

Tharwat 发表于 2022-7-5 18:11:59

我没有阅读完整的例程,从第一次遇到你有其他问题,所以这必须是一个完整的遵循你的用户输入指令;
 
(setq calc-val-list (append '(calc-val-num) lst))

Tharwat 发表于 2022-7-5 18:20:57

谢谢你的帮助,大卫和塔瓦!
 
看来
重新定义列表,查看它为“calc val list”返回的内容:

(setq calc-val-list (append (list calc-val-num) lst))

我还试图找到一种退出循环并显示总计的方法,所以我做到了:

(setq calc-val-list (append (list calc-val-num) lst))   
(princ (strcat "\nCalc-val-list: " (vl-princ-to-string calc-val-list) " "))                              
   ) ;; end of While
(setq grand-total (apply '+ (mapcar '(lambda (n) (if (= (type n) 'STR) (atoi n) n)) calc-val-list)))
(princ (strcat "\nGrand-total: " (itoa grand-total) " "))

但对于上述问题,我得到(在0011和0022之后)输入:
(setq calc-val-list (append (list calc-val-num) lst))
在这种情况下,它应该显示总计33。

Grrr 发表于 2022-7-5 18:36:28

这就是你想要的吗?
8

Lee Mac 发表于 2022-7-5 18:45:01

没错,李!
现在我将学习你们的例子,继续我的实验。
非常感谢!

Grrr 发表于 2022-7-5 18:52:15

不客气-如果有任何问题,请随时提问。

Lee Mac 发表于 2022-7-5 18:56:59

页: [1]
查看完整版本: 构造列表,总和