标签:RoCE software 有一个 exe NPU 工作 proc dev 退出
能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有 wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
基本功能:
扩展功能
说明:以上命令可叠加使用,即 -c -w -l <文件名> ,统计文件的字符数,单词数和行数。
WordCount 实现思路如下:
设计的流程图:

代码分包:

程序入口,获取输入,并进行解析,若为无输入则退出程序。
public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String m;
        while (!TextUtil.isEmpty(m = s.nextLine())) {
            String arr[] = m.split("\\s+");
            parseCommand(arr);
        }
    }
先检查输入是否正确,若正确则根据时候需要递归文件做不同的处理。
    /**
     * 解析并执行输入的命令
     *
     * @param str 输入
     */
    private static void parseCommand(String str[]) {
        String lastInput = str[str.length - 1];
        if (!checkInput(str)) {
            ListUtil.clear(sCommandList,sArgs);
            return;
        }
        if (sArgs.contains(CommandType.SEARCH_COUNT)) {
            executeWithDir(lastInput);
        } else {
            executeWithFile(lastInput);
        }
        ListUtil.clear(sCommandList,sArgs);
    }
判断输入的命令是否为和合法的命令,并初始化命令处理。
    /**
     * 检查输入是否为正确的输入
     *
     * @param str 输入
     * @return 返回结果
     */
    private static boolean checkInput(String[] str) {
        String args;
        boolean isCorrect = true;
        for (int i = 0; i < str.length; i++) {
            args = str[i];
            if (args.equals(CommandType.CHAR_COUNT) && !sArgs.contains(args)) {
                sCommandList.add(new CharCommand());
                sArgs.add(args);
            } else if (args.equals(CommandType.WORD_COUNT) && !sArgs.contains(args)) {
                sCommandList.add(new WordCommand());
                sArgs.add(args);
            } else if (args.equals(CommandType.LINE_COUNT) && !sArgs.contains(args)) {
                sCommandList.add(new LineCommand());
                sArgs.add(args);
            } else if (args.equals(CommandType.ALL_COUNT) && !sArgs.contains(args)) {
                sCommandList.add(new AllCommand());
                sArgs.add(args);
            } else if (args.equals(CommandType.SEARCH_COUNT) && !sArgs.contains(args)) {
                sArgs.add(args);
            } else if (i == str.length - 1) {
                isCorrect = checkFile(args);
            } else {
                showErrorMsg(args);
                isCorrect = false;
            }
        }
        return isCorrect;
    }
对文件进行检查,分为文件和递归的文件夹。
    /**
     * 检查文件输入是否正确
     *
     * @param args 输入
     * @return 返回结果
     */
    private static boolean checkFile(String args) {
        if (sArgs.contains(CommandType.SEARCH_COUNT)) {
            return checkCommandDir(args);
        } else {
            return checkCommandFile(args);
        }
    }
提示正确的输入。
    /**
     * 显示提示
     */
    private static void showErrorMsg(String s) {
        System.out.println(s + " 不是内部命令!");
        System.out.println("用法:");
        System.out.println("-c <文件名> ,统计文件字符数。");
        System.out.println("-w <文件名> ,统计文件单词数。");
        System.out.println("-l <文件名> ,统计文件行数数。");
        System.out.println("-s <文件路径+类型(?任意)> ,递归符合条件的文件。");
        System.out.println("-a <文件名> ,统计文件代码行,空白行,注释行。");
    }
检查 文件的输入是否正确,以及文件是否存在。
    /**
     * 检查文件输入是否正确
     *
     * @param filePath 输入文件名
     * @return 返回结果
     */
    private static boolean checkCommandFile(String filePath) {
        File file = new File(filePath);
        if (!file.isFile()) {
            System.out.println(filePath + " 不是一个完整或者正确的文件名!");
            return false;
        } else if (!file.exists()) {
            System.out.println(filePath + " 文件不存在!");
            return false;
        }
        return true;
    }
检查文件夹的输入是否正确,文件夹是否存在。
    /**
     * 检查文件夹输入是否正确
     *
     * @param dirPath 输入路径和文件通配符
     * @return 返回结果
     */
    private static boolean checkCommandDir(String dirPath) {
        if (!dirPath.contains("*")) {
            System.out.println(dirPath+"不是一个合法输入!");
            System.out.println("-s <文件路径+类型(?任意)> ,递归符合条件的文件。");
            return false;
        }
        String fileDir = dirPath.substring(0, dirPath.indexOf("*") - 1); // 文件所在文件夹
        File dir = new File(fileDir);
        if (!dir.isDirectory()) {
            System.out.println(fileDir + " 不是一个完整或者正确的文件夹名!");
            return false;
        } else if (!dir.exists()) {
            System.out.println(fileDir + " 文件夹不存在!");
            return false;
        }
        return true;
    }
对文件夹递归得到的所有符合条件的文件进行处理。
    /**
     * 处理文件夹
     *
     * @param input 输入
     */
    private static void executeWithDir(String input) {
        ArrayList<String> list = new SearchCommand().execute(input);
        for (String s : list) {
            System.out.println(s);
            for (Command c : sCommandList) {
                c.execute(s);
            }
        }
    }
对单一的文件进行处理。
    /**
     * 处理文件
     *
     * @param input 输入
     */
    private static void executeWithFile(String input) {
        for (Command c : sCommandList) {
            c.execute(input);
        }
    }
空白文件


只有一个字符的文件


只有一行的文件


递归文件调用

| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) | 
|---|---|---|---|
| Planning | 计划 | 30 | 60 | 
| · Estimate | · 估计这个任务需要多少时间 | 180 | 240 | 
| Development | 开发 | 150 | 180 | 
| · Analysis | · 需求分析 (包括学习新技术) | 15 | 15 | 
| · Design Spec | · 生成设计文档 | 10 | 10 | 
| · Design Review | · 设计复审 (和同事审核设计文档) | 10 | 15 | 
| · Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 10 | 10 | 
| · Design | · 具体设计 | 18 | 15 | 
| · Coding | · 具体编码 | 120 | 150 | 
| · Code Review | · 代码复审 | 30 | 35 | 
| · Test | · 测试(自我测试,修改代码,提交修改) | 10 | 10 | 
| Reporting | 报告 | 15 | 20 | 
| · Test Report | · 测试报告 | 10 | 20 | 
| · Size Measurement | · 计算工作量 | 15 | 15 | 
| · Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 12 | 30 | 
| 合计 | 635 | 825 | 
Word Count 的难点在于对输入进行判断和对不同的命令的处理的过程,需要理清不同命令参数和最后的文件的路径的输入的关系,在设计之前根据开发 的进度不断扩展,由于将命令处理抽象出来,因而后面的功能的添加较为方便。但是一开始项目的框架流程没有设计好,再添加了检查方法后对流程该较大,因此在开发之前就要设计好整体项目的架构。
标签:RoCE software 有一个 exe NPU 工作 proc dev 退出
原文地址:https://www.cnblogs.com/PriateHat/p/9643740.html