Tcl - 内置函数

Tcl为各种操作提供了许多内置函数(过程).这包括 :

  • list 处理.

  • string的功能处理.

  • 数组处理.

  • 词典处理的功能.

  • 文件I/O 处理的功能.

  • 用于创建名称空间和包.

  • 数学运算的函数.

  • 系统操作的功能.

以上各项除数学和系统功能在前面介绍过章节.数学和系统内置函数说明如下.

数学函数

下表列出了Tcl中可用的数学函数 :

Sr.No.方法&说明
1

abs arg

计算arg的绝对值.

2

acos arg

计算arg的反余弦值.

3

asin arg

计算arg的反正弦值.

4

atan arg

计算反义词arg.

5

atan2 yx

计算其参数(y/x)的商的反正切值.

6

ceil arg

计算大于或等于数字的最小整数.

7

cos arg

计算arg的余弦值.

8

cosh arg

计算arg的双曲余弦值.

9

double arg

计算arg是否为浮点值,返回arg,否则将arg转换为浮点并返回转换后的值.

10

exp arg

计算一个指数函数(e提高到arg的幂).

11

floor arg

计算小于或等于arg的最大整数.

12

fmod xy

计算浮点余数x的除法.如果y为0,则返回错误.

13

hypot xy

计算直角三角形sqrt的斜边长度(x * x + y * y).

14

int arg

计算arg是否是与机器字相同宽度的整数值,返回arg,否则将arg转换为整数./p>

15

log arg

计算arg的自然对数.

16

log10 arg

计算基数10 arg的对数.

17

pow xy

计算x增加到幂y的值.如果x为负数,则y必须为整数值.

18

rand

计算0到1之间的伪随机数.

19

round arg

计算arg的值四舍五入到最接近的整数.

20

sin arg

计算arg的正弦值.

21

sinh arg

计算arg的双曲正弦.

22

sqrt arg

计算平方根arg. arg必须是正面的.

23

srand arg

计算0到1之间的伪随机数.arg必须是整数,用于重置rand随机数生成器的种子.

24

tan arg

计算arg的正切值.

25

tanh arg

计算arg的双曲正切.

26

arg

计算至少64位宽的整数值(如果arg是a,则通过符号扩展计算如果arg已经不是一个,则为32位数字.

使用数学函数的一些例子给出低于 :

#!/usr/bin/tclsh

namespace import ::tcl::mathfunc::*
puts [tan 10]
puts [pow 10 2]
puts [ceil 10.34]
puts [hypot 10 20]
puts [srand 45]
puts [log 10]
puts [srand 45]

执行上面的代码时,它产生以下结果 :

0.6483608274590866
100.0
11.0
22.360679774997898
0.0003521866166741525
2.302585092994046
0.0003521866166741525

系统函数

Tcl中的重要系统函数包括,

  • 时钟 : 秒函数,以秒为单位返回当前时间.

  • 时钟 : 格式函数,将秒格式化为日期和时间.

  • 时钟 : 扫描功能,扫描输入字符串并将其转换为秒.

  • 打开 : 函数,用于打开文件.

  • exec :  function,用于执行系统命令.

  • 关闭 : 函数,用于关闭文件.

以上函数的一些示例列在下面 :

#!/usr/bin/tclsh

#get seconds
set currentTime [clock seconds]
puts $currentTime
#get format 
puts "The time is: [clock format $currentTime -format %H:%M:%S]"
puts "The date is: [clock format $currentTime -format %D]"

set date "Jun 15, 2014"
puts [clock scan $date -format {%b %d, %Y}]

puts [exec ls]
puts [exec dir]

set a  [open input.txt]
puts [read $a];
puts $a
close $a

执行上述代码时,会产生以下结果 :

1402819756
The time is: 03:09:16
The date is: 06/15/2014
1402808400
input.txt
main.tcl
input.txt  main.tcl
This is the file you can use to provide input to your program and later on open
   it inside your program to process the input.

file3

下表提供了可用于格式化日期和时间的列表字符串.

Sr.No.格式&说明
1

%a

日期缩短形式,例如:Sun.

2

%A

完整日例如:星期天.

3

%b

缩短月份.

4

%B

月完整形式.

5

%d

日期.

6

%j

朱利安日一年.

7

%m

月份数.

8

%y

两年级数字.

9

%Y

第一年n四位数.

10

%H

24小时制小时.

11

%I

12小时制的小时.

12

%M

分钟.

13

%S

秒.

14

%p

AM或PM.

15

%D

日期数量,mm/dd/yy.

16

%r

12小时制的时间.

17

%R

没有秒的24小时制的时间.

18

%T

24小时制时间,秒数.

19

%Z

时区名称,如GMT,IST,EST等.