第4题:设计一个保留字(或关键字)的统计程序

 建立保留字文件;
 从源数据文件(C或C++语言程序)中,读取字符或字符串,与保留字文件中的保留字进行匹配比较,并统计计数。
 输出两张表文件:保留字计数,扫描程序的次数,非保留字计数。

借鉴原作者

原文章

修改后(原作者有一定的代码冗余)

baoliu.h
#ifndef BAOLIU
#define BAOLIU

#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;
};
#endif
baoliu.cpp
#include "baoliu.h"
#include <iostream>

using namespace std;

void keyWord::display() {
	for (int i = 0; i < keynum; i++) {
		cout << keyword[i] << endl;
	}
}

void keyWord::search() {
	fstream fie;
	fie.open(filename + ".txt");
	ofstream fiet("keywords.txt");

	char str[2000];
	int flag = 0;

	for (int w = 0; 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] == '!') {
					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[w]) {
						keycount++;
						flag = i + 1;
					}
					else {
						nokeycount++;
						flag = i + 1;
					}
					delete[] word;
				}
			}
			time++;
			flag = 0;
		}
		fie.clear();
		fie.seekg(0, ios::beg);
	}
	key /= (keynum + 1);

	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];
	}
	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() {}
main.cpp
#include <iostream>
#include "baoliu.h"
#include <fstream>
using namespace std;

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("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;
}
  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值