首页 > 技术文章 > 第2周个人作业:WordCount

boqianliu 2018-03-20 13:06 原文

2018.03.21 23:11 更新了github地址,对助教评论进行回复

----------------------------------------

github地址:https://github.com/chaseMengdi/WCProject

PSP2.1表格

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

   

· Estimate

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

 20  25

Development

开发

   

· Analysis

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

 15  20

· Design Spec

· 生成设计文档

 15  15

· Design Review

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

20  20

· Coding Standard

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

 25  20

· Design

· 具体设计

 15  20

· Coding

· 具体编码

240  300

· Code Review

· 代码复审

 40  60

· Test

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

 30  40

Reporting

报告

   

· Test Report

· 测试报告

 20  20

· Size Measurement

· 计算工作量

 15  15

· Postmortem & Process Improvement Plan

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

 20  20
 

合计

 475  575

1、解题思路

如果想要通过命令行执行,在main()函数里面带一个字符数组类型的参数就可以了,然后再对参数的个数和具体内容判断处理,达到作业的要求。

首先本程序需要用到文件读写内容,java可以用文件输入输出流来实现。

字符和单词统计,都可以先把文本内容读取为一个字符串,然后进行计数。

行数统计使用FileInputStream与Scanner类也可以很简单的达到。

扩展功能和高级功能目前还在编写中。

递归处理之前要先确定用户输入的是一个路径,然后获取文件名,写个for循环计算相关内容即可。

代码行注释行和空行,只要对文本转换后的字符串进行判定即可。

停用单词表就是先计算单词并存储出现的单词,然后再与已拥有的单词表对比即可。

2、程序设计实现过程

本程序暂定只存在一个类wc,一个java文件wc.java,6个函数。

    //向文件写入字符串
    public static void writeFile(String str,String outFilePath) 
    //将文本内容读取为一个字符串
    public static String readFile(String filePath) 
    //利用FileInputStream与Scanner计算行数
    public static int calLine(String filePath) throws FileNotFoundException
    //计算单词数
    public static int calWord(String str)
    //计算字符数
    public static int calChar(String str)  
  //主函数,目前只实现了基本功能
    public static void main(String[] args) throws FileNotFoundException

关键函数就是主函数参数的判断和执行,其他函数都较为简单。

多参数读取,利用for循环就可以达到。

自定义输出文件的话,判断倒数第二个参数是否为"-o",然后再进行下一步就可以了。

主函数流程图如下:

 

 

3、代码说明

关键的代码就是main函数的判断与流程执行,还有行数、单词数、字符数的计算。

前面的1和2里面,思路已经描述的很清楚了,在代码里面也有进一步的说明。


//
主函数,目前只实现了基本功能 public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub int charNum,lineNum,wordNum,argc=args.length; //String filePath="C:\\Users\\chase_mengdi\\Desktop\\file.txt"; String op,filePath,outFilePath,str; System.out.println("参数数量:"+argc); //倒数第二个参数不为“-o”,即默认输出到根目录下result.txt if (!args[argc-1].equals("-o")) { filePath = args[argc]; outFilePath="result.txt"; str=readFile(filePath); //System.out.println("文本字符串:"+str); //System.out.println("filepath is "+filePath); charNum=calChar(str); lineNum=calLine(filePath); wordNum=calWord(str); writeFile(filePath,outFilePath); //System.out.println("源文件路径成功写入!\n"); //循环读取参数,计算行数、单词数、字符数 for (int i = 1; i < argc+1; i++) { op = args[i]; System.out.println("args["+i+"] is "+ args[i]); if (op.equals("-c")) { System.out.println("字符数:"+charNum); writeFile("字符数:"+charNum,outFilePath); //System.out.println("字符数成功写入!"); } if (op.equals("-l")) { System.out.println("行数:"+lineNum); writeFile("行数:"+lineNum,outFilePath); //System.out.println("行数成功写入!"); } if (op.equals("-w")) { System.out.println("单词数:"+wordNum); writeFile("单词数:"+wordNum,outFilePath); //System.out.println("单词数成功写入!"); } } }

 

//利用FileInputStream与Scanner计算行数
    public static int calLine(String filePath) throws FileNotFoundException{
        int lineNum=0;
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        Scanner scanner = new Scanner(fis);
        while(scanner.hasNextLine()){
            scanner.nextLine();
            lineNum++;
        }
        return lineNum;
    }
    //计算单词数
    public static int calWord(String str){
        int wordNum = 0;
        String c,lastC=" ";
        for(int i=0;i<str.length();i++){
            c=str.substring(i, i+1);
            if(c.equals("/")){
                c=str.substring(i, i+2);
                i++;
            }
            //System.out.println(i+"="+c);
            //当前字符不为分隔符,且上一字符为分隔符
            if(((!c.equals(","))||(!c.equals(" "))||(!c.equals("/n"))
                    &&(lastC.equals(",")||lastC.equals(" ")||lastC.equals("/n"))))
                wordNum++;
            lastC=c;
        }
        return wordNum;
    }
    //计算字符数
    public static int calChar(String str){
        return str.length();
    }

 

4、测试设计过程

测试用例的设计主要力求全面,可以覆盖到每一个程序执行的路径,尽力测试出程序的漏洞,提高程序的质量。

测试文本file.txt内容:

Your Excellencies, UN secretary general, president of the general assembly,
executive director of UN Women, and distinguished guests.
Today, we are launching a campaign called HeForShe.
I'm reaching out to you because we need your help.
We want to end gender inequality
and to do this, we need everyone involved.
This is the first campaign of its kind at the UN.
We want to try and galvanize as many men and boys as possible to be advocates for change
and we don't just want to talk about it.
We want to try and make sure that it's tangible.
//www
/*
我是注释,你会统计我吗?
*/
{//
}//

测试用例:

(1)wc.exe -c file.txt
(2)wc.exe -s file.txt -o result_1.txt
(3)wc.exe -w file.txt
(4)wc.exe -a file.txt -o result_1.txt
(5)wc.exe -l file.txt
(6)wc.exe -e file.txt -o result_1.txt
(7)wc.exe -w -l  file.txt
(8)wc.exe -w -l  file.txt -o result_1.txt
(9)wc.exe -w -c file.txt
(10)wc.exe -w -e file.txt -o result_1.txt
(11)wc.exe -l -c file.txt
(12)wc.exe -l -c -s file.txt -o result_1.txt
(13)wc.exe -w -c -l file.txt
(14)wc.exe -w -c -a file.txt -o result_1.txt

5、参考文献链接

(1)Java读取文件的简单实现方法

(2)java读取txt文件,获取行数

(3)教你用JAVA统计一段英语短文中单词的个数,并且输出每一个单词

(4) 手把手教你如何把jar文件,打包成jar文件以及转换为exe可执行文件

(5) java打包成可执行的jar或者exe的详细步骤

PS:这次课设时间比较紧迫,短时间只能做到这个地步了OTZ。

推荐阅读