2021-06-25

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

一、 问题概述

1.1 实验内容

对于一篇给定的英文文章,利用线性表来实现单词频率的统计,以及实现低频词的过滤。

1.2 实验目的

读取英文文章文件(Infile.txt),识别其中的单词。利用线性表构建单词的存储结构。当识别出一个单词后,若线性表中没有该单词,则在适当的位置上添加该单词;若该单词已经被识别,则增加其出现的频率。
统计结束后,删除出现频率低于五次的单词,并显示该单词和其出现频率。其余单词及其出现频率按照从高到低的次序输出到文件中(Outfile.txt),同时输出用两种方法完成该工作所用的时间。
计算查找表的ASL值。

1.3 实验要求

1.3.1 文档下载

		My father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and cursing the day of his audition and he did not want to be around that type of environment. 
	Occasionally, Dad would get out his mandolin and play for the family. We three children: Trisha, Monte and I, George Jr., would often sing along. Songs such as the Tennessee Waltz, Harbor Lights and around Christmas time, the well-known rendition of Silver Bells. "Silver Bells, Silver Bells, its Christmas time in the city" would ring throughout the house. One of Dad's favorite hymns was "The Old Rugged Cross". We learned the words to the hymn when we were very young, and would sing it with Dad when he would play and sing. Another song that was often shared in our house was a song that accompanied the Walt Disney series: Davey Crockett. Dad only had to hear the song twice before he learned it well enough to play it. "Davey, Davey Crockett, King of the Wild Frontier" was a favorite song for the family. He knew we enjoyed the song and the program and would often get out the mandolin after the program was over. I could never get over how he could play the songs so well after only hearing them a few times. I loved to sing, but I never learned how to play the mandolin. This is something I regret to this day. 
	
	  Dad loved to play the mandolin for his family he knew we enjoyed singing, and hearing him play. He was like that. If he could give pleasure to others, he would, especially his family. He was always there, sacrificing his time and efforts to see that his family had enough in their life. I had to mature into a man and have children of my own before I realized how much he had sacrificed. 
	
	  I joined the United States Air Force in January of 1962. Whenever I would come home on leave, I would ask Dad to play the mandolin. Nobody played the mandolin like my father. He could touch your soul with the tones that came out of that old mandolin. He seemed to shine when he was playing. You could see his pride in his ability to play so well for his family. 
	
	  When Dad was younger, he worked for his father on the farm. His father was a farmer and sharecropped a farm for the man who owned the property. In 1950, our family moved from the farm. Dad had gained employment at the local limestone quarry. When the quarry closed in August of 1957, he had to seek other employment. He worked for Owens Yacht Company in Dundalk, Maryland and for Todd Steel in Point of Rocks, Maryland. While working at Todd Steel, he was involved in an accident. His job was to roll angle iron onto a conveyor so that the welders farther up the production line would have it to complete their job. On this particular day Dad got the third index finger of his left hand mashed between two pieces of steel. The doctor who operated on the finger could not save it, and Dad ended up having the tip of the finger amputated. He didn't lose enough of the finger where it would stop him picking up anything, but it did impact his ability to play the mandolin. 
	
	  After the accident, Dad was reluctant to play the mandolin. He felt that he could not play as well as he had before the accident. When I came home on leave and asked him to play he would make excuses for why he couldn't play. Eventually, we would wear him down and he would say "Okay, but remember, I can't hold down on the strings the way I used to" or "Since the accident to this finger I can't play as good". For the family it didn't make any difference that Dad couldn't play as well. We were just glad that he would play. When he played the old mandolin it would carry us back to a cheerful, happier time in our lives. "Davey, Davey Crockett, King of the Wild Frontier", would again be heard in the little town of Bakerton, West Virginia. 
	
	  In August of 1993 my father was diagnosed with inoperable lung cancer. He chose not to receive chemotherapy treatments so that he could live out the rest of his life in dignity. About a week before his death, we asked Dad if he would play the mandolin for us. He made excuses but said "okay". He knew it would probably be the last time he would play for us. He tuned up the old mandolin and played a few notes. When I looked around, there was not a dry eye in the family. We saw before us a quiet humble man with an inner strength that comes from knowing God, and living with him in one's life. Dad would never play the mandolin for us again. We felt at the time that he wouldn't have enough strength to play, and that makes the memory of that day even stronger. Dad was doing something he had done all his life, giving. As sick as he was, he was still pleasing others. Dad sure could play that Mandolin! 
