1,Github网页链接:https://github.com/kekeing/Wordcount
注:采用了刘弋的代码
2,PSP表格如下:
PSP2.1 | PSP阶段 | 预估耗时 | 实际耗时 |
(分钟) | (分钟) | ||
Planning | 计划 | 10 | 20 |
· Estimate | · 估计这个任务需要多少时间 | 20 | 30 |
Development | 开发 | 40 | 80 |
· Analysis | · 需求分析 (包括学习新技术) | 60 | 110 |
· Design Spec | · 生成设计文档 | 60 | 100 |
· Design Review | · 设计复审 (和同事审核设计文档) | 40 | 80 |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 60 | 40 |
· Design | · 具体设计 | 300 | 360 |
· Coding | · 具体编码 | 360 | 440 |
· Code Review | · 代码复审 | 50 | 80 |
· Test | · 测试(自我测试,修改代码,提交修改) | 100 | 120 |
Reporting | 报告 | 70 | 80 |
· Test Report | · 测试报告 | 80 | 120 |
· Size Measurement | · 计算工作量 | 20 | 20 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 50 | 50 |
| 合计 | 1320 | 1730 |
3,解题思路:
刚看到题目的时候,觉得这个作业应该很简单,以前做过相类似的计算单词的程序,但是阅读了需求之后,发现这个题目有很多不同的地方,而且很多需求的描述都是不清楚
这个题目还有要求用java编写,而我对与java里面的文件操作等方面的东西不太熟悉,不过我想到上学期做的一个编译器比较类似,方法也相似,然后我在网上查了很多资料,也请
教了很多同学,做后还是能搞出来的。
4,程序实现的过程:
本程序只用了一个主类,然后一共有7个函数,分别是boolean类型的函数 isSpace(),isComma(),isEnter(),判断当前处理的字符是否为空格/逗号/回车等等,Read()函数即对于所选文件进
行读取操作,并且将文件中的所有字符存在数组中。GetChar()函数,对于当前处理的字符进行分析,并对于注释行,空行与代码行进行分析。Process()对于源代码的字符数组进行处理
,包括对于空格逗号回车的处理逻辑。最后就是程序的主函数main(String[] args)从命令行中中读入指令并进行相应的处理。并对前面所提到的六个函数进行调用。
5,代码说明:
public static void fileOut(String filename,String outfile,boolean w,boolean c,boolean l,boolean a) //写文件函数,将程序分析结果接入到指定文件。 { try{ String filepath = System.getProperty("user.dir"); filepath = filepath+'\\'; filename = filename.replace(filepath,""); if(w) { message = message +filename+","; message =message+ "单词数是"; if(e) { message = message +wordCount2; } else { message = message +wordCount; } message = message +"\r\n"; } if(c) { message = message +filename+","; message =message+ "字符数是"; message = message +charCount; message = message +"\r\n"; } if(l) { message = message +filename+","; message =message+ "行数是"; message = message +lineCount; message = message +"\r\n"; } if(a) { message = message +filename+","; message = message + "代码行/空行/注释行:"; message = message +codeLine +'/'+empLine+'/'+exLine; message = message +"\r\n"; } //System.out.println(outfile); File file = new File(outfile); FileWriter out = new FileWriter(outfile); //将需要输出的信息在需要输出的文件中进行输出 charMessage = message.toCharArray(); out.write(charMessage); out.close(); } catch (IOException e){ } }
public static void Process() //处理函数 { String temp = ""; while(!(isComma()||isSpace()||isEnter())) { if(Char!=' ') temp = temp + Char; GetChar(); if(Char!=' ') temp = temp + Char; if(Char=='\0') break; } wordArray[wordCount] = temp; wordCount++; while(isComma()||isSpace()||isEnter()) { GetChar(); if(Char=='\0') break; } } public static void Count() { do { Process(); }while(Char!='\0'); charCount--; charCount = charCount-lineCount; String temp = ""; String temp2 = ""; if(e) { for(int m = 0;m<wordCount;m++) { for(int n = 0;n<num;n++) { temp = stopArray[n]; temp2 = wordArray[m]; if(temp.equals(temp2)) { sp=true; } } if(!sp) { wordCount2++; } sp=false; } } } };
public static void main(String[] args) //程序从控制台接收数据 { String filename = ""; //文件名 String keyword = ""; String filepath = ""; //文件名 String path = ""; //文件名 String outfile = "result.txt"; //输出信息文件名 int i = 0; int num = 0; int length = args.length; //获取指令的个数 String stoplist = ""; while(i<length) { if(args[i].equals("-a")) { a = true; i++; } else if(args[i].equals("-o")) { o = true; i++; outfile =args[i]; if((i+1) == args.length) { break; } i++; } else if(args[i].equals("-c")) { c = true; i++; } else if(args[i].equals("-w")) { w = true; i++; } else if(args[i].equals("-l")) { l = true; i++; } else if(args[i].equals("-s")) { s = true; i++; } else if(args[i].equals("-e")) { e = true; i++; stoplist=args[i]; if((i+1) == args.length) { break; } i++; } else if(args[i].charAt(0)!='-') { filepath = System.getProperty("user.dir"); if(s) { path = args[i]; if(path.contains(".c")) keyword = ".c"; else if(path.contains(".txt")) keyword = ".txt"; } filename = args[i]; i++; } } Reader(filename,text); for(int h=0;h<200;h++) { stopArray[h] = ""; } if(s) //对指令路径下的文件进行遍历输出操作 { ArrayList<String>sourceNameArray = new ArrayList<String>(); getAllFile(sourceNameArray,filepath,keyword,s); for(int n= 0;n<sourceNameArray.size();n++) { Count(); fileOut(sourceNameArray.get(n),outfile,w,c,l,a); } } if(e) { Reader(stoplist,stop); int j = 0; while(j<stop.length) { SChar = stop[j]; while(SChar==' '||SChar==','||SChar=='\r') { j++; SChar = stop[j]; change = true; } if(change) { num++; change = false; } stopArray[num] = stopArray[num]+stop[j]; j++; } } do { Process(); }while(Char!='\0'); charCount--; charCount = charCount-2*lineCount; if(w) { message =message+ "单词数是"; if(e) { message = message +wordCount2; } else { message = message +wordCount; } message = message +"\r\n"; } if(c) { message =message+ "字符数是"; message = message +charCount; message = message +"\r\n"; } if(l) { Reader(stoplist,stop); int j = 0; while(j<stop.length) { SChar = stop[j]; while(SChar==' '||SChar==','||SChar=='\r') { j++; SChar = stop[j]; change = true; } if(change) { num++; //停用词数加一 change = false; } stopArray[num] = stopArray[num]+stop[j]; j++; } } do { Process(); }while(Char!='\0'); charCount--; charCount = charCount-lineCount; String temp = ""; String temp2 = ""; if(e) { for(int m = 0;m<wordCount;m++) { for(int n = 0;n<num;n++) { temp = stopArray[n]; temp2 = wordArray[m]; if(temp.contains(temp2)) { sp = true; } } if(!sp) { wordCount2++; } sp=false; } } fileOut(filename,outfile,w,c,l,a); }
首先,采用了刘弋同学的代码,程序的主函数会从命令行读入命令并进行分析。
6,测试设计:
1,输入 wc.exe -c test1.c -o result1.txt 预期输出字符数为48.
2,输入 wc.exe -w test2.c -o result2.txt 预期输出单词书数18,行数是3
3,输入 wc.exe -a test3.c -o result3.txt 预期输出代码行/空行/注释行:4/2/4
4,输入 wc.exe -w test4.c -e stoplist.txt -o result4.txt 预期输出单词数为:4
5,输入 wc.exe -a test5.c -o result5.txt 预期输出代码行/注释行/空行为:5/2/6
6,输入 wc.exe -a test6.c -o result6.txt 预期输出代码行/注释行/空行为:5/2/1
7,输入 wc.exe-s -w *.c -o result10.txt 预期输出
test1.c字符数为48
test2.c单词书数18,行数是3
test3.c代码行/空行/注释行:4/2/4
test4.c单词数为:4
test5.c代码行/注释行/空行为:5/2/6
草考文献:
https://github.com/Fatead/WordCount (刘弋的Git)
https://jingyan.baidu.com/article/0202781145eaab1bcc9ce5f0.html
http://blog.csdn.net/magic_jss/article/details/51472205