个人项目WordCount基础功能
码云地址:https://gitee.com/stedylan/WordCount
1.PSP表格:
PSP2.1 |
PSP阶段 |
预估耗时 (分钟) |
实际耗时 (分钟) |
Planning |
计划 |
10 |
10 |
· Estimate |
· 估计这个任务需要多少时间 |
600 |
590 |
Development |
开发 |
500 |
560 |
· Analysis |
· 需求分析 (包括学习新技术) |
60 |
80 |
· Design Spec |
· 生成设计文档 |
20 |
0 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
30 |
0 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
30 |
20 |
· Design |
· 具体设计 |
30 |
10 |
· Coding |
· 具体编码 |
300 |
380 |
· Code Review |
· 代码复审 |
20 |
10 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
110 |
160 |
Reporting |
报告 |
100 |
0 |
· Test Report |
· 测试报告 |
30 |
0 |
· Size Measurement |
· 计算工作量 |
35 |
0 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
35 |
30 |
|
合计 |
600 |
590 |
2.项目分析:
该项目的要求为WordCount的需求可以概括为:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件result.txt中。-c为统计字符数,-w为统计单词数,-l为统计行数,-o + 文件名为将结果输出到指定的文件中。并要求以命令行的形式对程序进行输入和输出。
解决思路:
在这次项目中,我选择用java完成。首先需要通过流读取文件,然后通过循环一行一行的读取字符串,并对每一行字符串进行统计。当读到文章末尾时,关闭文件。然后打印结果并写入到文件中。最后打包成exe文件并上传至码云。
3.实现过程:
在设计代码时,我将项目分为两个类。一是统计所用的方法类和客户端。
在Count类中有两个方法,本别实现了统计单词数和统计字符数。
代码说明:
在这段代码中,我实现了统计单词数量的功能。该方法中的参数为每一行的字符串。从第一个字符开始遍历,当遇到字母时,然后从当前位置进行二次遍历,知道当前字符不为字母,这时单词数+1。该方法返回统计数。
public static int Words(String str) { int count = 0; for (int i = 0; i < str.length(); i++) { if ((str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) {// 遇到字母时遍历直到出现不是字母的字符,此时单词数+1 if (i == str.length() - 1) break; else { int j = 0; for(j=i;j<str.length();j++) { if(!((str.charAt(j) >= 'a' && str.charAt(j) <= 'z') || (str.charAt(j) >= 'A' && str.charAt(j) <= 'Z'))) break; } i = j; count++; } } } return count; }
关于主函数,其中遇到的问题有如何通过命令行将参数传递,借鉴了该博客https://www.cnblogs.com/xy-hong/p/7197725.html。
在这段代码中,实现了可以输入多条命令来输出多项统计结果。这里我主要运用了集合中的方法,将数组当成集合,判断集合当中是否有这个元素,从而完成选择功能。
if (Arrays.asList(args).contains("-o")) { fw = new FileWriter(args[args.length - 1], true); if (Arrays.asList(args).contains("-c")) { fw.write(args[args.length - 3] + "," + "字符数:" + countChar+"\r\n"); System.out.println(args[args.length - 3] + "," + "字符数:" + countChar); } if (Arrays.asList(args).contains("-w")) { fw.write(args[args.length - 3] + "," + "单词数:" + countWord+"\r\n"); System.out.println(args[args.length - 3] + "," + "单词数:" + countWord); } if (Arrays.asList(args).contains("-l")) { fw.write(args[args.length - 3] + "," + "行数:" + countLine+"\r\n"); System.out.println(args[args.length - 3] + "," + "行数:" + countLine); } fw.flush(); } else { fw = new FileWriter("result.txt", true); if (Arrays.asList(args).contains("-c")) { fw.write(args[args.length - 1] + "," + "字符数:" + countChar+"\r\n"); System.out.println(args[args.length - 1] + "," + "字符数:" + countChar); } if (Arrays.asList(args).contains("-w")) { fw.write(args[args.length - 1] + "," + "单词数:" + countWord+"\r\n"); System.out.println(args[args.length - 1] + "," + "单词数:" + countWord); } if (Arrays.asList(args).contains("-l")) { fw.write(args[args.length - 1] + "," + "行数:" + countLine+"\r\n"); System.out.println(args[args.length - 1] + "," + "行数:" + countLine); } fw.flush(); }
其中还有遇到的问题是我读取文件时,每行首字母都会丢失。这个问题困扰了我很久。后来借鉴了该博客http://www.cnblogs.com/mybook/archive/2011/12/15/2289362.html,原因是bufferreader.read()方法会将首字母读出,这样用readline方法时便不会读取。按照博客里修改代码,最后解决了问题。
while ((s=br.readLine()) != null) { countChar += Count.Chars(s); countWord += Count.Words(s); countLine++; }
4.1测试代码:
在测试中,我主要的思路是通过java调用cmd控制台,并预设置多条输入指令,并在java控制台显示出来,这样以达到多次测试的功能。
具体代码(借鉴自https://blog.csdn.net/shenxiaomo1688/article/details/79196625):
通过executeCmd方法打开命令行,并执行相应指令。其中用了两个C源代码、一个java源代码和一个C#源代码作为测试用例。
public class TestClient { public static String executeCmd(String command) throws IOException { System.out.println("Execute command : " + command); Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec("cmd /c " + command); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK"))); String line = null; StringBuilder build = new StringBuilder(); while ((line = br.readLine()) != null) { System.out.println(line); build.append(line); } return build.toString(); } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub executeCmd("G:\\WordCount\\wc.exe -c test.cpp"); executeCmd("G:\\WordCount\\wc.exe -w sort.cpp"); executeCmd("G:\\WordCount\\wc.exe -l test.cpp"); executeCmd("G:\\WordCount\\wc.exe -c sort.cpp -o AutoTest1.txt"); executeCmd("G:\\WordCount\\wc.exe -c -w LoginDao.java -o AutoTest2.txt"); executeCmd("G:\\WordCount\\wc.exe -c -w -l sort.cpp -o AutoTest3.txt"); executeCmd("G:\\WordCount\\wc.exe -c -l test.cpp"); executeCmd("G:\\WordCount\\wc.exe -c -w Program.cs"); executeCmd("G:\\WordCount\\wc.exe -l -w test.cpp"); executeCmd("G:\\WordCount\\wc.exe -c -w -l LoginDao.java"); } }
4.2测试结果:
参考文献:
https://www.cnblogs.com/xy-hong/p/7197725.html
https://www.cnblogs.com/xy-hong/p/7197725.html
http://www.cnblogs.com/mybook/archive/2011/12/15/2289362.html
---恢复内容结束---