在统计的过程中,分词时可以利用空格或者标点符号作为划分单词依据,文章中默认只包含英文单词和标点符号。对单词进行排序时,是按照字母序进行的,每个结点还应包含该单词出现的频率

二、程序设计

2.1 线性表算法设计

2.1.1 线性表数据结构

/*单链表结点*/
typedef struct Node_1
{
	char Data[20];
	int Count;
	struct Node_1 *Next;
}LNode, *LinkList;

2.1.2 元素入表

void Insert_Liner(LinkList &l, char *array)
{
	int falg = 0;
	LinkList par;
	LinkList qar;
	qar = l->Next;
	while (qar != nullptr)
	{
		if (stricmp(array, qar->Data) == 0)
		{
			qar->Count++;
			falg = 1;
			break;
		}
		qar = qar->Next;
	}
	if (falg == 0)
	{
		par = (LinkList)malloc(sizeof(LNode));
		strcpy(par->Data, array);
		par->Count = 1;
		par->Next = l->Next;
		l->Next = par;

	}
}

2.1.3 交换元素

void Exchange(LinkList par, LinkList qar)
{
	char array[20];
	int temp;
	strcpy(array, par->Data);
	temp = par->Count;


	strcpy(par->Data, qar->Data);
	par->Count = qar->Count;

	strcpy(qar->Data, array);
	qar->Count = temp;
}

2.1.4 排序

void Sort(LinkList &l)
{
	LinkList par, qar, temp;
	par = l->Next;
	while (par)
	{
		qar = par->Next;
		while (qar)
		{
			if (par->Count < qar->Count)
			{
				Exchange(par, qar);
			}
			else
			{
				qar = qar->Next;
			}
		}
		par = par->Next;
	}

}

2.1.5 输出频率

void Output_Frequency(LinkList &l)
{
	LinkList par;
	par = l->Next;
	cout << "  " << "单词" << "         " << "个数统计" << endl;

	while (par != nullptr)
	{
		printf("    %-20s", par->Data);
		cout << "  ";
		cout << par->Count << endl;
		par = par->Next;

	}
}

2.1.6 识别统计

void Identification_Statistics()
{
	FILE *input;
	char array[20], a;
	int i;
	LinkList l;

	/*建立空的链表*/
	InitList(l);

	/*将文件放到项目目录下,并打开*/
	input = fopen("Infile.txt", "r");

	while (!feof(input))
	{
		i = 0;

		/*作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法*/
		memset(array, 0, sizeof(array));    /*内存初始化*/
		while ((a = fgetc(input)) != EOF && (a == ',' || a == '.' || a == '!' || a == '?' || a == ' ' || a == '(' || a == ')' || a == '\n') == 0)
		{
			array[i++] = a;
		}

		if (array[0])
		{
			Insert_Liner(l, array);
		}
	}

	Sort(l);
	Output_Frequency(l);
	fclose(input);
}

2.1.7 删除

void Delete()
{
	int i;
	FILE *input, *out;
	LinkList l, par, qar;
	char array[20], a;

	/*建立空的链表*/
	InitList(l);

	input = fopen("Infile.txt", "r");

	while (!feof(input))
	{
		i = 0;

		/*作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法*/
		memset(array, 0, sizeof(array));    /*内存初始化*/
		while ((a = fgetc(input)) != EOF && (a == ',' || a == '.' || a == '!' || a == '?' || a == ' ' || a == '(' || a == ')' || a == '\n') == 0)
		{
			array[i++] = a;
		}

		if (array[0])
		{
			Insert_Liner(l, array);
		}
	}

	Sort(l);
	fclose(input);

	cout << "删除低频词汇!" << endl;

	/*输出文件*/
	out = fopen("Infile_1.txt", "w+");

	par = l->Next;
	while (par != nullptr && par->Count >= 5)
	{
		par = par->Next;

	}

	while (par != nullptr)
	{
		qar = par;
		par = par->Next;
		printf("删除节点: %-20s", qar->Data);
		cout << " ";
		cout << qar->Count << endl;

		/*释放空间,删除结点*/
		free(qar);
	}

	fclose(out);

}

