明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1217|回复: 8

如何判定一个单行文本是实数或整数?

[复制链接]
发表于 2007-11-22 13:38 | 显示全部楼层 |阅读模式

实数或整数 返回 t

其他           返回 nil

有这样的函数吗?

发表于 2007-11-22 15:09 | 显示全部楼层

 用TYPE函数。

    (setq a 123 r 3.45 s "Hello! " x '(a b c))

    (setq f (open "name" "r"))

    (type 'a)           返回          SYM

    (type a)            返回          INT

    (type f)            返回          FILE    

    (type r)            返回          REAL

    (type s)            返回          STR

    (type x)            返回          LIST

    (type +)            返回         SUBR

    (type nil)          返回          nil

 

  (defun isint (a)

(if (= (type a) 'INT)   (是整型数形态吗? )

    T                   (是, 返回 T)

    Nil                 (否, 返回 nil)

    )

    )     

(if (= (type a) 'INT)   (是整型数形态吗? )

    T                   (是, 返回 T)

    Nil                 (否, 返回 nil)

    )

    )     

发表于 2007-11-22 15:21 | 显示全部楼层
本帖最后由 作者 于 2007-11-22 15:23:30 编辑

(defun Text_NumberP (txt)
  (numberp (read (cdr (assoc 1 (entget txt)))))
)

;;test

(Text_NumberP (car(entsel "\n选择单行文字: ")))

 楼主| 发表于 2007-11-22 16:02 | 显示全部楼层
本帖最后由 作者 于 2007-11-22 16:07:38 编辑

caoyin发表于2007-11-22 15:21:00(defun Text_NumberP (txt)  (numberp (read (cdr (assoc 1 (entget txt))))));;test(Text_NumberP (car(entsel \"\n选择单行文字: \")))

有两个问题:

1.  如果是一行文本就一个小数点,怎么出错了

     选择单行文字: ; 错误: 输入中的点位置不正确

2.  如果有空格,像"123   456"、"123    abc" 也会返回t,但像这样的应该返回nil才对

发表于 2007-11-22 17:07 | 显示全部楼层
本帖最后由 作者 于 2007-11-22 17:13:28 编辑

(defun Text_NumberP (txt / lst)
  (setq lst (vl-string->list (cdr (assoc 1 (entget txt)))))
  (and (apply 'and (mapcar '(lambda (x)
                                             (member x '(46 48 49 50 51 52 53 54 55 56 57))
                                           )
                                          lst
                             )
          )
          (/= (car lst) 46)
  )
)

;;test
(Text_NumberP (car(entsel "\n选择单行文字: ")))

 楼主| 发表于 2007-11-23 08:30 | 显示全部楼层

vl-string->list 转乘字符代码逐一判断,学习了

第三行很深奥,没看明白

发表于 2007-11-23 13:07 | 显示全部楼层

;;;简单判断字符型x值是整数,还是实数;

(setq x "333.73aa")
;(setq x ".3355")
;(setq x "564")
;x中不能以0-9或.以外的字符开头,否则判断都是整数!
;;;;程式开始;;;;;
(setq y (atof x))  ;转成实数型 ==> y=333.73
(setq z (atoi x))  ;转成整数值 ==> z=333
(if (> (- y z) 0)
    (princ "\n实数")
    (princ "\n整数")
)

发表于 2007-11-24 08:39 | 显示全部楼层
本帖最后由 作者 于 2007-11-24 9:42:20 编辑

楼上的方法好像有点问题
(setq x "333.0") 判断也是整数吗


;;根据AutoCAD的某些逻辑,重新写了一个

;; ▓ (l:str-numberP str)
;; [功能] 判断字符串 str 是否是数字
;; [返回] 整数则返回整数,实数则返回实数。其他->nil
;; [测试] (l:str-NumberP ".1")->0.1
;;        (l:str-NumberP " .1")->0.1
;;        (l:str-NumberP "0 .1")->nil
;;        (l:str-NumberP "0.1")->0.1
;;        (l:str-NumberP "1")->1
;;        (l:str-NumberP "1.")->1.0
(defun l:str-NumberP (str / num)
  (if (numberp (vl-catch-all-apply 'angtof (list str)))
    (progn
      (setq num (vl-catch-all-apply 'read (list str)))
      (if (vl-catch-all-error-p num)
        (atof str)
        num
      )
    )
  )
)

发表于 2007-11-24 11:13 | 显示全部楼层
  强..考虑周到...学习.....
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-18 12:33 , Processed in 0.272623 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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