个人项目-WC

个人项目-WC项目

项目相关要求

wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。

实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:
程序处理用户需求的模式为:

wc.exe [parameter] [file_name]

  • 基本功能列表:

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

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

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


1.项目Github地址

我的github的项目:
https://github.com/HQingshan/WC-project

2.遇到的困难及解决方法

苦难:拖延症 剩下一天时间做这个

解决方法:用最快速度做完

3.关键代码

主要函数

    public static void main(String[] args) {
        String input = ""; // 存储输入
        System.out.println("please input: ");
        System.out.println("wc.exe -c file.c    //返回文件 file.c 的字符数");
        System.out.println("wc.exe -w file.c    //返回文件 file.c 的词的数目  ");
        System.out.println("wc.exe -l file.c    //返回文件 file.c 的行数");

        while (true) {
            Scanner s = new Scanner(System.in);
            input = s.nextLine();//得到输入命令
            String[] paramAndPath = input.split(" "); // 分割命令
            if(!paramAndPath[0].equals("wc.exe") || paramAndPath.length!=3) {
                System.out.println("error command");
                continue;
            }
            //获取命令
            String parameter =paramAndPath[1];
            // 获取文件名
            String fileName = paramAndPath[2];
            int[] result=counter(fileName);//计数
            output(parameter,result);//输出结果
        }
    }

输出函数

//输出函数
    private static void output(String parameter,int[] result) {
        if(result[0]==-1) return;
        switch(parameter) {
            case "-l":
                System.out.println("line count:"+result[0]);
                break;
            case "-w":
                System.out.println("word count:"+result[1]);
                break;
            case "-c":
                System.out.println("char count:"+result[2]);
                break;
            default:
                System.out.println("error command");
        }
        System.out.println("");
    }

计数函数

 //计数
    private static int[] counter(String fileName) {
        int[] result= {0,0,0};//result[0]对应行数;result[1]对应单词数;result[2]对应字符数
        File file = new File(fileName);
        if (file.exists()) {
            try {
                //文件流读取文件
                FileInputStream fis = new FileInputStream(file);
                InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
                BufferedReader br = new BufferedReader(isr);
                String line = "";
                StringBuffer sb = new StringBuffer();
                while ((line = br.readLine()) != null) {
                    result[0]++;
                    sb.append(line);
                    result[2] += line.length();
                }
                result[1] = sb.toString().split("\s+").length;//
                br.close();
                isr.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("please enter correct path");
            result[0]=-1;
        }
        return result;
    }

3.1测试

text文本

测试结果

4.PSP

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

学习进度条

第N周 新增代码(行) 累计代码(行) 本周学习耗时(小时) 累计学习耗时(小时) 重要成长
1 86 86 1 1 熟悉markdown还有git使用
... ... ... ... ... ...
原文地址:https://www.cnblogs.com/huangqingshan/p/12499217.html