Tcl Open
proc invokeQuads { } {
set f [open |quads r+]
调用 Tcl 命令 open 用 |
作为参数的第一个字符。它确保这个 open 被作为命令管道而被调用。 open
使用参数的其余部分 quads 来建立指名的进程。在这种情况下,使用从 open 返回的标识符 f 来与子进程传输数据。
foreach e {.f.a .f.b .f.c} {
set entry [$e get]
if { [string compare $entry ""] == 0 } {
puts stdout "Some entry(ies) are null .... enter them Now \n"
close $f
return
} else {
puts $entry
}
}
flush $f
对于每个录入组件 ".f.a"、".f.b" 和 ".f.c",通过
"get" 动作得到键入的值。如果录入组件是空的,则提示用户输入。关闭管道并且进程从过程返回。如果值是非空,则把它写到管道中。刷新管道来把数据从缓冲区传输到"quads"。
注意这些录入组件没有对 <Return> 事件的绑定。
gets $f in_prompt ;# Input the coefficients a,b,c
gets $f disc ;# DISC : 1.0000000000000
gets $f iflag ;# IFLAG = 0
gets $f aux_msg ;# ROOTS ARE REAL or ... Complex ...
gets $f roots ;# x1 = 2.0000000000000 x2 = 1.0000000000000
close $f ;# now you can close
每次读与 quads
关联的输出缓冲区来得到一个非终结的字符串并赋给一个变量。这些字符串是 quads
通过 PRINT 和 WRITE 生成的消息。读完 quads 所有的消息之后,关闭管道。
set w .info.dum ;# a quick fix - reassignment avoids the need to declare
;# w as a global
if { [regexp {(COMPLEX|complex)} $aux_msg cmplx] == 1 } {
.zeros.x1 configure -text "Real Part"
.zeros.x2 configure -text "Imaginary Part"
$w.type config -text $aux_msg
} else {
.zeros.x1 configure -text x1
.zeros.x2 configure -text x2
$w.type config -text $aux_msg
}
regexp {(x1 = [ ]*[+|-]*[0-9]*\.[0-9]*)} $roots val1
regexp {(x2 = [ ]*[+|-]*[0-9]*\.[0-9]*)} $roots val2
.zeros.x1val configure -text $val1
.zeros.x2val configure -text $val2
$w.disc configure -text $disc
}
上面的脚本多次的使用了 Tcl regexp
命令来得到用于更改 GUI 和显示计算结果所需要的值。分析第一个字符串 "aux_msg" 来检查根是否是复数的(查找将找寻子串 COMPLEX 或 complex)。如果找到子串 "complex" ,则 x1 和 x2 表示两个复数根的实部和虚部。
否则 x1 和 x2 表示两个实根。
GUI 还显示判别式。调用 Tk 组件的 configure 动作来设置标签字符串。
proc clearEntries { } {
foreach e {.f.a .f.b .f.c} {
$e delete 0 end
}
}
过程 "clearEntries"
通过删除录入组件中从第一个到最后一个位置上的所有字符来删除录入组件中的所有数据。你可以调用它并多次调用 quads。