zoukankan      html  css  js  c++  java
  • 作业

    书上的一个小作业,要求得到一段代码的单词数,字符数和行数。我用Android写了一个程序。来实现这些功能:
    得到代码的字符数:

    public int getCharNum(InputStream inputStream1) throws IOException {
            reader = new InputStreamReader(inputStream1);
            bufferedReader = new BufferedReader(reader);
            int i;
            while ((i = bufferedReader.read()) != -1){
                num ++;
            }
            reader.close();
            bufferedReader.close();
            return num;
        }
    

    得到代码的行数:

     public int getLineNum(InputStream inputStream2) throws IOException{
            reader = new InputStreamReader(inputStream2);
            bufferedReader = new BufferedReader(reader);
            String str;
            while (( str = bufferedReader.readLine()) != null){
                num ++;
            }
            reader.close();
            bufferedReader.close();
            return num;
        }
    

    得到代码的单词数:

     public int getWordNum(InputStream inputStream3) throws IOException{
            reader = new InputStreamReader(inputStream3);
            bufferedReader = new BufferedReader(reader);
            String str1;
            while (( str1 = bufferedReader.readLine()) != null){
                //单词数,split()方法,返回是一个数组,根据(空格,标点符号)分割成字符串数组,数组长度就是单词数
                num += str1.split("[  , . ? { } ( )]").length;//使用正则表达式实现多个分隔符进行分隔的效果。
            }
            return num;
        }
    

    通过按钮的事件监听,调用以上方法:

    public void onClick(View view) {
            switch (view.getId()){
                case R.id.button_stats:
                    IOUtil ioUtil1 = new IOUtil();
                    IOUtil ioUtil2 = new IOUtil();
                    IOUtil ioUtil3 = new IOUtil();
                    InputStream inputStream1 = getResources().openRawResource(R.raw.a);
                    InputStream inputStream2 = getResources().openRawResource(R.raw.a);
                    InputStream inputStream3 = getResources().openRawResource(R.raw.a);
                    try {
                        int chars = ioUtil1.getCharNum(inputStream1);
                        int lines = ioUtil2.getLineNum(inputStream2);
                        int words = ioUtil3.getWordNum(inputStream3);
                        textView_show.setText("文本的字符数为:" +chars+ "
    "+ "文本的行数为:" +lines
                        +"
    "+"文本的单词数:"+words);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    

    这是我随机截取的一段代码,用来测是我的程序:

    public class IOUtil  {
        private int num = 0;
        public int charNum(String fileName){
            try {
                int i;
                FileInputStream fileInputStream = new FileInputStream(fileName);
                while ((i = fileInputStream.read()) != -1){
                    num ++;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return num;
        }
    }
    

    运行效果:

    由于能力有限,目前只是简单的实现了这几个功能。而且这个程序存在bug,比如我用split()方法,通过使用正则表达式实现多个分隔符进行分隔的效果。但在列举进行分割的符号中存在漏洞。希望在以后的学习中可以找的解决的方法。

    下面是我写这个程序的耗时记录表,从开始找资料到完成这些功能并检测,总计大概用了8小时。

    PSP2.1 Personal SofterWare Process Stages Time(%)
    Planning 计划 12.5
    ·Estimate ·估计这个任务的需要时间 12.5
    Development 开发 87.5
    ·Analysis ·需求分析 6.25
    ·Design ·具体设计 6.25
    ·Coding ·具体编码 62.5
    ·Test ·代码测试 12.5
  • 相关阅读:
    js字符串拼接 ·${}·
    [转]CRLF Injection
    域渗透-GPP(组策略)利用
    AS-REPRoasting
    域用户名枚举
    我理解的HTTP请求走私(HTTP Request Smuggling)
    NFS未授权访问
    Hessian反序列化RCE漏洞
    CVE-2020-15778 Openssh命令注入漏洞复现
    在非域内机器上运行harphound
  • 原文地址:https://www.cnblogs.com/Zyhzy4/p/6617471.html
Copyright © 2011-2022 走看看