tcl [3]: 设置变量,数学运算,强制类型转换

本文详细介绍了TCL语言中如何设置变量、执行数学运算以及进行强制类型转换的操作,对于理解TCL的基本语法和提升编程能力具有重要作用。
# <2> 设置变量
set X "this is a string, Y=:"
set Y 1.23
puts $X
puts $Y
puts "**********"
puts "$X $Y"

# <3> 数学计算
# [expr {$a - $b}]
# >>>>> eq ne in ni <<<<<<
# > compare two strings for equality (eq) or inequality (ne). and two operators for checking 
# > if a string is contained in a list (in) or not (ni). These operators all return 1 (true) or 0 (false).
# > Using these operators ensures that 
# > the operands are regarded exclusively as strings (and lists), not as possible numbers:
# >>>>>>>>>>>>>>>>>>>>>>>>
set X 100
set Y 256

set Z [expr {$Y + $X}]
set Z_LABEL "$Y plus $X is "

puts "$Z_LABEL $Z"
puts "The square root of $Y is [expr { sqrt($Y) }]\n"

puts "Because of the precedence rules \"5 + -3 * 4\"   is: [expr {-3 * 4 + 5}]"
puts "Because of the parentheses      \"(5 + -3) * 4\" is: [expr {(5 + -3) * 4}]"

set A 3
set B 4
puts "The hypotenuse of a triangle: [expr {hypot($A,$B)}]"

#
# The trigonometric functions work with radians ...
#
set pi6 [expr {3.1415926/6.0}]
puts "The sine and cosine of pi/6: [expr {sin($pi6)}] [expr {cos($pi6)}]"

#
# <4> 数组 Working with arrays
#
set a(1) 10
set a(2) 7
set a(3) 17
set b    2
puts "Sum: [expr {$a(1)+$a($b)}]"

# <5> 数的类型
# >>> 整形数相乘除,得到的数如果是浮点型,此浮点型不能被输出,而是以整形输输出
# >>> 如果需要计算结果输出浮点型,有两种方法
# >>> 1. 运算子其中一个为浮点型,如"1."
# >>> 2. 强制类型转换 double (1), tcl中浮点型为double,没有float
puts "1/2 is [expr {1/2}]"
puts "-1/2 is [expr {-1/2}]"
puts "1/2 is [expr {1./2}]"
puts "1/3 is [expr {1./3}]"
puts "1/3 is [expr {double(1)/3}]"

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值