2.1.8 输出高频词汇

void Output_VocabularyAndfrequency()
{
	int i, j = 1;
	FILE *input, *out;
	LinkList l, par, qar;
	char array[20], a;

	/*建立空的链表*/
	InitList(l);

	input = fopen("Infile.txt", "r");

	while (!feof(input))
	{
		i = 0;

		/*作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法*/
		memset(array, 0, sizeof(array));    /*内存初始化*/
		while ((a = fgetc(input)) != EOF && (a == ',' || a == '.' || a == '!' || a == '?' || a == ' ' || a == '(' || a == ')' || a == '\n') == 0)
		{
			array[i++] = a;
		}

		if (array[0])
		{
			Insert_Liner(l, array);
		}
	}

	Sort(l);

	/*关闭文件*/
	fclose(input);


	/*输出文件*/
	out = fopen("Infile_1.txt", "w+");

	par = l->Next;

	cout << "删除低频率单词后\n单词       个数统计\n";

	while (par&&par->Count >= 5)
	{
		printf("%-20s", par->Data);
		cout << "  ";
		cout << par->Count << endl;

		fprintf(out, "%s(%d)\t", par->Data, par->Count);
		par = par->Next;

	}
	cout << "写入文件f2.txt成功" << endl;
	fclose(out);//关闭文件
}

2.1.9 计算ASL

void ASL()
{
	FILE *in;
	int sum = 0;
	char array[20], a;
	LinkList l, par;
	InitList(l);
	in = fopen("Infile.txt", "r");//打开输入文件

	double ASL;
	while (!feof(in))//直到碰见文件结束符结束循环
	{
		int i = 0;
		/*作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法*/
		memset(array, 0, sizeof(array));    /*内存初始化*/
		while ((a = fgetc(in)) != EOF && (a == ',' || a == '.' || a == '!' || a == '?' || a == ' ' || a == '(' || a == ')' || a == '\n') == 0)
		{
			array[i++] = a;
		}

		if (array[0])
		{
			Insert_Liner(l, array);
		}

	}

	par = l->Next;

	while (par)
	{
		sum++;
		par = par->Next;

	}
	cout << "输出单词总数" << sum << endl;
	fclose(in);

	ASL = (double)(sum + 1) / 2.0;
	cout << "ASL=" << ASL << endl;
}

2.1.10 菜单函数

void menu()
{
	cout << ("1、连续执行至完毕\n") << endl;
	cout << ("2、显示执行时间\n") << endl;
	cout << ("3、单步执行:识别并统计单词\n") << endl;
	cout << ("4、单步执行:删除并显示出现频率低单词\n") << endl;
	cout << ("5、单步执行:输出其余单词及其频率\n") << endl;
	cout << ("6、单步执行计算并输出ASL值\n") << endl;
	cout << ("7、返回主菜单\n") << endl;
	cout << ("请选择你需要的服务,输入数字(1-7)\n") << endl;
}

2.1.11主体函数

void linear()
{

	int choose;
	menu();
	cin >> choose;


	switch (choose)
	{
	case 1:
		AllFinish();
		linear();
		break;
	case 2:
		Calculation_time();
		linear();
		break;
	case 3:
		Identification_Statistics();
		linear();
		break;
	case 4:
		Delete();
		linear();
		break;
	case 5:
		Output_VocabularyAndfrequency();
		linear();
		break;
	case 6:
		ASL();
		linear();
		break;
	case 7:
		break;
	}
}

2.1.2 计算时间

void Calculation_time()
{
	double star, finish, time;
	star = (double)clock();//获取当前时间

	Identification_Statistics();//统计
	Delete();
	Output_VocabularyAndfrequency();
	ASL();

	finish = (double)clock();//获取结束时间
	time = finish - star;
	cout << "执行时间" << time << endl;
}

2.1.2 执行

void AllFinish()
{
	Identification_Statistics();//统计
	Delete();
	Output_VocabularyAndfrequency();
	ASL();
}

2.2 二叉排序树算法设计

2.2.1 插入算法

