📜  Tcl-内置函数

📅  最后修改于: 2020-10-16 06:31:05             🧑  作者: Mango


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

除数学和系统功能外,以上各章均在前面的章节中介绍。下面说明数学和系统内置函数。

数学函数

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

Sr.No. Method & Description
1

abs arg

Calculates the absolute value of arg.

2

acos arg

Calculates the arccosine of arg.

3

asin arg

Calculates the arcsine of arg.

4

atan arg

Calculates the arctangent of arg.

5

atan2 y x

Calculates the arctangent of the quotient of its arguments(y/x).

6

ceil arg

Calculates the smallest integer greater than or equal to a number.

7

cos arg

Calculates the cosine of arg.

8

cosh arg

Calculates the hyperbolic cosine of arg.

9

double arg

Calculates if arg is a floating-point value, returns arg, otherwise converts arg to floating-point and returns the converted value.

10

exp arg

Calculates an exponential function (e raised to the power of arg).

11

floor arg

Calculates the largest integer less than or equal to arg.

12

fmod x y

Calculates the floating-point remainder of the division of x by y. If y is 0, an error is returned.

13

hypot x y

Calculates the length of the hypotenuse of a right-angled triangle sqrt(x*x+y*y).

14

int arg

Calculates if arg is an integer value of the same width as the machine word, returns arg, otherwise converts arg to an integer.

15

log arg

Calculates the natural logarithm of arg.

16

log10 arg

Calculates the base 10 logarithm of arg.

17

pow x y

Calculates the value of x raised to the power y. If x is negative, y must be an integer value.

18

rand

Calculates a pseudo-random number between 0 and 1.

19

round arg

Calculates the value of arg rounded to the nearest integer.

20

sin arg

Calculates the sine of arg.

21

sinh arg

Calculates the hyperbolic sine of arg.

22

sqrt arg

Calculates the square root of arg. arg must be positive.

23

srand arg

Calculates a pseudo-random number between 0 and 1. The arg, which must be an integer, is used to reset the seed for the random number generator of rand.

24

tan arg

Calculates the tangent of arg.

25

tanh arg

Calculates the hyperbolic tangent of arg.

26

wide arg

Calculates integer value at least 64-bits wide (by sign-extension if arg is a 32-bit number) for arg if it is not one already.

下面给出一些使用数学函数的示例-

#!/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中重要的系统功能包括:

  • clock -seconds函数,以秒为单位返回当前时间。

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

  • 时钟−扫描函数,它扫描输入的字符串并将其转换为秒。

  • open-函数,用于打开文件。

  • exec – 函数,用于执行系统命令。

  • close-函数,用于关闭文件。

下面列出了上述功能的一些示例-

#!/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. Format & Description
1

%a

Day in short form, eg:Sun.

2

%A

Day in full form eg:Sunday.

3

%b

Month in short form.

4

%B

Month in full form.

5

%d

Day of month.

6

%j

Julian day of year.

7

%m

Month in number.

8

%y

Year in two digits.

9

%Y

Year in four digits.

10

%H

Hour in 24 hour clock.

11

%I

Hour in 12 hour clock.

12

%M

Minutes.

13

%S

Seconds.

14

%p

AM or PM.

15

%D

Date in number, mm /dd/yy.

16

%r

Time in 12 hour clock.

17

%R

Time in 24 hour clock without seconds.

18

%T

Time in 24 hour clock with seconds.

19

%Z

Time Zone Name like GMT, IST, EST and so on.