编程作业

第二次编程作业

这个作业属于哪个课程《软件工程》
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-science-class3-2018/homework/11879
这个作业的目标 学习使用git命令,熟悉开发过程
参考文献 https://blog.csdn.net/w2462140956/article/details/104965486

git地址:https://gitee.com/wangzihao10_admin/project-C.git

PSP表

 
 
PSP2.1Personal Software Process Stages预估耗时实际耗时
Planning 计划 2h 1h
• Estimate • 估计这个任务需要多少时间 1h 2h
Development 开发 8h 5h
• Analysis • 需求分析 (包括学习新技术) 2h 3h
• Design Spec • 生成设计文档 1h 1h
• Design Review • 设计复审 1h 0.5h
• Coding Standard • 代码规范 (为目前的开发制定合适的规范) 2h 1h
• Design • 具体设计 1h 2h
• Coding • 具体编码 3h 2h
• Code Review • 代码复审 1h 2h
• Test • 测试(自我测试,修改代码,提交修改) 3h 1h
Reporting 报告 2h 1h
• Test Repor • 测试报告 2h 4h
• Size Measurement • 计算工作量 1h 3h
• Postmortem & Process Improvement Plan • 事后总结, 并提出过程改进计划 0.5h 1h
合计   17.5h 29.5 
 
设计与实现过程:
需求
实现一个命令行程序,不妨称之为WordCount。

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

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

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

testfile testfile::countcha(char *t, testfile f1)
{
    int i = 0;
    ifstream myfile;
    myfile.open(t);
    if (!myfile.is_open())
    {
        cout << "文件打开失败" << endl;
    }
    char c;
    myfile >> noskipws;//强制读入空格和换行符
    while (!myfile.eof())
    {
        myfile >> c;
        if (myfile.eof())
            break;//防止最后一个字符输出两次
        i++;
    }
    f1.characters = i;
    myfile.close();
    return f1;
}

testfile testfile::countline(char *t, testfile f1)
{
    ifstream myfile;
    myfile.open(t, ios::in);
    int i = 0;
    string temp;//作为getline参数使用
    if (!myfile.is_open())
    {
        cout << "文件打开失败" << endl;
    }
    while (getline(myfile, temp))
    {        if(temp.empty()) continue;
        i++;
    }
    f1.lines = i;
    myfile.close();
    return f1;
}

2、统计文件的单词总数(对应输出第二行),单词:至少以4个英文字母开头,跟上字母数字符号,单词以分隔符分割,不区分大小

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;
}

3、统计文件的有效行数(对应输出第三行):任何包含非空白字符的行,都需要统计。

InputStream inpStr = new FileInputStream(filePath);
BufferedReader br = new BufferedReader(new InputStreamReader(inpStr));

Pattern blankLinePattern = Pattern.compile(BLANK_LINE_REGEX);
String line = null;
while ((line = br.readLine()) != null) {
    if (blankLinePattern.matcher(line).find()) {
   
        blankLine++;
    }
    allLine++;
}

validLine = allLine-blankLine;
return validLine;

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

       List<String> result = new ArrayList<>();
        List<Map.Entry<String,Integer>> list = new ArrayList<>();
        list.addAll(map.entrySet());
        Collections.sort(list,new Comparator<Map.Entry<String,Integer>>()
        {    //对两个value进行比较
            public int compare(Map.Entry<String,Integer>e1,Map.Entry<String,Integer>e2)
            {
                int re = e2.getValue().compareTo(e1.getValue());
                if(re!=0)
                    return re;
                else
                    return e1.getKey().compareTo(e2.getKey());
            }
        });
        //将比较后的结构加入result中
        for(int i=0;i<map.size();i++)
            result.add(list.get(i).getKey());

 性能改进

本来将统计功能分为字符、行数、单词数、词频等四个函数,但发现分开之后需要进行参数传递和文件重新读入,更繁琐且浪费时间。
改进后将词频列为单独函数容易修

运行截图:

 

心路历程与收获:

之前有学过git的使用,但是没有一直坚持练习导致忘记了很多的操作知识,这让我意识到我应该多去了解一些实用的代码工具。尽管不一定会用到,但当需要使用时,好的工具将会节约大量时间,避免因为工具问题耽误作业工作学习的完成。
同时因为这次作业,对map的使用有了更深的了解,尤其是学习到了优先根据值排序,再对值相同按照key字母排序。当然还有其它一些之前没有完全掌握的知识,也在这次作业中巩固了。
实践的机会有很多,但像计算机专业实践这种有许多优秀助教指导点评和帮助的机会不多,我会珍惜这些机会,在下一次的作业中进行更充分的准备。

原文地址:https://www.cnblogs.com/wangzihaojun/p/14611021.html