2021-03-12

实验任务三:文本文件单词的检索与计数 ——(代码实现)


using namespace std;

class Word
{
public:
	Word() { text[0] = '\0'; }
	int Getlength()const;
	void PlaceVocabulary();
	char text[MaxStrSize];

};

void GetFailure(const Word& pat, int f[])
{
	int j = 0, k = -1;
	f[0] = -1;
	while (j < pat.Getlength() - 1)
		if (k == -1 || pat.text[k] == pat.text[j])
			f[++j] = ++k;
		else
			k = f[k];
}

int KMP_find(Word ob, Word pat, int p = 0)//p是用于下次匹配的开始检索位置
{
	int* f = new int[pat.Getlength()];
	GetFailure(pat, f);
	int i = p, j = 0;
	while (i < ob.Getlength() && j < pat.Getlength() && pat.Getlength() - j <= ob.Getlength() - i)
	{
		if (j == -1 || pat.text[j] == ob.text[i])
		{
			i++;
			j++;
		}
		else
			j = f[j];
	}
	delete[]f;
	if (j < pat.Getlength())
		return -1;
	else
		return i - j;

}
void WordCount()
{
	Word ob, pat;
	char filename[10];
	ifstream infile;
	int i = 0, j, k;
	cout << "Place input flie name:";
	cin >> filename;

	cout << "Place input vocabulary:";
	cin >> pat.text;
	infile.open(filename);
	while (infile)
	{
		infile.getline(ob.text, MaxStrSize);
		k = 0;
		while (k < ob.Getlength() - 1)
		{
			j = KMP_find(ob, pat, k);
			if (j < 0) break;
			else
			{
				i++;
				k = j + ob.Getlength();
			}
		}

	}
	infile.close();
	cout << pat.text << endl;
	cout << "在文本文件:" << filename << "   " << "总共出现" << i << "次" << endl;
}
void WordRetrieval()
{
	Word ob, pat;
	char filename[20];
	int i, j, k, l = 0, m;
	int wz[20];
	ifstream infile;
	cout << "Place text file name:";
	cin >> filename;
	cout << "Place you need retrieval:";
	cin >> pat.text;
	infile.open(filename);
	while (infile)
	{
		infile.getline(ob.text, MaxStrSize);
		l++; k = 0; i = 0;
		while (k < ob.Getlength() - 1)
		{
			j = KMP_find(ob, pat, k);
			if (j < 0) break;
			else
			{
				i++;
				wz[i] = j;
				k = j + ob.Getlength();
			}
		}
		if (i > 0)
		{
			cout << "所在行:" << l << "    出现次数:" << i;
			cout << "在该行出现的位置分别是:";
			for (m = 1; m <= i; m++) cout << wz[m] + 1;
			cout << endl;
		}
	}
	infile.close();

}
void Word::PlaceVocabulary()
{
	cout << "Place input text:" << endl;
	cin >> text;
}

int Word::Getlength() const
{
	int i;
	i = 0;

	while (text[i] != '\0')
	{
		i++;
	}

	return i;
}

void Menu()
{
	cout << "=======================================" << endl;
	cout << "文本文件单词的检索和计数系统" << endl;
	cout << "=======================================" << endl;
	cout << "1.建立文本文件" << endl;
	cout << "2.统计单词出现次数" << endl;
	cout << "3.检索单词" << endl;
	cout << "4.退出" << endl;
	cout << "请输入你的选择<1,2,3,4>:";
}
void CreateText()
{
	Word Text, end;
	char TextName[100];
	end.text[0] = '/';
	end.text[1] = 'e';
	end.text[2] = 'n';
	end.text[3] = 'd';
	end.text[4] = '\0';
	cout << "请输入文件名:";
	cin >> TextName;
	ofstream outfile(TextName, ios::out);
	if (!outfile)
	{
		cerr << "open error!" << endl;
	}
	cout << "请输入文本内容:(写完后用/end结束输入)" << endl;

	while (KMP_find(Text, end) == -1)
	{
		cin >> Text.text;
		if (KMP_find(Text, end) == -1)
			for (int i = 0; i < Text.Getlength(); i++) outfile << Text.text[i];
		outfile << '\n';
	}
	outfile.close();
	cout << "Text create success" << endl;
}
int main()
{

	int i, j = 1;
	do {

		Menu();
		cin >> i;
		switch (i)
		{
		case 1:		CreateText(); break;
		case 2:		WordCount(); break;
		case 3:		WordRetrieval(); break;
		case 4:		j = 0; break;
		default:	cout << "Place error" << endl;
		}

	} while (j);
	return 0;
}

运行结果
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值