void Insert_BiTree(BiTree &T, char *array)
{
	if (T == nullptr)
	{
		T = BiTree(malloc(sizeof(TNode)));
		strcpy(T->Data, array);
		T->lchild = nullptr;
		T->rchild = nullptr;
		T->Count = 1;
	}
	else
	{
		if (stricmp(array, T->Data) < 0)
		{
			Insert_BiTree(T->lchild, array);
		}
		else if (stricmp(array, T->Data) > 0)
		{
			Insert_BiTree(T->rchild, array);
		}
		else
		{
			T->Count++;
		}


	}
}

2.2.2 中序遍历

/*中序遍历*/
void InOrder_BiTree(BiTree T)
{
	if (T)
	{
		InOrder_BiTree(T->lchild);
		printf("%-20s            ", T->Data);
		cout << T->Count << endl;
		InOrder_BiTree(T->rchild);
	}

}
/*中序遍历二叉排序树*/
void New_Insert_BiTree(BiTree &T, BiTree P)
{
	if (T == nullptr)
	{
		T = (BiTree)(malloc(sizeof(TNode)));
		strcpy(T->Data, P->Data);
		T->Count = P->Count;
		T->lchild = nullptr;
		T->rchild = nullptr;


	}
	else
	{
		if (stricmp(P->Data, T->Data) < 0)
		{
			New_Insert_BiTree(T->lchild, P);
		}
		else
		{
			New_Insert_BiTree(T->rchild, P);
		}
	}
}


/*中序遍历二叉排序树*/
void New_InOrder_BiTree(BiTree T)
{
	
	if (T != nullptr)
	{
		New_InOrder_BiTree(T->lchild);
		if (T->Count < 5)
		{
			New_Insert_BiTree(P, T);
		}

		New_InOrder_BiTree(T->rchild);
	}
}


/*中序遍历二叉排序树的到有序*/
void New_InOrder_BiTree2(BiTree T)
{
	
	if (T != nullptr)
	{
		New_InOrder_BiTree2(T->lchild);
		if (T->Count >=5)
		{
			New_Insert_BiTree(P, T);
		}

		New_InOrder_BiTree2(T->rchild);
	}
}

2.2.3 读取文件

/*读取文件*/
void Read_FIlE()
{
	FILE *in;
	T = nullptr;
	in = fopen("Infile.txt", "r");
	char array[20], a;
	while (!feof(in))
	{
		int i = 0;
		/*作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法*/
		memset(array, 0, sizeof(array));    /*内存初始化*/
		while ((a = fgetc(in)) != EOF && (a == ',' || a == '.' || a == '!' || a == '?' || a == ' ' || a == '(' || a == ')' || a == '\n') == 0)
		{
			array[i++] = a;
		}
		if (array[0])
		{
			Insert_BiTree(T, array);
		}
	}
}

2.2.4 识别统计

/*识别统计单词*/
void Identification_Statistics_BiTree()
{
	Read_FIlE();

	cout << "单词                         个数统计" << endl;
	InOrder_BiTree(T);

}

2.2.5 删除

/*删除低频词汇*/
void Delete_BiTree()
{
	Read_FIlE();

	cout << "删除频率小于5的二叉排序树中序遍历" << endl;
	P = nullptr;
	New_InOrder_BiTree(T);
	cout << "单词                      个数统计" << endl;
	InOrder_BiTree(P);


}

2.2.6 输出

/*输出*/
void Output_BiTree()
{
	Read_FIlE();

	cout << "显示频率大于5的二叉排序树中序遍历" << endl;
	P = nullptr;
	New_InOrder_BiTree(T);
	cout << "单词                      个数统计" << endl;
	InOrder_BiTree(P);
}

2.2.7 执行

/*全部执行*/
void AllFinish_BiTree()
{
	Identification_Statistics_BiTree();
	Delete_BiTree();
	Output_BiTree();

}

2.2.8 输出执行时间

/*输出执行时间*/
void Calculation_time_BiTree()
{
	double star, finish;
	star = (double)clock();//获取当前时间
	
	AllFinish();

	finish = (double)clock();//获取结束时间
	cout<< (finish - star) << endl;
}

2.2.9 菜单函数

