wordcount(代码片段)

wpx979 wpx979     2023-01-02     697

关键词:

Word Count

一.个人Gitee地址:https://gitee.com/godcoder979/(该项目完整代码在这里)

 

二.项目简介:

该项目是一个统计文件字符、单词、行数等数目的应用程序,通过输入命令来执行你想要的操作。所用语言:java

命令格式:

wc.exe [para] <filename> [para] <filename> ... -o <filename>

功能:

-a:统计文件中的代码行、空行、注释行;

-c:统计文件中的字符数,不包括换行符;

-w:统计文件中的单词数;

-l:统计文件的行数;

-s:统计一个文件夹中符合要求的文件;

-o:指定输出文件;

三.PSP表格:

PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)
Planning 计划 20 10
· Estimate · 估计这个任务需要多少时间 5 5
Development 开发 500 600
· Analysis · 需求分析(包括学习新技术) 20 30
· Design Spec · 生成设计文档 30 30
· Design Review · 设计复审(和同事审核设计文档) 10 15
· Coding Standard · 代码规范(为目前的开发制定合适的规范) 5 5
· Design · 具体设计 15 20
· Coding · 具体编码 200 400
· Code Review · 代码复审 40 30
· Test · 测试(自我测试,修改代码,提交修改) 20 30
Reporting 报告 60 50
· Test Report · 测试报告 20 15
· Size Measurement · 计算工作量 10 5
· Postmortem & Process improvement Plan · 事后总结,并提出过程改进计划 30 30
  合计 580 660

四.解题思路:

整个项目比较简单,用户输入需要进行的操作和基本的文件信息就可以保存结果到一个文件里。所以可以这样分析:

1)识别用户输入的选项和文件名,分析需要进行的操作

2)实现每一种功能,根据分析执行相应的操作

3)将结果保存到一个文件里

代码模块划分:

1)主函数:

通过用户输入的指令进行相应的逻辑处理

2)功能调用模块:

这个模块被主函数调用,根据分析执行相应操作

3)判断模块:

根据用户输入的指令得出需要执行那些功能,这是功能调用模块执行的依据

4)功能实现模块:

这个模块就是实现以上统计字符、单词等功能了

五.关键代码展示:

 

1.judge方法(分析用户要实现的功能,当flag为true是代表将要实现)

 1 private static void judge(String str) 
 2         // TODO Auto-generated method stub
 3         if(str.equals("-s")&&s_flag==false)
 4             s_flag=true;
 5         
 6         else if(str.equals("-*")&&star_flag==false)
 7             star_flag=true;
 8             c_flag=true;
 9             w_flag=true;
10             l_flag=true;
11             a_flag=true;
12         
13         else if(str.equals("-a")&&a_flag==false)
14             a_flag=true;
15         
16         else if(str.equals("-c")&&c_flag==false)
17             c_flag=true;
18         
19         else if(str.equals("-w")&&w_flag==false)
20             w_flag=true;
21         
22         else if(str.equals("-l")&&l_flag==false)
23             l_flag=true;
24         
25         else if(file_flag==false)//还没有指定输入文件
26         
27             if(s_flag==false)//如果输入的不是路径
28                 file_name=str;
29                 txt_name=str;
30             
31             file_flag=true;
32         
33         else if(str.equals("-o")&&o_flag==false)
34             o_flag=true;
35         
36         else if(o_flag==true&&file_flag==true)//已经有输入文件且o_flag为真但是没有指定输出文件
37             output_name+=str;
38             all_flag=true;
39         
40         else
41             System.out.println("指令判断完成");
42         
43 
44     

 

2.字符统计:

 1 private static int num_of_char(String filename) 
 2         // TODO Auto-generated method stub
 3         System.out.println(filename);
 4 
 5         File file=new File(filename);
 6         Reader readfile=null;
 7         int c_num=0;
 8         try
 9             readfile = new InputStreamReader(new FileInputStream(file));
