基于线性表和二叉排序树的低频词过滤系统

基于线性表和二叉排序树的低频词过滤系统

#include<bits/stdc++.h>
using namespace std;
const int MAX=1000;

struct BSNode{
	string name;
	int count;
	BSNode*lchild,*rchild;
	BSNode(){count=0;lchild=NULL;rchild=NULL;}
	BSNode(string a):name(a){count=1;lchild=NULL;rchild=NULL;}
	BSNode(BSNode* tmp){
		name=tmp->name;
		count=tmp->count;
		lchild=NULL;
		rchild=NULL;
	}
};
void insertNode(BSNode*& root,string a){
	if(root){
		int b=root->name.compare(a);
		if(b==0)root->count++;
		else if(b<0)insertNode(root->lchild,a);
		else insertNode(root->rchild,a);
	}
	else root=new BSNode(a);
}
void insertNode(BSNode*& root,BSNode* tmp){
	if(root){
		int b=root->name.compare(tmp->name);
		if(b==0)root->count++;
		else if(b<0)insertNode(root->lchild,tmp);
		else insertNode(root->rchild,tmp);
	}
	else root=new BSNode(tmp);
}
void delet(BSNode*& root){
	if(root){
		delet(root->lchild);
		delet(root->rchild);
		delete root;
	}
}
void printTree(BSNode* root){
	if(root){
		printTree(root->lchild);
		cout.width(15); cout << root->name;
		cout << "\t\t\t";
		cout.width(4); cout << root->count;
		cout << endl;
		printTree(root->rchild);
	}
}
void update2(BSNode*& root,BSNode*& newRoot){
	if(root){
		if(root->count<=3){
			cout.width(15); cout << root->name;
			cout << "\t\t\t";
			cout.width(4); cout << root->count;
			cout << endl;
		}else insertNode(newRoot,root);
		update2(root->rchild,newRoot);
		update2(root->lchild,newRoot);
	}
}
void outPutTree(BSNode*& root,ofstream& fout){
	if(root){
		outPutTree(root->lchild,fout);
		fout.width(15); fout << root->name;
		fout << "\t\t\t";
		fout.width(4); fout << root->count;
		fout << endl;
		outPutTree(root->rchild,fout);
	}
}

class BSTree{
private:
	BSNode*root;
public:
	BSTree(){root=NULL;}
	~BSTree(){
		delet(root);
	}
	BSNode*& Root(){return root;}
	void upda(BSNode*& rot){
		delet(root);
		root=rot;
	}
};

double continueToFinish2();
void showTime2(double);
void distCount2(BSTree&);
void delLow2(BSTree& T);
void showHigh2(BSTree&);
void showASL2(BSTree& T);
void BSTr()
{
	bool flag = true; int ch;
	double time; 
	BSTree T;
	cout << endl;
	while (flag)
	{
		cout << "*******************\n";
		cout << "* 二叉排序树选择菜单 *\n";
		cout << "*******************\n";
		cout << "1、连续执行至完毕\n2、显示执行时间\n3、单步执行:识别并统计单词\n4、单步执行:删除并显示出现频率低单词\n5、单步执行:输出其余单词及其频率\n6、单步执行:计算并输出ASL值\n7、返回主菜单\n   请选择:";
		cin >> ch;
		switch (ch)
		{
		case 1:time = continueToFinish2(); break;
		case 2:showTime2(time); break;
		case 3:distCount2(T); break;
		case 4:delLow2(T); break;
		case 5:showHigh2(T); break;
		case 6:showASL2(T); break;
		case 7:flag = false; break;
		default:cout << "输入错误,请重新输入!\n"; break;
		}
	}
	cout << "退回主程序\n";
	std::system("Pause");
}
double continueToFinish2()
{
	double start, end;
	start = (double)clock();
	BSTree T;
	distCount2(T);
	delLow2(T);
	showHigh2(T);
	showASL2(T);
	end = (double)clock();
	return end - start;
}
void showTime2(double time)
{
	cout << "共执行了" << time << "ms\n";
}
void distCount2(BSTree& T)
{
	ifstream fin("Infile.txt");
	char c;
	while (!fin.eof())
	{
		string str;
		while ((c = fin.get()) != EOF && ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == 39))
		{
			str.push_back(c);
		}
		if (str != "\0")insertNode(T.Root(),str);
	}
	fin.close();
	cout.width(15); cout << "单词";
	cout << "\t\t\t个数统计\n";
	printTree(T.Root());
}
void delLow2(BSTree& T)
{
	cout << "删除成功!\n";
	cout.width(15); cout << "删除的单词";
	cout << "\t\t\t个数统计\n";
	BSNode* newRoot=NULL;
	update2(T.Root(),newRoot);
	T.upda(newRoot);	
}
void showHigh2(BSTree& T)
{
	ofstream fout("Outfire.txt");
	fout.width(15); fout << "单词";
	fout << "\t\t\t个数统计\n";
	outPutTree(T.Root(),fout);
	fout.close();
}
void calculateASL(BSNode* root, int& s, int& n, int p)
{
	if (root)
	{
		n++; p++; s+=p;
		calculateASL(root->lchild, s, n, p);
		calculateASL(root->rchild, s, n, p);
	}
}
void showASL2(BSTree& T)
{
	int s = 0, n = 0, p = 0;
	calculateASL(T.Root(), s, n, p);
	cout << "ASL=" << s / n << endl;
}


