TCL算術運算符

下表列出了所有Tcl語言支持的算術運算符。假設變量A=10,變量B=20,則:

運算符

描述

例子

+

兩個數相加

A + B = 30

-

第一個操作數減去第二個操作數

A - B = -10

*

兩個操作數相乘

A * B = 200

/

分子(B)除以分母(A)

B / A = 2

%

模運算和整數除法後的餘數

B % A = 0

例子

試試下面的例子就明白了所有Tcl語言中可用的算術運算符:

#!/usr/bin/tclsh

set a 21
set b 10
set c [expr $a + $b]
puts "Line 1 - Value of c is $c\n"
set c [expr $a - $b]
puts "Line 2 - Value of c is $c\n"
set c [expr $a * $b]
puts "Line 3 - Value of c is $c\n"
set c [expr $a / $b]
puts "Line 4 - Value of c is $c\n"
set c [expr $a % $b]
puts "Line 5 - Value of c is $c\n"

當編譯和執行上面的程序,它會產生以下結果:

Line 1 - Value of c is 31

Line 2 - Value of c is 11

Line 3 - Value of c is 210

Line 4 - Value of c is 2

Line 5 - Value of c is 1