一.填空题(每题3分,共30分)
1. regexp { ([0-9]+) *([a-z]+)} ” there is 100 apples” total num word
puts ” $total ,$num,$word”
最后输出结果为___________________.
参考:100 apples ,100,apples
2. regsub there ”They live there lives ” their x
puts $x
最后输出结果为___________________.
参考:They live their lives
3.(每空一分)
TCL提供三种形式的置换:_________,_____________和____________.
参考:变量置换、命令置换和反斜杠置换
4. set x 10
Set y $x+100
最后输出结果为___________________.
参考:10+100
5.(每空1分)
Set x 100
Set y “$x ddd” 此句输出内容为__________
set y {/n $x} 此句输出内容为__________
Set y [expr {$x+10}]此句输出内容为__________
参考:100 ddd
/n $x
110
6.建立一个数组day,它有两个元素Monday,Tuesday,值分别为1 2
创建语句为:_______________
_______________
参考:set day(monday) 1
set day(tuesday) 2
7. lindex {1 2 {3 4}} 2
输出结果为_______________
参考:3 4
8. linsert { 1 2 {3 4} 1 7 8 {9 10}
输出结果为_______________
参考:1 7 8 {9 10} 2 {3 4}
9.string first ab defabc
输出结果为_______________
参考:3
10. catch {return “all done”} string
Set string
输出结果为_______________
参考:all done
二.简答题(每题10分,共30分)
1.#!/usr/bin/tclsh
#
#Demonstrate operators and
#math functions
set PI [expr 2*asin(1.0)]
if {$argc==3} {
set X [lindex $argv 0]
set Y [lindex $argv 1]
set Rad [lindex $argv 2]
set Dist [expr sqrt(($X*$X)+($Y*$Y))]
set Cir [expr 2*PI*$Rad]
set Area [expr $Rad*$Rad]
puts stdout “Distance = $Dist”
puts stdout “Circumference = $Cir”
} else {
puts stdout “Wrong argument count!”
puts stdout “Needs X,Y ,and Radius”
}
提示,asin(1.0)值为1.5707963 Linux下以上脚本程序输出内容为:
______________________________________
参考: Wrong argument count!
Needs X,Y ,and Radius
2. #!/usr/bin/tclsh
#
#Demonstate global variables
#and backslash substitution
if {$argc >=1 } {
set N 1
foreach Arg $argv {
puts stdout “$N:$Arg\n”
set N [expr $N +1 ]
if {$Arg==”ring”} {
puts stdout “\a”
}
}
} else {
puts stdout “$argv0 on X Display $env(DISPLAY)\n”
}
Linux中以上脚步命名为hello3,则运行脚本以下结果为:
$ ./hello3.tcl
______________________
$ ./hello3.tcl ring
_______________________
3.当 y值分别为 a b c时
以下程序运行结果是什么?为什么会是这个结果
set x 10
switch $y {
a {incr $x}
b {incr $y}
default {incr $x}
}
_________________
参考:均出错,incr表达式应为 incr 变量 数值
三.编程题(每题20分,共40分)
参考代码:
proc main {x} {
if {$x==0} {
return 1
} else {
set sum 1
for { set i 0} {$i<=[expr $x-1]} {incr i 1} {
set sum [expr $sum+[expr $sum*$i]]
}
return $sum
}
}
参考代码:
proc 2to10 {x} {
set sum 0
proc length {x} {
string length $x
}
set n [length $x]
for {set i 0} {$i<=$n-1} {incr i 1} {
proc a {x i} {
string index $x end-$i
}
if {[a $x $i]==0} {
} elseif {[a $x $i]==1} {
set a [expr pow(2,$i)]
set b [expr [a $x $i]]
set p [expr [expr $a*$b]]
set sum [expr $sum+$p]
} else {
puts stdout “请正确输入二进制!”
return
}
}
puts stdout $sum
}