zoukankan      html  css  js  c++  java
  • 福大软工1816 · 第二次作业

    一.Github 网址:https://github.com/FZU1816K/personal-project/tree/ae221aac3ea33add2c3db0a6b12637937ab1e95a/Cplusplus/031602312

    二.PSP表格:

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

    三.解题思路:

    对于统计文件的字符个数,单词个数,我的想法是使用getline函数每行读入,对每一行的字符进行单词统计和字符统计。封装接口方面,我打算把实现过程写在函数里实现接口的封装。

    四.设计实验过程:

    函数 功能
    int count_character(string s) 计算字符数
    int count_word(string s) 计算单词数
    void sort_wordcount() | 单词按词频排序
    

    五.

    六.代码说明.

    #include"stdafx.h"
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<string>
    #include<unordered_map>
    #include<iostream>
    #include<vector>
    #include<fstream>
    using namespace std;
    string s;
    int character, word, line;
    unordered_map<string, int>mp;
    vector<pair<int, string> >v;
    bool cmp(pair<int, string> a, pair<int, string> b);//单词按词频排序的函数
    int count_character(string s);//计算字符数
    int count_word(string s);//计算单词数
    void sort_wordcount();//单词按词频排序
    
    int main(int argc, char *argv[])
    {
    ifstream file1;//读入文件
    file1.open(argv[1]);
    if (!file1.is_open()) {
    	cout << "文件打开失败";//异常处理
    	return 0;
    }
    character = 0;//初始化
    word = 0;
    line = 0;
    mp.clear();
    while (getline(file1, s))//按行读入
    {
    	if (s.empty()) continue;;
    	line++;
    	character += count_character(s);
    	word += count_word(s);
    }
    cout << "characters: " << character << endl;
    cout << "words: " << word << endl;
    cout << "lines: " << line << endl;
    sort_wordcount();
    for (int i = 0; i < 10 && i < v.size(); i++)
    {
    	cout << "<" << v[i].second << ">: " << v[i].first << endl;
    }
    return 0;
    }
    
    bool cmp(pair<int, string> a, pair<int, string> b)
    {
    if (a.first != b.first)return a.first > b.first;//优先考虑词频,词频相同考虑字典序
    else return a.second < b.second;
    }
    int count_character(string s)
    {
    return(s.length());//字符个数就是单词长度1
    }
    bool iszimu(char c)//判断是否是字母
    {
    if (c >= 'a'&&c <= 'z') return true;
    else if (c >= 'A'&&c <= 'Z') return true;
    else return false;
    }
    int count_word(string s)
    {
      string word = "";
      int len = s.length(), cnt = 0;
      for (int i = 0; i < len; i++)
      {
      	  if (s[i]==' '||!isdigit(s[i])&&!iszimu(s[i])||i==len-1)//遇到分隔符
    	  {
    		  if (word.length() >= 4 && word[0] != '' && !isdigit(word[0]) && !isdigit(word[1]) && !isdigit(word[2]) && !isdigit(word[3]))//判断是否可以成为单词
    		  {
    			mp[word]++;
    			cnt++;
    		  }
    		  word = "";
    	  }
    	  else if (isdigit(s[i])) word += s[i];//遇到数字直接加入单词
    	  else
    	  {
    		if (s[i] < 'a') s[i] += 32;//遇到大写转化成小写
    		word += s[i];
    	  }
      }
      return cnt;
    }
    void sort_wordcount()
    {
      unordered_map<string, int>::iterator it;
      v.clear();
      for (it = mp.begin(); it != mp.end(); it++)
      {
    	  v.push_back(make_pair(it->second, it->first));
      }
      sort(v.begin(), v.end(), cmp);
    }
    

    七.心路历程:

    这次写代码对我来说可真的是非常大的挑战,我花费的时间远比我预想中的多得多,我得学会使用Github,光会写实现代码远远不够,得学会封装接口,学会vs的使用和项目的创建。对于刚刚使用vs的我来说,真的是一个非常大的挑战。这次项目也让我感觉到自己认识的计算机世界实在太小了,我以前就是写写代码,对项目并没有什么概念,现在我知道了项目是比代码难度更大代码量更大的。而且,我还必须照顾代码格式。总的来说,这次项目对我真的是很大的挑战,出了会写代码,其他对于我来说基本是从0开始,相信过了一段时间,我会越来越熟悉项目的创建还有vs github的使用,希望有一天我会喜欢上软工。

  • 相关阅读:
    从零开始学习html(五)与浏览者交互,表单标签——上
    从零开始学习html(四)认识标签(第三部分)
    从零开始学习html(三) 认识标签(第二部分)
    从零开始学习html(二)认识标签(第一部分)——下
    git 创建管理远程分支
    git分支
    Git 忽略一些文件不加入版本控制
    windows下 git+tortoiseGit的使用【转】
    WebStorm + JetBrains IDE Support 实现自动刷新功能
    Sublime Text 3 安装插件管理 Package Control
  • 原文地址:https://www.cnblogs.com/hhhhh123/p/9637743.html
Copyright © 2011-2022 走看看