鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: ~风介~

[日问] #每日一问# 王の城堡

 关闭 [复制链接]
发表于 2017-5-13 15:05:31 | 显示全部楼层

回帖奖励 +2 鱼币

  1. import easygui as g
  2. import os

  3. def show_result(start_dir):
  4.     lines = 0
  5.     total = 0
  6.     text = ''

  7.     for i in source_list:
  8.         lines = source_list[i]
  9.         total += lines
  10.         text += "【%s】源文件%d个,源代码%d行\n" %( i,file_list[i],lines)

  11.     title = '统计结果'
  12.     msg = '您目前共累积编写了%d行代码,完成进度:%.2f %%\n离10w行代码还差%d行,请继续\\\
  13. 努力!' % (total, total/100000, 100000-total)
  14.     g.textbox(msg,title,text)

  15. def calc_code(file_name):
  16.     lines = 0
  17.     with open(file_name,encoding = 'utf-8') as f:
  18.         print('正在分析文件:%s.......'%file_name)
  19.         try:
  20.             for each_line in f:
  21.                 lines += 1
  22.         except UnicodeDecodeError:
  23.             pass #不可避免会遇到格式不兼容的文件,这里忽略。。。
  24.     return lines

  25. def search_file(start_dir):
  26.     os.chdir(start_dir) #改变工作目录

  27.     for each_file in os.listdir(os.curdir):#列举当前工作目录文件名
  28.         ext = os.path.splitext(each_file)[1]#得文件后缀名
  29.         if ext in target:
  30.             lines = calc_code(each_file) #调用计算行数文件

  31.             #统计文件数
  32.             try:
  33.                 file_list[ext] += 1
  34.             except KeyError:   #如果字典中不存在则抛出异常,添加字典减
  35.                 file_list[ext] = 1

  36.             #统计源代码行数
  37.             try:
  38.                 source_list[ext] += lines
  39.             except KeyError:
  40.                 source_list[ext] = lines


  41.         if os.path.isdir(each_file):
  42.             search_file(each_file) #递归调用
  43.             os.chdir(os.pardir)#递归调用后切记返回上一层目录


  44. target = ['.c',',cpp','.py','.cc','.java','.pas','.asm'] #源代码类型
  45. file_list = {}
  46. source_list = {}

  47. g.msgbox('请打开您存放所有代码的文件夹.......','统计代码量') #msgbox(msg,title)
  48. path = g.diropenbox('请选择您的代码库:')# 提供一个对话框,选择打开文件夹

  49. search_file(path)
  50. show_result(path)
复制代码

虽然程序是甲鱼大大写的,但是统计已经编写的代码行数很有成就感呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-14 02:36:49 | 显示全部楼层

回帖奖励 +2 鱼币

常用的就是冒泡排序,好久没有编程了也。一时想不起来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-14 12:40:23 | 显示全部楼层
getchar()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-14 20:25:07 | 显示全部楼层

回帖奖励 +2 鱼币

各种hello world!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 18:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表