/*菜单函数*/
void BiTree_menu()
{
	cout << ("1、连续执行至完毕\n") << endl;
	cout << ("2、显示执行时间\n") << endl;
	cout << ("3、单步执行:识别并统计单词\n") << endl;
	cout << ("4、单步执行:删除并显示出现频率低单词\n") << endl;
	cout << ("5、单步执行:输出其余单词及其频率\n") << endl;
	cout << ("6、单步执行计算并输出ASL值\n") << endl;
	cout << ("7、返回主菜单\n") << endl;
	cout << ("请选择你需要的服务,输入数字(1-7)\n") << endl;
}

2.2.10 主体函数

void Tree()
{
	int choose;
	BiTree_menu();
	cin >> choose;
	

	switch (choose)
	{
	case 1:
		AllFinish_BiTree();
		Tree();
		break;
	case 2:
		Calculation_time_BiTree();
		Tree();
		break;
	case 3:
		Identification_Statistics_BiTree();
		Tree();
		break;
	case 4:
		Delete_BiTree();
		Tree();
		break;
	case 5:
		Output_BiTree();
		Tree();
		break;
	case 6:
		Tree();
		break;
	case 7:
		break;
	}

}

三、源代码

3.1 Node.h(头文件)

#pragma once


/*单链表结点*/
typedef struct Node_1
{
	char Data[20];
	int Count;
	struct Node_1 *Next;
}LNode, *LinkList;


/*二叉树结点*/
typedef struct Node_2
{
	char Data[20];
	int Count;
	struct Node_2 *lchild, *rchild;
}TNode, *BiTree;

BiTree T, P;



3.2 LinkList.h (头文件)

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996) 

#include<fstream>
#include<iostream>
#include<time.h>
#include<malloc.h>
#include<cstring>
#include "Node.h"

using namespace std;

/***************************************************************************************/

/*1.线性表初始化*/
void InitList(LinkList &l)
{
	l = (LinkList)malloc(sizeof(LNode));
	l->Next = nullptr;

}

/*2.将元素放入线性表中*/
void Insert_Liner(LinkList &l, char *array)
{
	int falg = 0;
	LinkList par;
	LinkList qar;
	qar = l->Next;
	while (qar != nullptr)
	{
		if (stricmp(array, qar->Data) == 0)
		{
			qar->Count++;
			falg = 1;
			break;
		}
		qar = qar->Next;
	}
	if (falg == 0)
	{
		par = (LinkList)malloc(sizeof(LNode));
		strcpy(par->Data, array);
		par->Count = 1;
		par->Next = l->Next;
		l->Next = par;

	}
}


/*3.交换元素(大的在左边,小的在右边)*/
void Exchange(LinkList par, LinkList qar)
{
	char array[20];
	int temp;
	strcpy(array, par->Data);
	temp = par->Count;


	strcpy(par->Data, qar->Data);
	par->Count = qar->Count;

	strcpy(qar->Data, array);
	qar->Count = temp;
}

/*4.比较出现次数,交换*/
void Sort(LinkList &l)
{
	LinkList par, qar, temp;
	par = l->Next;
	while (par)
	{
		qar = par->Next;
		while (qar)
		{
			if (par->Count < qar->Count)
			{
				Exchange(par, qar);
			}
			else
			{
				qar = qar->Next;
			}
		}
		par = par->Next;
	}

}

/*5.输出单词出现频率*/
void Output_Frequency(LinkList &l)
{
	LinkList par;
	par = l->Next;
	cout << "  " << "单词" << "         " << "个数统计" << endl;

	while (par != nullptr)
	{
		printf("    %-20s", par->Data);
		cout << "  ";
		cout << par->Count << endl;
		par = par->Next;

	}
}


/*6.识别并统计单词*/
void Identification_Statistics()
{
	FILE *input;
	char array[20], a;
	int i;
	LinkList l;

	/*建立空的链表*/
	InitList(l);

	/*将文件放到项目目录下,并打开*/
	input = fopen("Infile.txt", "r");

	while (!feof(input))
	{
		i = 0;

		/*作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法*/
		memset(array, 0, sizeof(array));    /*内存初始化*/
		while ((a = fgetc(input)) != EOF && (a == ',' || a == '.' || a == '!' || a == '?' || a == ' ' || a == '(' || a == ')' || a == '\n') == 0)
		{
			array[i++] = a;
		}

		if (array[0])
		{
			Insert_Liner(l, array);
		}
	}

	Sort(l);
	Output_Frequency(l);
	fclose(input);
}


