第二次作业

github地址

 https://github.com/xieqiguang/xqg

psp2.1表格

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

30

30 

· Estimate

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

30 

30 

Development

开发

350

460 

· Analysis

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

20 

20 

· Design Spec

· 生成设计文档

30 

20 

· Design Review

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

30 

40 

· Coding Standard

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

30 

30 

· Design

· 具体设计

60 

70 

· Coding

· 具体编码

120 

180 

· Code Review

· 代码复审

30 

40 

· Test

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

30 

60 

Reporting

报告

60

90

· Test Report

· 测试报告

20 

30 

· Size Measurement

· 计算工作量

20 

30 

· Postmortem & Process Improvement Plan

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

20 

30 

 

合计

440

580 

解题思路

首先要进行文件的读入,运用到I/O流。通过\n”判断是否换行,通过“ ”判断是否为一个单词结束。当然,通过计数判断有几个字符。注意,在这里我把空格也当做字符处理。

程序设计实现过程

wc

统计文本文件的行数、字符数、单词数函数

main

主函数接收从命令行传来的参数,通过wc函数统计结果并输出到文件中

代码说明

 

/**

 * 统计文本文件的行数,单词书,字符数

 */

class WordCount {

    public static int words = 1;//单词数

    public static int lines = 1;//行数

    public static int chars = 0;//字符数

 

    public static void wc(InputStream f) throws IOException {

        int c = 0;

        boolean lastNotWhite = false;

        String whiteSpace = " \t\n\r";

        while ((c = f.read()) != -1) {

         if(c!='\n'&&c!='\0'&&c!='\r')

         chars++;

            if (c == '\n') {

                lines++;

            }

            if (whiteSpace.indexOf(c) != -1) {

                if (lastNotWhite) {

                    words++;

                }

                lastNotWhite = false;

            } else {

                lastNotWhite = true;

            }

        }

}

 

public static void main(String args[]) throws IOException {

     int size=args.length;

boolean l=false,o=false,w=false,c=false;

String output="result.txt";

String filepath;

//读取命令行中的指令

for(int i=0;i<size;i++){

if(args[i].length()==2){

switch(args[i].charAt(0)){

case 'l':l=true;break;

case 'o':o=true;

output=args[++i];

break;

case 'w':w=true;break;

case 'c':c=true;break;

}

}

else

filepath=args[i];

}

        FileInputStream f;

        

        try {

            if (args.length == 0) { // 检查是否读入

             Scanner input1 =new Scanner(System.in);

             String str=input1.next();

                f = new FileInputStream(str);

                wc(f);

            } else { // 判断行

                for (int i = 0; i < args.length; i++) {

                    f = new FileInputStream(args[i]);

                    wc(f);

                }

            }

        } catch (IOException e) {

            return;

        }

        File file = new File(output);

        FileOutputStream fos = new FileOutputStream(file);

        

        

        if(c){

         String content = "字符数:"+chars;

         fos.write(content.getBytes());

        }

        else if(w){

         String content = "单词数:"+words;

         fos.write(content.getBytes());

         //输出单词数

        }

        else if(l){

         String content = "行数:"+lines;

         fos.write(content.getBytes());

         //输出行数   指定的输出文件用output

        }

        fos.flush();

        fos.close();

}

}

测试设计过程

wc.exe -c file.c     //返回文件 file.c 的字符数

wc.exe -w file.c     //返回文件 file.c 的单词总数

wc.exe -l file.c     //返回文件 file.c 的总行数

wc.exe -o outputFile.txt     //将结果输出到指定文件outputFile.txt

wc.exe -s            //递归处理目录下符合条件的文件

wc.exe -a file.c     //返回更复杂的数据(代码行 / 空行 / 注释行)

wc.exe -e stopList.txt  // 停用词表,统计文件单词总数时,不统计该表中的单词

wc.exe -i file.c   //错误指令

wc.exe -c 123.c  //文件不存在

Wc.exe qwe.c   //未输入指令

 

 

参考文献链接

http://blog.csdn.net/ycy0706/article/details/45457311

https://wenku.baidu.com/view/a16b2dd8b307e87101f696ed.html

posted @ 2018-03-20 22:11  谢奇光  阅读(148)  评论(1)    收藏  举报