软工作业--WC(java)

github传送门:https://github.com/hongzhang98/WC-Project/tree/master/wc

 

一、项目完成情况

        ----  -c    返回文件的字符数 (完成)

        ----  -w   返回文件的词数     (完成)

        ----  -l     返回文件的行数     (完成)

        ----  -a   返回文件的代码行 / 空行 / 注释行数 (完成)

        ----  -s   递归处理目录下符合条件的文件 (未完成)

        ----  -x   图形界面 (未完成)

 

、PSP表格

 

 

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

 40  30

· Estimate

· 估计这个任务需要多少时间

 40  30

Development

开发

 560  530

· Analysis

· 需求分析 (包括学习新技术)

 60  90

· Design Spec

· 生成设计文档

 50  40

· Design Review

· 设计复审 (和同事审核设计文档)

 30  30

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 30  30

· Design

· 具体设计

 90  50

· Coding

· 具体编码

 200  180

· Code Review

· 代码复审

 30  50

· Test

· 测试(自我测试,修改代码,提交修改)

 70  60

Reporting

报告

 120  100

· Test Report

· 测试报告

 50  30

· Size Measurement

· 计算工作量

 40  40

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 30  30

合计

   720

 660

 

 

、设计思路   

       从题目需求分析,首先要考虑如何用输入流读取文件中的内容,读到内容后如何截取内容,如何分析截取的内容,所以我是用了readline()的方法读取了由BufferedReader所读入的内容,再用各种方法如startwith、endwith、silt及各种条件限制计算字符数、行数,其中最特殊的为以“/*”开头和以“*/”结尾,这里我用了一个标记来记录,来区分“/*”和“*/”这种注释,同时用nextline()方法来记录键盘的输入,以判断请求的功能。

 

、关键代码

 

统计代码

public class Count {
      
    
    
    private static final char[] chars = null;
    private static final char[] words = null;
    private static final char[] lines = null;

    public static void CountAll(String sc,String sc2){     
     BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(new File(sc2)));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     
     String word = null;
     int lines = 0, words = 0, chars = 0, blanklines = 0, notelines = 0, codelines = 0;
     boolean flag = true;
     try {
           while((word = reader.readLine()) != null)
             {   if(flag)
                   {   if(word.length() < 1)
                           blanklines++; 
                       else if(word.startsWith("//"))
                           notelines++;
                       else if(word.startsWith("/*") && word.endsWith("*/"))
                           notelines++;
                       else if(word.startsWith("/*") && !word.endsWith("*/"))
                          {   notelines++;
                              flag = false;
                          }
                       else codelines++;
                    }
                   else if(!word.startsWith("/*") && word.endsWith("*/"))
                     {   notelines++;
                        flag = true;
                     }
                   else notelines++;         
                  words = words + word.split("[ \\,?.]").length;
                  chars = chars + word.length();                   
                  lines++;
             } 
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
     try {
        reader.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     switch (sc)
        {   case "-c": System.out.println("字符数为:" + chars); break;
            case "-w": System.out.println("词数为:" + words); break;
            case "-l": System.out.println("行数为:" + lines); break;
            case "-a": System.out.println("代码行数为:" + codelines + "\n" + "空行数为:" + blanklines + "\n" + "注释行数为:" + notelines); break;
        }
    }

 

main函数

public static void main(String[] args) throws IOException {
     Scanner sc = new Scanner(System.in); 
     String a = sc.nextLine();
     String[] b = a.split(" ");
     System.out.println(b[0]+" "+b[1]);
     CountAll(b[0],b[1]);
}

 

、测试结果

测试文件

 

 

测试结果

 

 

 

 

 

、总结

      我已经很久没碰java了,虽然我的java也只是半桶水的水平,但这次wc作业还是让我重温了一下java的知识,也从这次作业中规范了一下自己的编程习惯,同时也是我第一次写博客,还是一次挺新鲜的体验的,虽然这次作业花的时间不短,但总体来说还是获益匪浅的。

 

posted on 2018-09-14 22:50  zhh111  阅读(91)  评论(0编辑  收藏  举报