/*7.删除低频单词*/
void Delete()
{
	int i;
	FILE *input, *out;
	LinkList l, par, qar;
	char array[20], a;

	/*建立空的链表*/
	InitList(l);

	input = fopen("Infile.txt", "r");

	while (!feof(input))
	{
		i = 0;

		/*作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法*/
		memset(array, 0, sizeof(array));    /*内存初始化*/
		while ((a = fgetc(input)) != EOF && (a == ',' || a == '.' || a == '!' || a == '?' || a == ' ' || a == '(' || a == ')' || a == '\n') == 0)
		{
			array[i++] = a;
		}

		if (array[0])
		{
			Insert_Liner(l, array);
		}
	}

	Sort(l);
	fclose(input);

	cout << "删除低频词汇!" << endl;

	/*输出文件*/
	out = fopen("Infile_1.txt", "w+");

	par = l->Next;
	while (par != nullptr && par->Count >= 5)
	{
		par = par->Next;

	}

	while (par != nullptr)
	{
		qar = par;
		par = par->Next;
		printf("删除节点: %-20s", qar->Data);
		cout << " ";
		cout << qar->Count << endl;

		/*释放空间,删除结点*/
		free(qar);
	}

	fclose(out);

}

/*8.输出高频词汇以及频率*/
void Output_VocabularyAndfrequency()
{
	int i, j = 1;
	FILE *input, *out;
	LinkList l, par, qar;
	char array[20], a;

	/*建立空的链表*/
	InitList(l);

	input = fopen("Infile.txt", "r");

	while (!feof(input))
	{
		i = 0;

		/*作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法*/
		memset(array, 0, sizeof(array));    /*内存初始化*/
		while ((a = fgetc(input)) != EOF && (a == ',' || a == '.' || a == '!' || a == '?' || a == ' ' || a == '(' || a == ')' || a == '\n') == 0)
		{
			array[i++] = a;
		}

		if (array[0])
		{
			Insert_Liner(l, array);
		}
	}

	Sort(l);

	/*关闭文件*/
	fclose(input);


	/*输出文件*/
	out = fopen("Infile_1.txt", "w+");

	par = l->Next;

	cout << "删除低频率单词后\n单词       个数统计\n";

	while (par&&par->Count >= 5)
	{
		printf("%-20s", par->Data);
		cout << "  ";
		cout << par->Count << endl;

		fprintf(out, "%s(%d)\t", par->Data, par->Count);
		par = par->Next;

	}
	cout << "写入文件f2.txt成功" << endl;
	fclose(out);//关闭文件
}


/*9.计算ASL*/
void ASL()
{
	FILE *in;
	int sum = 0;
	char array[20], a;
	LinkList l, par;
	InitList(l);
	in = fopen("Infile.txt", "r");//打开输入文件

	double ASL;
	while (!feof(in))//直到碰见文件结束符结束循环
	{
		int i = 0;
		/*作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法*/
		memset(array, 0, sizeof(array));    /*内存初始化*/
		while ((a = fgetc(in)) != EOF && (a == ',' || a == '.' || a == '!' || a == '?' || a == ' ' || a == '(' || a == ')' || a == '\n') == 0)
		{
			array[i++] = a;
		}

		if (array[0])
		{
			Insert_Liner(l, array);
		}

	}

	par = l->Next;

	while (par)
	{
		sum++;
		par = par->Next;

	}
	cout << "输出单词总数" << sum << endl;
	fclose(in);

	ASL = (double)(sum + 1) / 2.0;
	cout << "ASL=" << ASL << endl;
}


/*10.输出执行时间*/
void Calculation_time()
{
	double star, finish, time;
	star = (double)clock();//获取当前时间

	Identification_Statistics();//统计
	Delete();
	Output_VocabularyAndfrequency();
	ASL();

	finish = (double)clock();//获取结束时间
	time = finish - star;
	cout << "执行时间" << time << endl;
}


