个人项目(python)

试试看

一、项目要求:

基本功能列表:

wc.exe -c file.c     //返回文件 file.c 的字符数(已完成)

wc.exe -w file.c    //返回文件 file.c 的词的数目  (已完成)

wc.exe -l file.c      //返回文件 file.c 的行数(已完成)

扩展功能:(已完成)
    -s   递归处理目录下符合条件的文件。
    -a   返回更复杂的数据(代码行 / 空行 / 注释行)。

空行:本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如“{”。

代码行:本行包括多于一个字符的代码。

注释行:本行不是代码行,并且本行包括注释。一个有趣的例子是有些程序员会在单字符后面加注释:

    } //注释
在这种情况下,这一行属于注释行。

[file_name]: 文件或目录名,可以处理一般通配符。

高级功能:(没有完成)

 -x 参数。这个参数单独使用。如果命令行有这个参数,则程序会显示图形界面,用户可以通过界面选取单个文件,程序就会显示文件的字符数、行数等全部统计信息。

需求举例:
  wc.exe -s -a *.c

 

二、PSP

PSPPersonal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning 计划 70 140
· Estimate 估计这个任务需要多少时间 30 40
Development 开发 150 180
Analysis 需求分析 (包括学习新技术) 50 120
·Design Spec 生成设计文档 20 40
·Design Review 设计复审 (和同事审核设计文档) 0 0
· Coding Standard 代码规范 (为目前的开发制定合适的规范) 20 20
·Design 具体设计 50 90
·Coding 具体编码 100 120
·Code Review 代码复审 50 70
·Test 测试(自我测试,修改代码,提交修改) 20 15
Reporting 报告 20 30
·Test Report 测试报告 20 20
·Size Measurement 计算工作量 40 60
·Postmortem & Process Improvement Plan 事后总结, 并提出过程改进计划 20 40
合计   640 985
       

 

三、代码实现:

源代码:

#-*- coding:utf-8 -*-
import os
import re
import sys
import glob

def count_char_number(input): #统计字符个数
content = ""
with open(input,'r') as rea:
c = rea.readlines()
for i in c:
i = i.strip()
if len(i) <= 0:
continue
content += i
return len(content)
def count_word_number(input): #统计单词字数
with open(input,'r') as rea:
content = rea.read()
content = content.strip()
c_r_word_list = [r'\o',r'\n',r'\r',r'\t',r'\v',r'\a',r'\b',r'\f',r'\ddd',r'\xhh']
for w in c_r_word_list:
content = content.replace(w,'')
c_p_word_list = [r'%d',r'%o',r'%x',r'%u',r'%c',r'%s',r'%f',r'%e',r'%g']
for w in c_p_word_list:
content = content.replace(w,'')


#只留非特殊字符
content = re.findall('[a-zA-Z]+', content, re.S)
# for c in content:
# print(33,c)
return len(content)


def count_line_number(input): #统计行数
n = 0
with open(input,'r') as rea:
content = rea.readlines()
return len(content)

return_file_paths = []
def get_files(file_path): #递归获取目录以及子目录
root_path = file_path
files = os.listdir(root_path)
for f in files:
_file_path = os.path.join(root_path,f)
if os.path.isdir(_file_path):
get_files(_file_path)
elif os.path.isfile(_file_path):
appendix = _file_path.split('.')[-1]
if appendix == 'c':
return_file_paths.append(_file_path)
return return_file_paths

def count_complex_number(input): #获取空行,注释,代码行
blank = 0
code = 0
note = 0
with open(input,'r') as rea:
content = rea.readlines()
is_note = False
for line in content:
line = line.replace(' ','').strip()


if line == "":
blank += 1
elif len(line) <= 1 and (line == '{' or line == '}'):
blank += 1
elif '//' in line :
note += 1
elif '/*' in line:
is_note = True
note+=1
elif '*/' in line:
is_note = False
note +=1
elif is_note is True:
note += 1
else:
code += 1

return blank,code,note

def main(): #主函数
_get_char_number = None
_get_word_number = None
_get_line_number = None
_get_complex_number= None

args = sys.argv
reverse = False
if len(args) == 3:
if '-c' in args:
_get_char_number = str(args[-1])
elif '-w' in args:
_get_word_number = str(args[-1])
elif '-l' in args:
_get_line_number = str(args[-1])
elif '-a' in args:
_get_complex_number = str(args[-1])

elif len(args) == 4:
if '-c' in args and '-s' in args:
_get_char_number = str(args[-1])
reverse = True
elif '-w' in args and '-s' in args:
_get_word_number = str(args[-1])
reverse = True