//单词结点
struct word
{
	string name;
	int count;
	word() { count = 0; }
};
//线性表
class SeqList
{
public:
	SeqList() { length = 0; }
	void insert(string a){	
		for(int i=1;i<=length;i++){
			if(Word[i].name.compare(a)==0){
				Word[i].count++;return;
			}
		}
		Word[++length].name=a;
		Word[length].count=1;
	}
	void printList(){
		cout.width(15); cout << "单词";
		cout << "\t\t\t个数统计\n";
		for(int i=1;i<=length;i++){
			cout.width(15); cout << Word[i].name;
			cout << "\t\t\t";
			cout.width(4); cout << Word[i].count;
			cout << endl;
		}
		cout<<endl;
	}
	word* getWord() { return Word; }
	int getLength() { return length; }
	void update1(word* high,int count){
		for(int i=1;i<=count;i++){
			Word[i]=high[i];
		}
		length=count;
	}
	void outPutList(ofstream& fout){
		fout.width(15); fout << "单词";
		fout << "\t\t\t个数统计\n";
		for(int i=1;i<=length;i++){
			fout.width(15); fout << Word[i].name;
			fout << "\t\t\t";
			fout.width(4); fout << Word[i].count;
			fout<<endl;
		}
		fout << endl;	
	}
private:
	word Word[MAX];
	int length;
};
void distCount1(SeqList&);
void showTime1(double);
void delLow1(SeqList&);
void showHigh1(SeqList&);
void showASL1(SeqList&);
double continueToFinish1();
void List(){
	bool flag = true; int ch;
	double time; SeqList S;
	cout << endl;
	while (flag){
		cout << "*******************\n";
		cout << "*  线性表选择菜单  *\n";
		cout << "*******************\n";
		cout << "1、连续执行至完毕\n2、显示执行时间\n3、单步执行:识别并统计单词\n4、单步执行:删除并显示出现频率低单词\n5、单步执行:输出其余单词及其频率\n6、单步执行:计算并输出ASL值\n7、返回主菜单\n   请选择:";
		cin >> ch;
		switch (ch){
		case 1:time=continueToFinish1(); break;
		case 2:showTime1(time); break;
		case 3:distCount1(S); break;
		case 4:delLow1(S); break;
		case 5:showHigh1(S); break;
		case 6:showASL1(S); break;
		case 7:flag = false; break;
		default:cout << "输入错误,请重新输入!\n"; break;
		}
	}
	cout << "退回主程序\n";
	system("Pause");
}

double continueToFinish1(){
	double start, end;
	start = (double)clock();
	SeqList S;
	distCount1(S);
	delLow1(S);
	showHigh1(S);
	showASL1(S);
	end = (double)clock();
	return end - start;
}
void showTime1(double time){
	cout << "共执行了" << time << "ms\n";
}
void distCount1(SeqList& S){
	ifstream fin("Infile.txt");
	char c;
	while (!fin.eof()){
		string str;
		while ((c = fin.get()) != EOF && ((c>='0'&&c<='9')||(c>='A'&&c<='Z')||(c>='a'&&c<='z')||c==39))
			str.push_back(c);
		if (str != "\0")S.insert(str);
	}
	fin.close();
	S.printList();
}
void delLow1(SeqList& S){
	word high[1000];
	int count = 0;
	int i;
	cout << "删除成功!\n";
	cout.width(15); cout << "删除的单词";
	cout << "\t\t\t个数统计\n";
	for (i = 1; i <= S.getLength(); i++){
		word tmp=S.getWord()[i];
		if (tmp.count <= 3){
			cout.width(15); cout << tmp.name;
			cout << "\t\t\t";
			cout.width(4); cout << tmp.count;
			cout << endl;
		}
		else{
			high[++count] = tmp;
		}
	}
	S.update1(high, count);
}
void showHigh1(SeqList& S){
	ofstream fout("Outfire.txt");
	S.outPutList(fout);
	fout.close();
}
void showASL1(SeqList& S){
	cout << "ASL=" << (1 + S.getLength()) / 2;
	cout << endl;
}
int main(){
	bool flag = true; int ch;
	while (flag){
		system("cls");
		cout << "*******************\n";
		cout << "*      主菜单      *\n";
		cout << "*******************\n";
		cout << "1、线性表\n2、二叉排序树\n3、退出程序\n   请选择:";
		cin >> ch;
		switch (ch){
		case 1:List(); break;
		case 2:BSTr(); break;
		case 3:flag = false; break;
		default:cout << "输入错误,请重新输入!\n";
		}
	}
	cout << "退出程序!\n";
	system("Pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值