/*11.全面执行*/
void AllFinish()
{
	Identification_Statistics();//统计
	Delete();
	Output_VocabularyAndfrequency();
	ASL();
}


/*12.菜单函数*/
void menu()
{
	cout << ("1、连续执行至完毕\n") << endl;
	cout << ("2、显示执行时间\n") << endl;
	cout << ("3、单步执行:识别并统计单词\n") << endl;
	cout << ("4、单步执行:删除并显示出现频率低单词\n") << endl;
	cout << ("5、单步执行:输出其余单词及其频率\n") << endl;
	cout << ("6、单步执行计算并输出ASL值\n") << endl;
	cout << ("7、返回主菜单\n") << endl;
	cout << ("请选择你需要的服务,输入数字(1-7)\n") << endl;
}


/*13.主体函数*/
void linear()
{

	int choose;
	menu();
	cin >> choose;


	switch (choose)
	{
	case 1:
		AllFinish();
		linear();
		break;
	case 2:
		Calculation_time();
		linear();
		break;
	case 3:
		Identification_Statistics();
		linear();
		break;
	case 4:
		Delete();
		linear();
		break;
	case 5:
		Output_VocabularyAndfrequency();
		linear();
		break;
	case 6:
		ASL();
		linear();
		break;
	case 7:
		break;
	}
}
/********************************************************************************/



3.3 BSTNode.h (头文件)

#pragma once
#include "Header file.h"


/*插入*/
void Insert_BiTree(BiTree &T, char *array)
{
	if (T == nullptr)
	{
		T = BiTree(malloc(sizeof(TNode)));
		strcpy(T->Data, array);
		T->lchild = nullptr;
		T->rchild = nullptr;
		T->Count = 1;
	}
	else
	{
		if (stricmp(array, T->Data) < 0)
		{
			Insert_BiTree(T->lchild, array);
		}
		else if (stricmp(array, T->Data) > 0)
		{
			Insert_BiTree(T->rchild, array);
		}
		else
		{
			T->Count++;
		}


	}
}


/*读取文件*/
void Read_FIlE()
{
	FILE *in;
	T = nullptr;
	in = fopen("Infile.txt", "r");
	char array[20], a;
	while (!feof(in))
	{
		int i = 0;
		/*作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法*/
		memset(array, 0, sizeof(array));    /*内存初始化*/
		while ((a = fgetc(in)) != EOF && (a == ',' || a == '.' || a == '!' || a == '?' || a == ' ' || a == '(' || a == ')' || a == '\n') == 0)
		{
			array[i++] = a;
		}
		if (array[0])
		{
			Insert_BiTree(T, array);
		}
	}
}

/*识别统计单词*/
void Identification_Statistics_BiTree()
{
	Read_FIlE();

	cout << "单词                         个数统计" << endl;
	InOrder_BiTree(T);

}



/*中序遍历*/
void InOrder_BiTree(BiTree T)
{
	if (T)
	{
		InOrder_BiTree(T->lchild);
		printf("%-20s            ", T->Data);
		cout << T->Count << endl;
		InOrder_BiTree(T->rchild);
	}

}
/*中序遍历二叉排序树*/
void New_Insert_BiTree(BiTree &T, BiTree P)
{
	if (T == nullptr)
	{
		T = (BiTree)(malloc(sizeof(TNode)));
		strcpy(T->Data, P->Data);
		T->Count = P->Count;
		T->lchild = nullptr;
		T->rchild = nullptr;


	}
	else
	{
		if (stricmp(P->Data, T->Data) < 0)
		{
			New_Insert_BiTree(T->lchild, P);
		}
		else
		{
			New_Insert_BiTree(T->rchild, P);
		}
	}
}


/*中序遍历二叉排序树*/
void New_InOrder_BiTree(BiTree T)
{
	
	if (T != nullptr)
	{
		New_InOrder_BiTree(T->lchild);
		if (T->Count < 5)
		{
			New_Insert_BiTree(P, T);
		}

		New_InOrder_BiTree(T->rchild);
	}
}


/*中序遍历二叉排序树的到有序*/
void New_InOrder_BiTree2(BiTree T)
{
	
	if (T != nullptr)
	{
		New_InOrder_BiTree2(T->lchild);
		if (T->Count >=5)
		{
			New_Insert_BiTree(P, T);
		}

		New_InOrder_BiTree2(T->rchild);
	}
}

