位置:首页 » 文章/教程分享 » 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