elif '-l' in args and '-s' in args:
_get_line_number = str(args[-1])
reverse = True

elif '-a' in args and '-s' in args:
_get_complex_number = str(args[-1])
reverse = True

file_path = os.getcwd()
if _get_char_number is not None:
if reverse is False:
res = count_char_number(_get_char_number)
print("文件%s字符数为:%s" % (_get_char_number,str(res)))
else:
files = get_files(file_path)
for f in files:
res = count_char_number(f)
print("文件%s字符数为:%s" % (f, str(res)))

if _get_word_number is not None:
if reverse is False:
res = count_word_number(_get_word_number)
print("文件%s 单词数为: %s " % (_get_word_number,str(res)))

else:
files = get_files(file_path)
for f in files:
res = count_word_number(f)
print("文件%s 单词数为: %s " % (f, str(res)))

if _get_line_number is not None:
if reverse is False:
res = count_line_number(_get_line_number)
print("文件%s 行数为: %s " %(_get_line_number,str(res)))

else:
files = get_files(file_path)
for f in files:
res = count_line_number(f)
print("文件%s 行数为: %s " % (f, str(res)))

if _get_complex_number is not None:
if reverse is False:
blank,code,note = count_complex_number(_get_complex_number)
print("文件 %s 空行数量 %s , 代码行数量: %s , 注释行数量: %s" % (_get_complex_number,str(blank), str(code), str(note)))

else:
files = get_files(file_path)
for f in files:
blank, code, note = count_complex_number(f)
print("文件 %s 空行数量 %s , 代码行数量: %s , 注释行数量: %s" % (f, str(blank), str(code), str(note)))


main()


test 1:
main()
{
int day,month,year,sum,leap;
printf("\nplease input year,month,day\n");
scanf("%d,%d,%d",&year,&month,&day);
switch(month) //zhushi
{
 case 1:sum=0;break;
 case 2:sum=31;break;
 case 3:sum=59;break;
 case 4:sum=90;break;
 case 5:sum=120;break;
 case 6:sum=151;break;
 case 7:sum=181;break;
 case 8:sum=212;break;
 case 9:sum=243;break;
 case 10:sum=273;break;
 case 11:sum=304;break;
 case 12:sum=334;break;
 default:printf("data error");break;
}
sum=sum+day;  
/*aaa AA Q W QWEQ% %Qs%qa
d
*/
 if(year%400==0||(year%4==0&&year%100!=0))
  leap=1;
 else
  leap=0;
if(leap==1&&month>2)
sum++;
printf("It is the %dth day.",sum);}

test 2:
main()
{
long int i;
int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;
scanf("%ld",&i);
bonus1=100000*0.1;bonus2=bonus1+100000*0.75;
bonus4=bonus2+200000*0.5;
bonus6=bonus4+200000*0.3;
bonus10=bonus6+400000*0.15;
 if(i<=100000)
  bonus=i*0.1;
 else if(i<=200000)
     bonus=bonus1+(i-100000)*0.075;
    else if(i<=400000)
        bonus=bonus2+(i-200000)*0.05;
       else if(i<=600000)
           bonus=bonus4+(i-400000)*0.03;
          else if(i<=1000000)
              bonus=bonus6+(i-600000)*0.015;
             else
              bonus=bonus10+(i-1000000)*0.01;
printf("bonus=%d",bonus);
}

test 3:
main()
{
int i,j,k;
printf("\n");
for(i=1;i<5;i++)    //222
 for(j=1;j<5;j++)
  for (k=1;k<5;k++)
   {
    if (i!=k&&i!=j&&j!=k) //444
    printf("%d,%d,%d\n",i,j,k);
   }
}

hello.c:

#include <stdio.h>
 
int main() {
 printf("Hello World !");
return 0;
}
//sss

四、结果截图





五、个人心得
因为个人对python比较感兴趣,也学了一点点的相关知识,所以这个项目想用Python来解决。在完成的过程中发现很多自己想要实现的功能所需要的知识自己还是处于初级阶段,一边打码一边
查询书本或者在网络上搜索自己想要的语法语句,实际花费的时间比自己计划中的时间要多得多,而且在测试过程中也很容易忽略一些特殊情况,经过反复调试才可以解决,而且高级功能没能
做出来。经过这次的项目,让我知道了打码是一个日记月累的过程,在学习过程中必须要细心,需要考虑的问题往往比自己想象的要多。打码路上,任重道远。

posted on 2020-03-28 01:22  0·2℃  阅读(321)  评论(0编辑  收藏  举报

导航