编程作业1

|这个作业属于哪个课程| https://edu.cnblogs.com/campus/zswxy/computer-science-class3-2018 |

|这个作业要求在哪里| https://edu.cnblogs.com/campus/zswxy/computer-science-class3-2018/homework/11879 |

|这个作业的目标| 学习使用github、撰写词汇统计程序 |

part1:

WordCount编程

1. Github项目地址

2. PSP表格

3. 解题思路描述

4. 代码规范制定链接

5. 设计与实现过程

6. 性能改进

7. 单元测试

8. 异常处理说明

9. 心路历程与收获 psp

 gitee地址

 https://gitee.com/guo-yadong/project-java

     psp表格

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

3.解题思路

   首先需要使用gets()函数来获取输入的字符串,因为题目要求最大为1000个字符所以定义一个char类型的函数大小为1001.也就是char b[1001];

   要获取字符串中最大出现最多的那个字符:先要定义一个指针p指向字符串b; 也就是*p=b; 

   定义指针的目的是为了指向字符串b,那么就可以再定义一个数组int a[200]={0};用于存放每个字符的个数;因为每个字母所对应的阿斯科码一般是不超过200的所以定义一个大小为200的int 型数组。数组内存放的是每个阿斯科码对应出现的个数,也就是从零到两百的阿斯科码中,这些阿斯科码出现的次数;有了这个函数就可以实现对字符串中数据的统计;

    此时指针p已经指向了我们输入的字母串b;想要判断字符串中的字母是位于A-Z之间还是a-z之间,因为题目要求的是不区分大小写的进行统计,输出结果也是小写的。所以要加一个判断的语句。也就是if('A'<*p && *p<'Z');如果当前的字符是大写的,就需要转换成小写的然后存储到数组a中也就是执行下列两个步骤:1、*p=*p-'A'+'a'; 2、a[*p]++; 第一步将当前字符的阿斯科码减去大写A的阿斯科码再加上小写a的阿斯科码 就可以得到当前字符在小写的阿斯科码。然后放入数组a当中自加一;如果当前的是小写的字符就不需要执行步骤1了,直接执行步骤2;最后指针向后移一位,即p++;

    最后需要在数组a当中找出数值最大的那一个;这时候需要定义两个变量来存放输出的结果,这里定义为max,k;

    查阿斯科码表可以得到小写a 到小写z的数值是在97和122之间;

定义一个循环来接这个数组里面的最大值;

for(int i=97;i<122;i++){
if(a[i]>max)

max=a[i];  //记录下最大的值

k=i;         //同时记录下当前最大值所对应的阿斯科码

}

4. 代码规范制定链接

 https://gitee.com/guo-yadong/project-java/blob/master/20188487%20郭亚东

5.实现过程

一、CharactersCount(字符数统计类)

public static int charactersCount(String filename) {
        File file = new File(filename);
        int cnt = 0;//总行数
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            int temp;
            while ((temp = br.read()) != -1) {
                cnt ++;
            }
            br.close();    
        } catch(Exception e) {
            System.out.println("文件不存在!");
        }
        return cnt;
    }

二、WordsCount(单词总数统计类)

public static String[] words = new String[1000];//单词数组 
    public static int wordsCount(String filename) {
        File file = new File(filename);
        String[] str = new String[1000];//仅包含字母和数字的字符串数组            
        int cnt = 0;//筛选出的字符串个数
        int num = 0;//单词数
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String tempLine;
            while ((tempLine = br.readLine()) != null) {
                if (!tempLine.equals("")) {
                    Pattern pattern = Pattern.compile("\s+|\W+");
                    String[] newStr = pattern.split(tempLine);
                    for(String ss : newStr) {
                        if (isLetterDigit(ss)) {
                            str[cnt++] = ss;
                        }
                    }
                }
            }
            for (String ss : str) {
                if (isWord(ss)) {
                    words[num++] = ss;
                }
            }
            br.close();
        } catch(IOException e) {
            e.printStackTrace();//System.out.println("文件不存在!");
        }
        return num;
    }

isLetterDigit函数

private static boolean isLetterDigit(String str) {//判断仅包含字母和数字的字符串
        String regex = "^[a-z0-9A-Z]+$";
        return str.matches(regex);
    }

isWord函数

private static boolean isWord(String str) {//判断是否为单词
        if (str != null&&str.length() >= 4) {//此处因为要判断参数是否为空导致空指针异常
            char ch1 = str.charAt(0);
            char ch2 = str.charAt(1);
            char ch3 = str.charAt(2);
            char ch4 = str.charAt(3);        
            if (((ch1 >= 'A' && ch1 <= 'Z')||(ch1 >= 'a' && ch1 <= 'z'))
                    &&((ch2 >= 'A' && ch2 <= 'Z')||(ch2 >= 'a' && ch2 <= 'z'))
                    &&((ch3 >= 'A' && ch3 <= 'Z')||(ch3 >= 'a' && ch3 <= 'z'))
                    &&((ch4 >= 'A' && ch4 <= 'Z')||(ch4 >= 'a' && ch4 <= 'z'))) 
                return true;
            else return false;
        }
        else return false;
    }
}

三、LinesCount(有效行数统计类)

public static int linesCount(String filename) {
        File file = new File(filename);
        int cnt = 0;//总行数
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String tempLine;
            while ((tempLine = br.readLine()) != null) {
                cnt++;
                if (tempLine.equals(""))//判断行为空白字符的行
                    cnt--;
            }
            br.close();
        } catch(Exception e) {
            System.out.println("文件不存在!");
        }
        return cnt;
    }

四、FrequentWordsCount(频词个数统计类)

public static void frequentWordsCount() {
        int size = WordsCount.words.length;
        Map<String,Integer> map = new HashMap<String,Integer>();
        int cnt = 0;
        String[] words = new String[size];
        System.arraycopy(WordsCount.words, 0, words, 0, size);
        for (int i = 0; i < size; i++) {
            if (words[i] != null)
            words[i] = words[i].toLowerCase();
        }
        for (int i = 0; i < size; i++) {
            if (map.containsKey(words[i])) {
                map.put(words[i],map.get(words[i])+1);
            }
            else
                map.put(words[i],1);
        }
        map = sortByValueDescending(map);
        for(Entry<String, Integer> vo : map.entrySet()) {
            if (vo.getKey() !=null) {
                frequentWords[cnt] = vo.getKey();
                num[cnt++] = vo.getValue();
            }
        }
    }

sortByValueDescending函数

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDescending(Map<K, V> map) {
        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
            @Override
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                int compare = o1.getValue().compareTo(o2.getValue());
                return -compare;
            }
        });
        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

实现过程

 

 心路历程与收获

作业刚刚发布的时候,看起来一脸懵逼,感觉好长好长,肯定完蛋,肯定不会做,肯定要漏交,经过零零散散的做,做的也不是很完全,出现了很多纰漏,自己也不怎么会,只能赶着时间做一点。

原文地址:https://www.cnblogs.com/Allen15773771785/p/14591096.html