这是TCL中的代码,用于生成由用户给出的作为参数的数字阶乘。TCL阶乘计算代码:紧跟大括号后的额外字符
if {$argc !=1}{
puts stderr "Error! ns called with wrong number of arguments! ($argc)"
exit 1
} else
set f [lindex $argv 0]
proc Factorial {x}{
for {set result 1} {$x>1}{set x [expr $x - 1]}{
set result [expr $result * $x]
}
return $result
}
set res [Factorial $f]
puts "Factorial of $f is $res"
还有一个类似的SO question,但它似乎不直接解决我的问题。我有双重检查语法错误的代码,但它并没有通过tclsh
成功在Cygwin中编译产生的错误:
$ tclsh ext1-1.tcl
extra characters after close-brace
while executing
"if {$argc !=1}{
puts stderr "Error! ns called with wrong number of arguments! ($argc)"
exit 1
} else
set f [lindex $argv 0]
proc Factorial {x}{..."
(file "ext1-1.tcl" line 3)
TCL代码来自:NS模拟器对于初学者来说,索菲亚 - 安提波利斯,2003-2004
它应该是'proc Factorial {x} {'。只需在两者之间添加一个空格。 – Dinesh
与条件相同的东西:应该是'if {$ argc!= 1} {'。这是导致错误的原因。代码中出现同样的问题。将所有参数分开是至关重要的。 –