每天一个tcl脚本练习-第4天

题目:

统计代码行数,编写一个脚本,读取代码文件,统计其中的代码行数(即不包括注释和空行),并输出总行数和代码行数。**

set in_f "./in.f"
set blank_lines 0
set comment_lines 0
set code_lines 0

set fin [open $in_f r]

while {[gets $fin line] != -1 } {
     set line [string trim $line]
     if{$line == " "} {
        incr blank_lines  ##空行计数
     } elseif {[string match "/*" $line] } {
          set comment_lines [expr {$comment_lines +1}]
          while {[gets $fin line] != 1} {
          set comment_lines [expr{$comment_lines +1}] ##多行注释计数
          if {[string match "*/" $line]} {
             break
          }
      }
    } elseif {[string match "//*" $line]} {
         incr comment_lines   ##单行注释计数
    } else {
         incr code_lines
    }
}

set total_lines [expr $balnk_lines + $comment_lines + $code_lines]
puts "total lines: $total_lines "
puts "code_lines: $code_lines "

close $fin

多行注释很容易忘掉,使用了字符串命令string match和数值计算命令expr

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值