202103226-1 编程作业

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/computer-science-class1-2018
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-science-class1-2018/homework/11877
这个作业的目标 使用github
学号 20188397

github地址

https://gitee.com/lxy1060951450/project-java

代码规范链接

https://gitee.com/lxy1060951450/project-java/blob/master/代码规范

PSP

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

解题思路描述

先从要求实现的主要功能入手,有几个功能就可以分成几个函数。
然后可以定义一个属于输入文件的file类,将word定义为一个单独的类,将单词名与单词出现次数增加在一起,通过vector容器来进行存储一个文件中的出现的单词,在文件统计各项的函数中,使用到了fstream文件流类进行读取与输出,同时对读取字符进行各种情况下的判断以进行不同的操作。

设计与实现过程

先定义类,用来定义各种信息

	class WordCount
	{
	public:
	WordCount()
	{
		this->fileName = "";
		this->characterNum = this->wordNum = 0;
	}
	WordCount(string s)
	{
		this->fileName = s;
		this->characterNum = this->wordNum = 0;
	}
	int Countchar(fstream& in, fstream &out);
	int Countword(fstream& in, fstream &out);
	int Countline(fstream& in, fstream &out);
	void Sortmap(fstream &out);
	string fileName;
	int characterNum;
	int wordNum;
	map<string, int> wMap;
	};

关键代码

        in.unsetf(ios_base::skipws);//设置不跳过换行符和空白符
	in >> tempCh;
	while (!in.eof())
	{
		totalCount++;
		in >> tempCh;
	}
	//通过find函数判断单词是否在map容器中出现过,若出现过则增加迭代器返回的指针的值既单词次数即可,未出现过则加入新的元素。
        map<string, int>::iterator it = this->wMap.find(wordString);
	if (it != this->wMap.end())
	{
			it->second++;
	}
	else
	{
			this->wMap.insert(pair<string, int>(wordString, 1));
	}
	wordString = "";

约束单词

        in >> temp;
	if (temp <= 'Z'&&temp >= 'A')//如果字符为大写字母将其转换为小写字母
	{
		temp += 32;
	}
	if (!(isalpha(temp)||isdigit(temp)))//判断是否遇到分割符,是则执行以下条件
	{
		if (wordString.length() < 4)//如果单词长度小于4,则直接跳过并清除字符串内容
		{
			wordString = "";
			continue;
		}
		for (int i = 0;i < 4;i++)
		{
			if (!isalpha(wordString[i]))//判断字符串前四位是否为字母,不为字母则直接结束循环
			{
				wordString = "";
				isWord = false;
				break;
			}
		}
		if (!isWord)//如果不为单词则继续执行下一个while循环,不执行以下代码
		{
			isWord = true;
			continue;
	        }

单元测试

异常处理说明

文件无法正常打开时的判断

        if (!in.is_open())
	{
		cout << "无法打开文件:"<<argv[1] << endl;
		exit(0);
	}

心路历程与收获

还需要更多的学习才行

posted @ 2021-04-02 12:49  Teriri!  阅读(60)  评论(0)    收藏  举报