/*删除低频词汇*/
void Delete_BiTree()
{
	Read_FIlE();

	cout << "删除频率小于5的二叉排序树中序遍历" << endl;
	P = nullptr;
	New_InOrder_BiTree(T);
	cout << "单词                      个数统计" << endl;
	InOrder_BiTree(P);


}

/*输出*/
void Output_BiTree()
{
	Read_FIlE();

	cout << "显示频率大于5的二叉排序树中序遍历" << endl;
	P = nullptr;
	New_InOrder_BiTree(T);
	cout << "单词                      个数统计" << endl;
	InOrder_BiTree(P);
}

/*全部执行*/
void AllFinish_BiTree()
{
	Identification_Statistics_BiTree();
	Delete_BiTree();
	Output_BiTree();

}


/*输出执行时间*/
void Calculation_time_BiTree()
{
	double star, finish;
	star = (double)clock();//获取当前时间
	
	AllFinish();

	finish = (double)clock();//获取结束时间
	cout<< (finish - star) << endl;
}


/*菜单函数*/
void BiTree_menu()
{
	cout << ("1、连续执行至完毕\n") << endl;
	cout << ("2、显示执行时间\n") << endl;
	cout << ("3、单步执行:识别并统计单词\n") << endl;
	cout << ("4、单步执行:删除并显示出现频率低单词\n") << endl;
	cout << ("5、单步执行:输出其余单词及其频率\n") << endl;
	cout << ("6、单步执行计算并输出ASL值\n") << endl;
	cout << ("7、返回主菜单\n") << endl;
	cout << ("请选择你需要的服务,输入数字(1-7)\n") << endl;
}

/*主体函数*/

void Tree()
{
	int choose;
	BiTree_menu();
	cin >> choose;
	

	switch (choose)
	{
	case 1:
		AllFinish_BiTree();
		Tree();
		break;
	case 2:
		Calculation_time_BiTree();
		Tree();
		break;
	case 3:
		Identification_Statistics_BiTree();
		Tree();
		break;
	case 4:
		Delete_BiTree();
		Tree();
		break;
	case 5:
		Output_BiTree();
		Tree();
		break;
	case 6:
		Tree();
		break;
	case 7:
		break;
	}

}


3.4 Header file.h(头文件)

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996) 

#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<time.h>
#include<malloc.h>
#include<cstring>
#include<string>
#include "Node.h"
using namespace std;

3.5 screeh.h(头文件)

#pragma once
#include<iostream>

using namespace std;

void screem()//主屏幕
{
	cout << ("1、线性表\n") << endl;
	cout<< ("2、二叉排序树\n") <<endl;
	cout<< ("3、退出系统\n") <<endl;
	cout<< ("请选择你需要的服务,输入数字(1-3)") <<endl;
}


3.6 main.cpp (源文件)

#include"LinkList.h"
#include"BSTNode.h"
#include"screen.h"

int main()
{
	int choose;
	while (1)
	{
		screem();
		cin >> choose;
		system("cls");
		while (!(choose == 1 || choose == 2 || choose == 3))
		{
			cout << "输入有误,请重新输入" << endl;
			cin >> choose;
			system("cls");
		}

		switch (choose)
		{
		case 1:
			linear();
			break;
		case 2:
			Tree();
			break;
		case 3:
			return 0;
		}
	}

	return 0;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用python中的pymsql完成如下:表结构与数据创建 1. 建立 `users` 表和 `orders` 表。 `users` 表有用户ID、用户名、年龄字段,(id,name,age) `orders` 表有订单ID、订单日期、订单金额,用户id字段。(id,order_date,amount,user_id) 2 两表的id作为主键,`orders` 表用户id为users的外键 3 插入数据 `users` (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28); `orders` (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4); 查询语句 1. 查询订单总金额 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 3. 查询订单总数最多的用户的姓名和订单总数。 4. 查询所有不重复的年龄。 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 8. 查询订单总金额最大的用户的姓名和订单总金额。 9. 查询订单总金额最小的用户的姓名和订单总金额。 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。
06-03

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值