10             int tempchar;
11             while ((tempchar=readfile.read()) != -1) 
12                 if((char)tempchar!=‘
‘&&(char)tempchar!=‘
‘)
13                     c_num++;
14                 
15             
16             readfile.close();
17         
18         catch(Exception e)
19             e.printStackTrace();
20             System.out.println("指定输入文件不存在");
21         
22         return c_num;
23     

 

3.单词统计:(这里面我用IsLetter来判断字母的大小写)

 1 private static int num_of_word(String filename)
 2         File file=new File(filename);
 3         Reader readfile=null;
 4         boolean letter_flag=false;
 5         int w_num=0;
 6         try
 7             readfile = new InputStreamReader(new FileInputStream(file));
 8             int tempchar;
 9             while ((tempchar=readfile.read()) != -1) 
10                 if(IsLetter((char)tempchar))
11                     letter_flag=true;
12                 
13                 else if(letter_flag==true)
14                     letter_flag=false;
15                     w_num++;
16                 
17             
18             readfile.close();
19         
20         catch(Exception e)
21             System.out.println("指定输入文件不存在");
22         
23         return w_num;
24     

4.行数统计:(遇到换行符就加一。Windows中的换行符为“ ”,这里只使用" "。)

 1 private static int num_of_line(String filename) 
 2         // TODO Auto-generated method stub
 3         File file=new File(filename);
 4         Reader readfile=null;
 5         int l_num=1;
 6         try
 7             readfile = new InputStreamReader(new FileInputStream(file));
 8             int tempchar;
 9             while ((tempchar=readfile.read()) != -1) 
10                 if((char)tempchar==‘
‘)
11                     l_num++;
12                 
13             
14             readfile.close();
15         
16         catch(Exception e)
17             System.out.println("指定输入文件不存在");
18         
19         return l_num;
20     

