Tcl提供了一些內(nèi)置的功能(程序),用于各種操作。這包括,
列表處理函數(shù)。
字符串處理函數(shù)。
數(shù)組處理函數(shù)。
字典處理函數(shù)。
文件I/O處理函數(shù)。
命名空間和包處理函數(shù)。
數(shù)學處理函數(shù)。
操作系統(tǒng)處理函數(shù)。
數(shù)學和系統(tǒng)函數(shù)以外的都包含在前面的章節(jié)。數(shù)學與系統(tǒng)內(nèi)置函數(shù)說明如下。
Tcl的數(shù)學函數(shù)可列于下表中。
| SN | 方法名稱 | 描述 |
|---|---|---|
| 1 | abs arg | 計算arg的絕對值。 |
| 2 | acos arg | 計算arg的反余弦值。 |
| 3 | asin arg | 計算arg的反正弦。 |
| 4 | atan arg | 計算arg的反正切。 |
| 5 | atan2 y x | 計算其參數(shù)的比值(y/x)的反正切。 |
| 6 | ceil arg | 計算比最小整數(shù)大于或等于的一個數(shù)。 |
| 7 | cos arg | 計算arg的余弦值。 |
| 8 | cosh arg | 計算arg的雙曲余弦值。 |
| 9 | double arg | 計算arg如果是一個浮點值,返回arg,否則arg轉(zhuǎn)換為浮點,并返回轉(zhuǎn)換后的值。 |
| 10 | exp arg | 計算指數(shù)函數(shù)(e的arg次冪)。 |
| 11 | floor arg | 計算比最大的整數(shù)小于或等于arg。 |
| 12 | fmod x y | 由y計算x相除的浮點余數(shù)。如果y是0,則返回一個錯誤。 |
| 13 | hypot x y | 計算一個直角三角形的斜邊的長度 sqrt(x*x+y*y). |
| 14 | int arg | 計算,如果arg是相同的寬度的機器字的整數(shù)值,則返回arg,否則轉(zhuǎn)換arg為整數(shù)。 |
| 15 | log arg | 計算arg的自然對數(shù)。 |
| 16 | log10 arg | 計算以10為底arg的對數(shù)。 |
| 17 | pow x y | 計算y的x的冪值。如果x為負,y必須是一個整數(shù)值。 |
| 18 | rand | 計算0和1之間的偽隨機數(shù)。 |
| 19 | round arg | 計算arg的四舍五入為最接近的整數(shù)的值。 |
| 20 | sin arg | 計算arg的正弦值。 |
| 21 | sinh arg | 計算arg的雙曲正弦。 |
| 22 | sqrt arg | 計算arg的平方根。arg必須為正。 |
| 23 | srand arg | 計算0和1之間所述arg一個偽隨機數(shù),它必須是一個整數(shù),用來復位的種子隨機數(shù)發(fā)生器。 |
| 24 | tan arg | 計算arg的正切值。 |
| 25 | tanh arg | 計算arg的雙曲正切值。 |
| 26 | wide arg | 計算用于arg,如果它不是整數(shù)值至少64位寬(通過符號擴展,如果arg是一個32位的數(shù)字)。 |
使用數(shù)學函數(shù)的一些例子如下。
#!/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]
當執(zhí)行上面的代碼,它產(chǎn)生了以下結(jié)果。
0.6483608274590866 100.0 11.0 22.360679774997898 0.0003521866166741525 2.302585092994046 0.0003521866166741525
在Tcl中包含重要的系統(tǒng)函數(shù)如下,
clock - 秒函數(shù)返回當前時間以秒為單位。
clock - 格式化函數(shù)格式化秒到的日期和時間。
clock - 掃描函數(shù)掃描輸入字符串,并將其轉(zhuǎn)換為秒。
open - 函數(shù)用于打開一個文件。
exec - 函數(shù)用于執(zhí)行一個系統(tǒng)命令。
close - 函數(shù)用于關閉一個文件。
對于上述函數(shù)的一些實例在下面列出。
#!/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
當執(zhí)行上面的代碼,產(chǎn)生以下結(jié)果:
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
下表提供了可用于格式化的日期和時間的列表字符串。
| SN | 格式 | 描述 |
|---|---|---|
| 1 | %a | 天縮寫形式,例如:Sun. |
| 2 | %A | 天完全形式,例如:Sunday. |
| 3 | %b | 月份縮寫形式。 |
| 4 | %B | 月份完整形式。 |
| 5 | %d | 月份中日期 |
| 上一篇:TCL錯誤處理下一篇:TCL數(shù)組 |