统计9645_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > 统计9645

统计9645

 2010/12/26 9:47:23  fumes  http://fumes.javaeye.com  我要评论(0)
  • 摘要:require'Find'defremarkstr,typeifstr.strip[0]==typereturn1endstr1=strstring=""whileindex=str.index(type)#<=>0yfufuyfystring<<str[0..index-1]num=string.count("\'")%2+string.count("\"")%2+(string.count("\[")-string.count("\]")).abs+(string
  • 标签:
require 'Find'
def remark str,type
  if str.strip[0] == type
    return 1
  end
  str1 = str
  string = ""
  while index = str.index(type)#<=>0yfufuyfy
    string << str[0..index-1]
   
    num = string.count("\'")%2+string.count("\"")%2+(string.count("\[")-string.count("\]")).abs+(string.count("\{")-string.count("\}")).abs+(string.count("\(")-string.count("\)")).abs
    if num == 0
      return 2
    end
    str = str[index+1..-1]
  end
  return 0
end
def place i,record,singnum,blankline,tow,benum,ennum,type,tybegin,tyend,mark = 0
  #puts mark
  #puts "hello"
  if mark == 0
    #puts "beign"
    if i.chomp == tybegin
      benum << record
    elsif i.chomp == tyend
      ennum << record
    elsif remark(i,type) == 1
      singnum << record
    elsif remark(i,type) == 2
      singnum << record
      $two += 1
    elsif i.chomp == ""
      blankline << record
    end
  end
  if mark != 0
    #puts "end"
    if remark(i,tybegin)== 1
      benum << record
    elsif remark(i,tybegin)== 2
      benum << record
      $two += 1
    elsif  i.index(tyend) == i.length - tyend.length - 1
      ennum << record     
    elsif i.include? tyend and i.index(tyend) != i.length - tyend.length - 1
      puts "inde"
      ennum << record
      $two += 1
    elsif remark(i,type) == 1
      singnum << record
    elsif remark(i,type) == 2
      singnum << record
      $two += 1
    elsif i.chomp == ""
      blankline << record
    end
  end 
end

def type str
  string = File.basename(str).reverse
  if index = string.index('.')
    type = string[0,index].reverse
  end
  type
end

def snum benum,ennum,record #bjkbiugkjbvfvuy
  num = []
  benum.sort!
  ennum.sort!
  belength = benum.length
  enlength = ennum.length
  i = 0
  j = 0
  if belength > 0
    if enlength == 0
      num << benum[0] << record
    end
    while j < enlength
      if benum[0] < ennum[j]###viuvui
        num << benum[i] << ennum[j]
        i +=1
        j += 1
        puts num
        puts i,j
        break
      else j += 1        
      end
    end
    while i <belength and j < enlength
      if benum[i] < ennum[j] and benum[i] > ennum[j-1]
        num << benum[i] << ennum[j]
        i += 1
        j += 1
      else j += 1
      end
    end
    if i < j 
      num << benum[i] << record
    end
    return num
  end
  return num
end
def countannotations record,singnum,benum,ennum,blankline
  arrnum = snum(benum,ennum,record)
  print singnum.length
  annotations = 0
  annotations  += singnum.length
  if arrnum.length > 0
    arrnum.length.times{|i|
      if i%2 == 1
      annotations = annotations+arrnum[i].to_i-arrnum[i-1].to_i+1 
    end
    }
   puts "annotations :#{annotations}"
    i = 0
    arrnum.sort!
    for i in (0..arrnum.length-1)
    if i%2 == 1
        if singnum.length > 0
         
          for j in (0..singnum.length-1)
            puts "times#{singnum[j]}"
            puts "times#{arrnum[i-1]}"
            if singnum[j] < arrnum[i] and singnum[j] > arrnum[i-1]
              puts "gengoiwengownego",arrnum[i-1]
              annotations -= 1
            end
          end
        end
        if blankline.length > 0
          blankline.length.times{|j|
            if blankline[j]< arrnum[i] and blankline[j]> arrnum[i-1]
              annotations -= 1
            end
          }
        end
      end
      i += 1
    end
  end
  print "the all code:#{record}\t"
  print "the fact code:#{record-annotations-blankline.length+$two}\t"
  print "the blank line:#{blankline.length}\t"
  print "the annotations:#{annotations}\t"
  $codeall += record-annotations-blankline.length+$two
  $blankal += blankline.length
  $annall  += annotations
  $all     += record
end
dir = gets.chomp
patharr = []
$codeall = 0
$blankal = 0
$all = 0
$annall = 0
Find.find('d:/') do |path|
  if ["rb","c","cpp","cpp","java"].include? type(path)
   
    file = File.read(path)
    benum = []
    ennum = []
    singnum = []
    blankline = []
    record = 0
    $two = 0
    sing = ""
    tybegin = ""
    tyend = ""
    if ["c","cpp","cpp","java"].include? type(path)
      sing = '//'
      tybegin = '/*'
      tyend = '*/'
      mark = 1
    else
      sing = "#"
      tybegin = '=begin'
      tyend = '=end'
      mark = 0     
    end
   
    file.each_line{|i|
      record += 1
      place i,record,singnum,blankline,$tow,benum,ennum,sing,tybegin,tyend,mark#,"begin"
=begin
      if i.chomp == "=begin"
        benum << record
      elsif i.chomp == "=end"
        ennum << record
      elsif remark(i) == 1
        singnum << record
      elsif remark(i) == 2
        singnum << record
        $two += 1
      elsif i.chomp == ""
        blankline << record
      end
=end
    }
   print type(path),"\t"
    countannotations record,singnum,benum,ennum,blankline
    puts path
  end
end
puts "the all factcode   :#{$codeall}"
      puts "the all blankal    :#{$blankal} "
      puts "the all annotations:#{$annall}"
      puts "the all code       :#{$all}"
  • 相关文章
发表评论
用户名: 匿名