Binary 顺序二进制TCL
请对我的计划提出改进建议。该程序提供4位二进制增量o/p 我正在寻找优化这个地方有不必要的代码Binary 顺序二进制TCL,binary,tcl,Binary,Tcl,请对我的计划提出改进建议。该程序提供4位二进制增量o/p 我正在寻找优化这个地方有不必要的代码 #!/bin/sh # This puts "+++++++++++++++++\n" set ipaddr "0.0.0.0" set limit 4 set splitip [split $ipaddr "."] puts
#!/bin/sh
# This
puts "+++++++++++++++++\n"
set ipaddr "0.0.0.0"
set limit 4
set splitip [split $ipaddr "."]
puts "Split ip address = $splitip"
# MAIN ROUTINE
set ilength [llength $splitip]
puts "Length of string is $ilength"
set first [lindex $splitip 0]
set sec [lindex $splitip 1]
set third [lindex $splitip 2]
set four [lindex $splitip 3]
for { set limit 1} { $limit >0} {} {
for { set first $first } { $first <= $limit} {} {
for { set sec $sec } { $sec <= $limit} {} {
for { set third $third } { $third <= $limit} {} {
for { set four $four } { $four <= $limit} {} {
puts " f:$first $sec $third $four"
incr four
}
set four 0
incr third; #puts " t:$four $third $sec $first\n"
}
set third 0
incr sec
}
#puts " f:$four $third $sec $first"
set sec 0
incr first
}
incr limit -1
}
# End Main
puts "\n++++++End Program+++++++++++"
请对我的计划提出改进建议。该程序提供4位二进制增量o/p
我正在寻找优化这个地方有不必要的代码。
请对我的计划提出改进建议。该程序提供4位二进制增量o/p
我正在寻找优化这个地方有不必要的代码
#!/bin/sh
# This
puts "+++++++++++++++++\n"
set ipaddr "0.0.0.0"
set limit 4
set splitip [split $ipaddr "."]
puts "Split ip address = $splitip"
# MAIN ROUTINE
set ilength [llength $splitip]
puts "Length of string is $ilength"
set first [lindex $splitip 0]
set sec [lindex $splitip 1]
set third [lindex $splitip 2]
set four [lindex $splitip 3]
for { set limit 1} { $limit >0} {} {
for { set first $first } { $first <= $limit} {} {
for { set sec $sec } { $sec <= $limit} {} {
for { set third $third } { $third <= $limit} {} {
for { set four $four } { $four <= $limit} {} {
puts " f:$first $sec $third $four"
incr four
}
set four 0
incr third; #puts " t:$four $third $sec $first\n"
}
set third 0
incr sec
}
#puts " f:$four $third $sec $first"
set sec 0
incr first
}
incr limit -1
}
# End Main
puts "\n++++++End Program+++++++++++"
#/垃圾箱/垃圾箱
#这个
放入“+++++++++++++++++++++++++++++\n”
将ipaddr设置为“0.0.0.0”
设置限制4
设置splitip[split$ipaddr.”
放置“拆分ip地址=$splitip”
#主要程序
设置ilength[llength$splitip]
放置“字符串长度为$ILENGHT”
先设置[lindex$splitip 0]
设置秒[lindex$splitip 1]
第三组[lindex$splitip 2]
第四组[lindex$splitip 3]
对于{set limit 1}{$limit>0}{}{
对于{SETFIRST$first}{$first您的程序本质上就是这样,这是否符合您的意图
for { set first 0 } { $first <= 1} {incr first} {
for { set sec 0 } { $sec <= 1} {incr sec} {
for { set third 0 } { $third <= 1} {incr third} {
for { set four 0 } { $four <= 1} {incr four} {
puts " f:$first $sec $third $four"
}
}
}
}
Tcl 8.5中添加了lassign
。如果您使用的是较旧版本的Tcl,请改用foreach
赋值:
foreach {first sec third four} $splitip break
构造
for { set limit 1} { $limit >0} {incr limit -1} { ... }
简单的意思是“执行…恰好一次”:它不会以任何方式影响程序的执行。即使删除它(将代码保留在body参数中),它内部的代码仍将恰好执行一次
为清楚起见,incr x
调用应该位于For
的第三个参数内,而不是第四个主体参数内
最后请注意,如果您的目的是打印出一系列二进制数字,那么这样做要容易得多:
for {set i 0} {$i < 16} {incr i} { puts [format %04b $i] }
对于{set i 0}{$i<16}{incr i}{put[格式%04b$i]}
是的!我想是的。只需要在每次循环后重置为0(?)但是你是对的。哇!谢谢你的建议!!这会让我的编程变得更好。对不起,新用户。我不知道这个选项。谢谢你给我打印bin no的提示,但是我的目的是使用循环,这样我会做得更好:)我怀疑这一点,但不提另一种方式是不对的。如果你真的处理IP地址,妈妈你也可以看看tcllib ip包。()谢谢schlenk的链接。