5.空行、代码行、注释行统计:(按行读文件,去除每一行的换行符和空格,获得的string长度为0就代表空行;一行中如果只有“/*”、“//”、“//”、“/*”代表注释行(这些就是注释的开头),直到统计到注释的末尾;其余的均为代码行。)

 1 private static int [] code_ana(String filename)
 2         File file=new File(filename);
 3         int nothing=0;
 4         int line=0;
 5         int note=0;
 6         int code_line=0;
 7         boolean note_flag=false;
 8         BufferedReader readfile = null;
 9         try
10             readfile = new BufferedReader(new FileReader(file));
11             String tempString = null;
12             while ((tempString = readfile.readLine()) != null) 
13                 line++;
14                 tempString=tempString.replaceAll("
"," ");//去掉所有换行符和空格
15                 if(note_flag==true)
16                     note++;
17                     if(tempString.endsWith("*/"))
18                         note_flag=false;//代表注释内容在本行结束
19                     
20                 
21                 if(tempString.equals(" ")||tempString.equals("")||tempString.equals(""))
22                     nothing++;
23                 
24                 if(tempString.startsWith("//")||tempString.startsWith("//"))
25                     note++;
26                 
27                 if(tempString.startsWith("/*")||tempString.startsWith("/*"))
28                     if(tempString.endsWith("*/"))
29                         note++;
30                     
31                     else
32                         note++;
33                         note_flag=true;//代表注释的内容在本行还没结束
34                     
35                 
36                 code_line=line-note-nothing;
37             
38             readfile.close();
39         
40         catch(Exception e)
41             System.out.println("指定输入文件不存在");
42         
43         int []num =new int[3];
44         num[0]=code_line;
45         num[1]=nothing;
46         num[2]=note;
47         return num;
48     

7.功能调用模块(这里通过judge得到的功能需求,然后调用功能模块执行相应的操作):

 1 private static void work() 
 2         // TODO Auto-generated method stub
 3         if(o_flag==true)
 4             if(all_flag==false)
 5                 System.out.println("命令格式不正确:有输出指令但是没有指定输出文件");
 6         
 7 
 8         if(a_flag==true)
 9             String Command="代码行/空行/注释行";
10             int []num=code_ana(file_name);
11             System.out.print(txt_name+","+Command+":"+num[0]+"/"+num[1]+"/"+num[2]+"
");
12         
13 
14         if(c_flag==true)
15             String Command="字符数";
16             System.out.print(txt_name+","+Command+":"+num_of_char(file_name)+"
");
17         
18 
19         if(l_flag==true)
20             String Command="行数";
21             System.out.print(txt_name+","+Command+":"+num_of_line(file_name)+"
");
22         
23 
24         if(all_flag==true)
25             if(a_flag==true)
26                 String Command="代码行/空行/注释行";
27                 code_output(output_name,Command,code_ana(file_name));
28             
29             if(c_flag==true)
30                 String Command="字符数";
31                 output(output_name,Command,num_of_char(file_name));
32             
33             if(w_flag==true)
34                 String Command="单词数";
35                 output(output_name,Command,num_of_word(file_name));
36             
37             if(l_flag==true)
38                 String Command="行数";
39                 output(output_name,Command,num_of_line(file_name));
40             
41             System.out.println("运行并且输出内容保存成功");
42         
43         else 
44             System.out.println("本指令没有指定输出文件,所以输出内容没有保存");
45         
46     

 8.读取文件夹(当我们输入的是一个绝对路径时,在这里向下递归查找符合要求的文件):

 1 private static void files_output(String path_name)
 2         String []path=path_name.split("\\*");
 3         File file=new File(path[0]);
 4         if(file.exists())
 5             File[]files=file.listFiles();
 6             if(files!=null)
 7                 for(File f:files)
 8                     if(f.getName().endsWith(path[1]))
 9                         txt_name=f.getName();
10                         file_name=f.getAbsolutePath();
11                         work();
12                     
13                 
14             
15             else
16                 System.out.println("文件夹内容为空");
17             
18         
19         else
20             System.out.println(path[0]+":指定路径或文件不存在");
21         
22 
23     

 

六.测试用例:

这里我们用的是idea:直接通过传入参数的不同来测试每一种功能以及他们的组合:

下面是我用的输入文件:

技术分享图片

 

1)-c input.txt -o output.txt:

技术分享图片

2)-w input.txt -o output.txt:

 技术分享图片

3)-l input.txt -o output.txt:

 技术分享图片

 

4)-a input.txt -o output.txt:

技术分享图片

5)-a -w -l -c input.txt -o output.txt:

技术分享图片

