菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
288
0

Word Count

原创
05/13 14:22
阅读数 63907

1.Github项目地址

https://github.com/ZJHqs/WC

2.实现程序前,模块开发预计时间

PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning 计划 60     30
Estimate 估计这个任务需要多少时间 20  10
Development 开发 400  300
Analysis 需求分析(包括学习新技术) 600  900 
Design Spec 生成设计文档 30  30
Design Review 设计复审(和同事审核设计文档) 0  
Coding Standard 代码规范(为目前的开发置顶合适的规范) 30  15
Design 具体设计 60  90
Coding 具体编码 240  180
Code Review 代码复审 300  60
Test 测试(自我测试,修改代码,提交修改) 120 300 
Reporting 报告 60  90
Test Report 测试报告 30 45 
Size Measurement 计算工作量 20 10 
Postmortem & Process Improvement Plan 事后总结,并提出过程改进计划 20 15 
Total 合计 1990  2075

3.解题思路

编程语言的选择

因为最近在学Java,而之前学的C语言也没有学过IO操作,索性用Java来写。

需要用到的新知识

因为我学Java还没学到IO那一部分,为了完成这次作业,快速地看了那一章内容,之后还得再仔细看看

结构的分析

因为只实现了四个功能,所以就将三个基本功能一起写,然后扩展的功能额外写一个函数去实现它。

遇到的困难

在算注释行/**/的时候遇到了困难,不知道该怎么样去计算注释行,后来查了一些资料解决了这个问题。

然后在注释行//的时候,发现了有些程序员可能注释写的时候可能会有空格 特殊字符等,利用正则表达式比较难解决这个问题。

4.设计实现过程

功能模块

基本功能:Operation(String[] command){...}

统计特殊行:SpecialLine(BufferedReader br){...}

5.代码说明

1.主方法:利用数组存储命令与被操作文件路径及文件名称

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) throws Exception{
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("please input '[order] [filename]':");
System.out.print("wc.exe ");
String command[]=sc.nextLine().split(" ");
Operation(command);
}
}

2.基本功能 统计字符数、词个数、行数,若命令为"-a",调用SpecialLine方法

//实现基本操作
public static void Operation(String[] command) throws Exception{
String line=null;
int CharCount=0,WordCount=0,LineCount=0;
FileInputStream fin=new FileInputStream(command[1]);
InputStreamReader reader=new InputStreamReader(fin);
BufferedReader br=new BufferedReader(reader);
//统计字符数
if ("-c".equals(command[0])){
while((line=br.readLine())!=null){
CharCount=CharCount+line.length();
}
System.out.println("CharCount:"+CharCount);
}
//统计词个数
else if ("-w".equals(command[0])){
while ((line=br.readLine())!=null){
//Matcher m=Pattern.compile("(\\w+)").matcher(line);
Matcher m=Pattern.compile("\\b\\w+\\b").matcher(line);
while(m.find()){
WordCount++;
}
}
System.out.println("WordCount:"+WordCount);
}
//统计行数
else if ("-l".equals(command[0])){
while((line=br.readLine())!=null){
LineCount++;
}
System.out.println("LineCount:"+LineCount);
}
else if ("-a".equals(command[0])){
SpecialLine(br);
}
}
3.统计特殊行数//统计行数
    //统计特殊行
public static void SpecialLine(BufferedReader br)throws Exception{
String line=null;
boolean flag=false;
int BlankLine=0,CodesLine=0,CommentsLine=0;
//统计空行
while ((line=br.readLine())!=null){
//除去注释前的空格
line=line.trim();
//m1匹配空行
Matcher m1=Pattern.compile("^\\s*$").matcher(line);
//m2匹配//型注释
Matcher m2=Pattern.compile("\\/{2,}\\S*$").matcher(line);
if (m1.find()) {
BlankLine++;
}
else if (m2.find()){
CommentsLine++;
}
else if (line.startsWith("//")){
CommentsLine++;
}
else if (line.startsWith("/*")&&!line.endsWith("*/")){
CommentsLine++;
flag=true;
}
else if (line.startsWith("/*")&&line.endsWith("*/")){
CommentsLine++;
}
else if (flag==true){
CommentsLine++;
if (line.endsWith("*/")){
flag=false;
}
}
else {
CodesLine++;
}
}
System.out.println("CodesLine:"+CodesLine);
System.out.println("BlankLine:"+BlankLine);
System.out.println("CommentsLine:"+CommentsLine);
}
}
}
 

5.测试运行

测试所用文件:

 

 其中A为空文件 字符数、字个数、行数、特殊行数均为0

 

 

B为单字符文件 字符数、字个数、行数、代码行数为1 空行、注释行为0

 

 C为只有一行的文件 字符数:18 字个数:3 行数:1 代码行:1 空行、注释行:0

 

 D为一个典型源文件

字符数个数:101 字个数:10 行数:9  代码行:4 空行:0 注释行:5

 

 

测试结果:

 

 

 

 

 

 

 

 均与文件所拥有的字符数、词个数、行数、特殊行数相符合

7.项目小结

通过本次练习,不仅初步掌握了I/O的基本操作,还对软件工程的思想有了进一步的认识,通过估计时间与实际完成时间的对比发现自己比想象中还要弱= =,也让我对Java的正则表达式有了更深刻的理解,不得不说这个确实是非常的灵活好用,就是对一些习惯不好的程序员比如我来说想要匹配他们奇奇怪怪的代码格式需要考虑的东西很多,认识到做好代码规范的重要性。

 

发表评论

0/200
288 点赞
0 评论
收藏
为你推荐 换一批