求C++期末课程设计

第4题:设计一个保留字(或关键字)的统计程序
1.建立保留字文件;
2.从源数据文件(C或C++语言程序)中,读取字符或字符串,与保留字文件中的保留字进行匹配比较,并统计计数。
3.输出两张表文件:保留字计数,扫描程序的次数,非保留字计数

#include<iostream>
#include<cstring>
#include<fstream>
#include<string>
#define max 10

using namespace std;
class keyWord
{
public:
	keyWord();
	void setData(string *keyw, string filen, long key, long kcon, long nkcon, int keyn, int t);
	void search();
	void display();
	~keyWord();

private:
	string keyword[max];
	string filename;
	long key;
	long keycount;
	long nokeycount;
	int keynum;
	int time;
};

void keyWord::display() {
	for (int i = 0; i < keynum; i++) {
		cout << keyword[i] << endl;
	}
}
void keyWord::search() {
	fstream fie;
	fie.open("C:\\Users\\24498\\Desktop\\" + filename + ".txt");
	ofstream fiet("C:\\Users\\24498\\Desktop\\keywords.txt");

	char str[2000];
	int flag = 0;

	while (fie.getline(str, 2000))
	{
		for (int i = 0; i < strlen(str); i++) {
			if (str[i] == ' ' || str[i] == '.' || str[i] == ',' || str[i] == '?' || str[i] == '!') {
				key++;
				char* word = new char[i + 1 - flag];
				for (int j = flag; j < i; j++) {
					word[j - flag] = str[j];
				}
				word[i - flag] = '\0';
				string st = string(word);

				if (st == keyword[0]) {
					keycount++;
					flag = i + 1;
				}
				else {
					nokeycount++;
					flag = i + 1;
				}
				delete[]word;

			}
		}
		time++;
		flag = 0;

	};
	fie.clear();
	fie.seekg(0, ios::beg);
	nokeycount = nokeycount + keycount;

	for (int w = 1; w < keynum; w++) {

		while (fie.getline(str, 2000))
		{
			for (int i = 0; i < strlen(str); i++) {
				if (str[i] == ' ' || str[i] == '.' || str[i] == ',' || str[i] == '?' || str[i] == '!') {

					char* word = new char[i + 1 - flag];
					for (int j = flag; j < i; j++) {
						word[j - flag] = str[j];
					}
					word[i - flag] = '\0';
					string st = string(word);

					if (st == keyword[w]) {
						keycount++;
						flag = i + 1;
					}
					else {

						flag = i + 1;
					}
					delete[]word;

				}
			}

			time++;
			flag = 0;


		}
		fie.clear();
		fie.seekg(0, ios::beg);
	}

	nokeycount = nokeycount - keycount;
	cout << "一共有" << key << "个单词" << endl;
	cout << "其中有" << keycount << "个单词匹配" << ",有" << nokeycount << "个单词不匹配。" << "一共扫描" << time << "次文件";
	fiet << "关键字出现次数:" << keycount << "  非关键字出现次数 " << nokeycount << "一共扫描" << time << "次文件" << endl;



}
void keyWord::setData(string *keyw, string filen, long keyo, long kcon, long nkon, int keyn, int ti) {
	for (int i = 0; i < keyn; i++) {
		keyword[i] = keyw[i];

		//keyw++;
	}
	filename = filen;
	key = keyo;
	keycount = kcon;
	nokeycount = nkon;
	keynum = keyn;
	time = ti;
}
keyWord::keyWord()
{
	keyword[0] = "xxx";
	filename = "xxx";
	keycount = 0;
	nokeycount = 0;
	time = 0;
}

keyWord::~keyWord()
{
}
int main() {
	string filen;

	int k;
	int time = 0;
	cout << "这是一个统计文件中关键字的程序" << endl;
	cout << "输入要匹配的文件名:";
	cin >> filen;
	cout << "输入要查找的关键字个数(最大为10):";
	cin >> k;

	string* keywod = new string[k];
	for (int i = 0; i < k; i++) {
		cout << "输入第" << i + 1 << "个关键字" << endl;
		cin >> keywod[i];
	}
	ofstream fot("E:\\c plus content\\shixun\\keyword\\keyfile.txt", ios::in);
	for (int i = 0; i < k; i++) {
		fot << keywod[i] << "  ";
	}
	keyWord KeySearch;
	KeySearch.setData(keywod, filen, 0, 0, 0, k, time);
	KeySearch.search();
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吾酒慰风尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值