6) -s -w -l  D:/test/*.txt -o output.txt:这里我们是在一个文件夹里向下递归查找符合要求的txt文件:

技术分享图片

七.参考文献

《构建之法--现代软件工程》 --邹欣 [第三版]

PS:(本次项目还有一些功能没有实现)未完待续......

 

wordcount(代码片段)

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;namespaceWordCountclassProgramstaticvoidMain(string[]args)Typetype=newType() 查看详情

wordcount(代码片段)

1.Github项目地址https://github.com/chaindreamjet/WordCount2.PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 20 30·Estimate·估计这个任务需要多少时间 20 30Development开发 700 940·Analysis·需求分析(包括 查看详情

mapreduce编写wordcount程序代码实现(代码片段)

MapReduce经典案例代码(wordcount)以经典的wordcount为例,通过自定义的mapper和reducer来实现单词计数packagecom.fwmagic.mapreduce;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.io. 查看详情

分享知识-快乐自己:运行(wordcount)案例(代码片段)

运行wordcount案例:一):大数据(hadoop)初始化环境搭建二):大数据(hadoop)环境搭建三):运行wordcount案例四):揭秘HDFS五):揭秘MapReduce六):揭秘HBase七):HBase编程-----------------------------------------------------------------Hadoo... 查看详情

wordcount(代码片段)

一.码云地址https://i.cnblogs.com/EditPosts.aspx?opt=1https://gitee.com/lttzy/wordcount二.PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 30 20·Estimate·估计这个任务需要多少时间 20 10Development开发 45 查看详情

wordcount(代码片段)

一.github:https://github.com/JiejieCM/WordCount二.pspPSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 30 30·Estimate·估计这个任务需要多少时间3030Development开发 3天 5天·Analysis·需求分析(包括学习新技术) 2天&nbs... 查看详情

wordcount(代码片段)

WordCount一.个人Gitee地址:https://gitee.com/godcoder979/(该项目完整代码在这里) 二.项目简介:该项目是一个统计文件字符、单词、行数等数目的应用程序,通过输入命令来执行你想要的操作。所用语言:java命令格式:wc.exe[para]&... 查看详情

wordcount(代码片段)

一、Gitee项目地址https://gitee.com/YSS_MYGit/WordCount二、基本思路 我看到项目后,首先想到的是使用C#语言编写,因为我对C#语言比较熟悉些。此项目有两个类,一个是Program类,该类是程序的入口,主要用于传参,另一个是WC类,... 查看详情

wordcount(代码片段)

一.Gitee地址:https://gitee.com/zjgss99/WordCount 二.项目分析:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。命令格式:wc.exe[para] ... 查看详情

个人项目wordcount(代码片段)

1.项目GitHub地址:https://github.com/FPXBao/wordcount2.解题思路:分析程序基本需求,将其功能分为三个函数调用:主函数intmain();功能函数Ccount();Wcount();Lcount();并进行相关知识学习。3.代码说明:主函数:#include<stdio.h>#include<stdlib.... 查看详情

wordcount(代码片段)

   码云地址:https://gitee.com/qaqxx88/wc1WordCount需求说明 (1)对源文件进行字符数,行数,单词数的统计并且放在与wc.exe相同目录下的result.txt中  (2)基本功能  wc.exe -c file.c   &nbs 查看详情

wordcount基础功能(代码片段)

Gitte地址:https://gitee.com/gyuyue/WordCount PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 15 20·Estimate·估计这个任务需要多少时间 30 30Development开发 60 80·Analysis·需求分析(包括学习新技术)... 查看详情

wordcount代码实现及测试(代码片段)

...者:201631062515201631062415码云地址:https://gitee.com/heshuxiang/WordCount/tree/master2.项目需求  对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多... 查看详情

wordcount基本功能(代码片段)

项目的gitee地址:https://gitee.com/ITtoto/WordCount 开发语言:C语言PSP2.1表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划20 16·Estimate·估计这个任务需要多少时间 30 27Development开发 120 160·Analysis·需... 查看详情

wordcount(代码片段)

   Gitee地址:https://gitee.com/dyh1621838953/WordCount1此项目我只实现了基本功能,即字符、行数、单词数的计算以及将结果输出到文件中。该项目用C#语言进行实现。解题思路:   根据题目要求所知,需要读取一... 查看详情

[mapreduce_1]运行wordcount示例程序(代码片段)

 0.说明  MapReduce实现WordCount示意图&& WordCount代码编写    1. MapReduce实现WordCount示意图     1.Map:预处理阶段,将原始数据映射成每个K-V,发送给reduce  2.Shuffle:混洗(分类),将相同的Key... 查看详情

wordcount(代码片段)

gitee地址:https://gitee.com/yzyindex/wordcount需求分析通过程序设计,编写一个可执行文件exe能够对源程序文件进行统计字符数、单词数、行数,统计结果可以以指定的格式输出到默认文件中。可执行程序命名为:wc.exe,该程序处理用... 查看详情

wordcount-软测作业(代码片段)

1.github地址 https://github.com/zgwe/wordCount2.PSP2.1PSP阶段预估耗时实际耗时(分钟)(分钟)Planning计划 10 10·Estimate·估计这个任务需要多少时间 20 10Development开发 40 60·Analysis·需求分析(包括学习新技术)  查看详情