20210326-软件工程作业-3-编程作业

这个作业属于哪个课程 软工-2018级计算机1班
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-science-class2-2018
这个作业的目标 实践学习软件工程
学号 20188377

Github地址

https://github.com/leoleo-7/Project-Java

PSP表格

PSP Personal Software Process tages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 10 15
Estimate 估计这个任务需要多少时间 300 350
Development 开发 60 80
Analysis 需求分析(包括学习新技术)
Design Spec 生成设计文档 20 30
Design Review 设计复审 10 10
Coding Standard 代码规范 (为目前的开发制定合适的规范)
Design 具体设计 60 60
Coding 具体编码 60 60
Code Review 代码复审 10 10
Test 测试(自我测试,修改代码,提交修改) 20 20
Reporting 报告 60 60
Test Repor 测试报告 20 20
Size Measurement 计算工作量 5 5
Postmortem & Process Improvement Plan 事后总结, 并提出过程改进计划 20 20
合计 625 710
三、需求
实现一个命令行程序,不妨称之为WordCount。

1、实现基本需求
假设有一个软件每隔一小段时间会记录一次用户的搜索记录,记录为英文。

输入文件和输出文件以命令行参数传入。例如我们在命令行窗口(cmd)中输入:

//C语言类
WordCount.exe input.txt output.txt

//Java语言
java WordCount input.txt output.txt
则会统计input.txt中的以下几个指标

统计文件的字符数(对应输出第一行):

只需要统计Ascii码,汉字不需考虑
空格,水平制表符,换行符,均算字符
统计文件的单词总数(对应输出第二行),单词:至少以4个英文字母开头,跟上字母数字符号,单词以分隔符分割,不区分大小写。

英文字母: A-Z,a-z
字母数字符号:A-Z, a-z,0-9
分割符:空格,非字母数字符号
例:file123是一个单词, 123file不是一个单词。file,File和FILE是同一个单词
统计文件的有效行数(对应输出第三行):任何包含非空白字符的行,都需要统计。

统计文件中各单词的出现次数(对应输出接下来10行),最终只输出频率最高的10个。

频率相同的单词,优先输出字典序靠前的单词。

例如,windows95,windows98和windows2000同时出现时,则先输出windows2000

输出的单词统一为小写格式

然后将统计结果输出到output.txt,输出的格式如下;其中word1和word2 对应具体的单词,number为统计出的个数;换行使用'\n',编码统一使用UTF-8。

题目需求很明确,所以主函数有着四个功能,分别是获取行数,获取总单词数,还有获取字符数,统计各个单词及出现次数。

主函数

import java.io.;
import java.util.;

public class WordCount {
private static final File ROOT_File =new File("D:\新建文件夹");
private static int count = 0;
private static Map<String,Integer> wordCount = new HashMap<>();

public static void main(String [] args) throws Exception{
    String intputFileName =  args[0];
    String outputFileName = args[1];
    File inputFile = new File(ROOT_File,intputFileName);
    File outputFile = new File(ROOT_File,outputFileName);

    // 判断是否存在:
    if(inputFile.exists()){
        doCheck(inputFile);
    }else{
        throw new RuntimeException("error");
    }
    PrintStream stream = new PrintStream(new FileOutputStream(outputFile));
    System.setOut(stream);
    show();
    System.out.println("单词数:"+obtainTotalWords());
    System.out.println("行数:"+count);
    System.out.println("字符数:"+(inputFile.length()));
}
public  static void doCheck(File inputFile) throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
    String line = null;
    while(null!=(line=br.readLine())){
        incrLine();
        // 分析每一行。
        analysis(line);
    }
}

获取行数

public static void show(){
    Set<Map.Entry<String, Integer>> entries = wordCount.entrySet();
    // 排序
    ArrayList<String> words = new ArrayList<>();
    for (Map.Entry<String, Integer> entry : entries) {
        words.add(entry.getValue()+"#"+entry.getKey());
    }

    // 排序
    Collections.sort(words);
    words.forEach(obj->{
        String[] split = obj.split("#");
        String str = split[1]+": "+split[0];
        System.out.println(str);
    });
}

public static void incrLine(){
    // 行数加1
    count++;
}

获取总单词数

//总单词数
public static long obtainTotalWords(){
    long sum = 0;
    Set<Map.Entry<String, Integer>> entries = wordCount.entrySet();
    for (Map.Entry<String, Integer> entry : entries) {
        sum+=entry.getValue();
    }
    return sum;
}

统计单词及其出现的个数

// 得到每一个单词以及次数, 并且记录到Map集合中
public static void analysis(String line){
    String [] words = line.split(" ");
    for(int i=0;i<words.length;i++){
        String word = words[i].trim();
        word = word.toLowerCase();
        word = word.contains(",")||word.contains(".")?word.substring(0,word.length()-1):word;
        if(word.length()>=4&&isWord(word.substring(0,4))){
            if(wordCount.containsKey(word)){
                Integer count = wordCount.get(word);
                count++;
                wordCount.put(word,count);
            }else{
                wordCount.put(word,1);
            }
        }
    }
}

public static boolean isWord(String word){
    for(int i=0;i<word.length();i++){
        if(word.charAt(i)>=97 && word.charAt(i)<=122){
            continue;
        }else{
            return false;
        }
    }
    return true;
}


总结:
看到题目之后,嗯?不能使用第三方库,并且只能使用C和Java。对于我这种很少敲c和java的人来说,这道作业着实有了难度。首先主要问题可能还是代码问题,代码使用Java理解并且写出来还是耗费了许多时间的。关于性能分析的话,感慨一句如果使用python的可能会方便很多。对于c和java,费好大劲才借鉴理解别人的代码。并且代码规范的话,也算是平常敲代码所养成的习惯,复习了git和Java的一些相关内容也还是值得的。

posted @ 2021-04-01 23:17  PeanutLeo  阅读(91)  评论(0)    收藏  举报