软工实践第二次作业

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 1110 1690
• Estimate • 估计这个任务需要多少时间 1110 1690
Development 开发 900 1445
• Analysis • 需求分析 (包括学习新技术) 360 600
• Design Spec • 生成设计文档 30 20
• Design Review • 设计复审 30 10
• Coding Standard • 代码规范 (为目前的开发制定合适的规范) 30 15
• Design • 具体设计 30 20
• Coding • 具体编码 120 240
• Code Review • 代码复审 120 120
• Test • 测试(自我测试,修改代码,提交修改) 180 420
Reporting 报告 210 245
• Test Repor • 测试报告 120 150
• Size Measurement • 计算工作量 30 15
• Postmortem & Process Improvement Plan • 事后总结, 并提出过程改进计划 60 80
summary 合计 1110 1690
  • 解题思路描述
    • 设计一个File类,其私有变量为文件名name,字符个数Char,单词个数Word,行数Line,以及存放词频的pair数组str[]。
    • File类含有公有函数为计算字符数CharCount(),计算单词数WordCount(),计算行数LineCount(),计算词频WordSeq(),写文件函数writeTxt()。
  • 设计实现过程
    • 读文件:通过ifstream将txt读进缓冲流。
    • 计算字符数个数:通过ifstream>>noskipws将输入流中字符一个一个读取,通过计算读取次数得到字符个数。
    • 计算单词个数:编写单词判别函数isword(),用于判断该字符串是不是单词。将字符一个一个加入string,遇到空白字符则停止,然后将得到的string输入isword()判断它是不是单词。
    • 计算单词频数:与计算单词个数同样的方法,如果它是单词则将其加入map中,单词的字符串作为key,单词出现的次数作为对应的value。利用sort()对其进行排序得到前10个词频。
    • 计算行数:编写Delete()函数,去掉每行中的空格和制表符。用getline()逐行读取,计算非空白行。
  • 改进思路
    • 计算字符个数时逐个读取字符,可以改成将所有字符存入string中,利用string的length()读出字符个数。
    • 计算词频时,所用数据结构不合适造成代码冗余。
  • 代码说明
DLL_API int charCount(string fname)
{
	ifstream infile;
	infile.open(fname, ifstream::in);
	if (!infile.is_open())
		cout << "open error!" << endl;

	char c;
	int Char = 0;
	infile >> noskipws;

	while (infile.peek() != EOF)
	{
		infile >> c;
		Char++;
//		cout << Char << endl;
	}
	return Char;
}
DLL_API int wordCount(string fname)
{
	ifstream infile;
	infile.open(fname, ifstream::in);
	if (!infile.is_open())
		cout << "open error!" << endl;

	char c;
	int Word=0;
	infile >> noskipws;

	infile.clear();
	infile.seekg(0);

	while (infile.peek() != EOF)
	{
		string w;
		infile >> c;
		while ((c != ' ') && (c != '\t') && (c != '\n'))
		{
			if (c >= 65 && c <= 90)   //大写转小写
				c = c + 32;

			w = w + c;				//越界
			infile >> c;
		}
		if (isWord(w))  //w是单词,将其加入map
			Word++;
	}
	return Word;
}
DLL_API void wordSeq(string fname,pair<string,int> str_cnt[])
{
	typedef pair<string, int> PAIR;
	vector<PAIR> vec;
	map<string, int> cnt;
	map<string, int>::iterator curr;
	vector<PAIR>::iterator it;

	ifstream infile;
	infile.open(fname, ifstream::in);
	if (!infile.is_open())
		cout << "open error!" << endl;

	char c;
	int i=0;
	infile >> noskipws;

	infile.clear();
	infile.seekg(0);

	while (infile.peek() != EOF)
	{
		string w;
		infile >> c;
		while ((c != ' ') && (c != '\t') && (c != '\n'))
		{
			if (c >= 65 && c <= 90)   //大写转小写
				c = c + 32;

			w = w + c;				//越界
			infile >> c;
		}
		if (isWord(w))  //w是单词,将其加入map
		{
			if (cnt.count(w) == 0)
			{
				cnt[w] = 1;
			}
			else
			{
				cnt[w]++;
			}
		}
	}
  • 收获
    • 通过这次作业学到了很多东西。本来不会的读文件写文件操作,现在用上了。把类封装成扩展文件的技能也get到了。只是自己知识面太狭窄了,作业中提出的要求有些还不知道它指的是什么,而且在调试和封装过程中遇到了许多问题,调试封装的过程中查了许多文档看了很多博客最终才实现了。算起来调试封装和百度花的时间比真正编程的时间要多得多。总的来说还是学到了很多,也意识到了还有很多需要学习的东西,还需要不断积累。

posted on 2018-09-12 22:53  code-123  阅读(149)  评论(2编辑  收藏  举报

导航