基于快速排序的英文书籍单词表生成(C/C++实现)

   本程序为使用快速排序实现的英文单词表生成大代码,可以按单词出现的频次以及按单词的字母进行排序,
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include <cassert>
#include <algorithm>
#include <math.h>
#include <iterator>
#include <cctype>

using namespace std;

typedef struct Unit{
    string sequence;
    int freq;
}Unit, Type;     //用于存储词汇表, sequence表示单词, freq表示该单词的频率

Unit verb[10000];   //10000表示很大的一个值,单词的个数不会超过这个值
int  numVerb=0;   //该变量表示不同单词的数量
int  newWord=1;   //该单词用于标记一个单词是否收录进词汇表中
string  text[100];
int line=0;
string  word="";

void readTxt( string file );     //按行读取txt文本
void textToword(  Unit verb1[] ,string text1[], int line1 );  //把text文本内容变为单个单词,并存储
void save( string strr, Unit verb1[] );
bool com( int type, Type a, Type x );
void sortbyword( Unit verb1[] , int numVerb1 );     //按单词排序
void sortbyfrequen(  Unit verb1[] , int numVerb1 );  //按频率排序
void QuickSort(Type a[], int p, int r, int type);
bool cmp( int type, Type a, Type x );
int  Partition(Type a[], int p, int r, int type);
int  RandomizedPartition(Type a[],int p,int r, int type);
void Swap(Type a[], int i, int j);
int  Random(int p, int r);

int main()
{
	int i;
	string file = "D://text.txt";   //需要自行修改

	readTxt( file );  //按行读取txt文本

	textToword( verb, text, line );

	int nOperType = 0;
	while( 1 )
	{
		printf("============================================================\n");
		printf("                     操作命令选项:\n\n");
		printf("%d退出\t%d显示文件内容\t%d按字母排序\t%d按词频排序\n",0,1,2,3 );
		printf("============================================================\n");
		printf("请输入操作选项:");
		scanf( "%d", &nOperType );
		switch( nOperType )
		{
		case 0:
			exit(0);
			break;
		case 1:
			system("cls");
			printf("显示文件内容:\n" );
			for( i=0;i<line;i++)
			{
				cout<< text[i] << endl;
			}
			continue;
		case 2:
			system("cls");
			printf("按字母排序:\n" );
			sortbyword( verb, numVerb );
			cout.setf( ios::left );
			for( i=0;i<numVerb;i++ )
			{
				cout<<" "<<setw(16)<< verb[i].sequence<<setw(5)<<verb[i].freq  <<endl;	
			}
			continue;
		case 3:
			system("cls");
			printf("按词频排序:\n" );
			sortbyfrequen( verb, numVerb );
			cout.setf( ios::left );
			for( i=0;i<numVerb;i++ )
			{
				cout<<" "<<setw(16)<< verb[i].sequence<<setw(5)<<verb[i].freq  <<endl;	
			}
			continue;
		default :
			system("cls");
			printf("显示文件内容:\n" );
			for( i=0;i<line;i++ )
			{
				cout<< text[i] << endl;
			}
			continue;
		}
	}
	return 0;
}


void readTxt( string file )   //按行读取txt文本
{
    ifstream infile; 
    infile.open( file.data() );   //将文件流对象与文件连接起来 
    assert( infile.is_open() );   //若失败,则输出错误消息,并终止程序运行 

    while( getline(infile,text[line]) )
    {
		line++;
    }
    infile.close();             //关闭文件输入流 
}

void textToword( Unit verb1[], string text1[], int line1 )   //把text文本内容变为单个单词,并存储
{
	string word1 = "";
	for( int i=0; i<line1; i++ )
	{
		for(int k=0;k<text1[i].size()+1;k++)
		{
			if(((text1[i][k]>='a')&&( text1[i][k]<='z'))||((text1[i][k]>='A')&&(text1[i][k]<='Z')))
			{
				word1 += text1[i][k]; 
			}
			if(((text1[i][k]<'A')||(text1[i][k]>'Z'))&&((text1[i][k]<'a')||(text1[i][k]>'z')))
			{
				if(word1 != "")
				{
					save( word1,verb1 );
					word1 = ""; 
				}
			}

		}
	}
}

void save( string strr, Unit verb1[] )
{
	int newWord1 = 1;
	string str = strr;
	transform(str.begin(), str.end(), str.begin(), ::tolower);
	for(int i=0;i< numVerb;i++)
	{
		if(str==verb1[i].sequence)   
		{
			verb1[i].freq++;
			newWord1 = 0;    
		}
	}
	if(newWord1==1)  
	{
		verb1[numVerb].sequence = str;  
		verb1[numVerb].freq = 1;  
		numVerb++;
	}
}


void sortbyword( Unit verb1[] , int numVerb1 )
{
	QuickSort(  verb1, 0, numVerb1-1, 1);
}


void sortbyfrequen(  Unit verb1[] , int numVerb1 )
{
	QuickSort(  verb1, 0, numVerb1-1, 3);
}


bool cmp( int type, Type a, Type x )    //type=1 时按字母排序 , type=3 时按词频排序
{
	if( type==1 )
	{
		const char *str1 = a.sequence.data();
		const char *str2 = x.sequence.data();
		if( strcmp( str1,str2 )<0 )
			return true;
		else
			return false;
	}else if( type==2 )
	{
		const char *str1 = a.sequence.data();
		const char *str2 = x.sequence.data();
		if( strcmp( str1,str2 )>0 )
			return true;
		else
			return false;
	}else if( type==3 )
	{
		if( a.freq > x.freq )
			return true;
		else
			return false;
	}else if( type==4 )
	{
		if( a.freq < x.freq )
			return true;
		else
			return false;
	}else
		return false;
}


void QuickSort(Type a[], int p, int r, int type)
{
	if (p<r)
	{
		int q = RandomizedPartition(a,p,r,type);
		QuickSort(a,p,q-1,type); 
		QuickSort(a,q+1,r,type); 
    }
}

int Partition(Type a[], int p, int r, int type)
{       
	int i = p, j = r + 1; 
    Type x = a[p];

    while (true) {
		while ( cmp( type, a[++i], x ) && i<r );
		while ( cmp( type+1, a[--j], x ) );
        if (i >= j) break; 
        Swap(a,i,j);
    }
    a[p] = a[j];
    a[j] = x;
    return j;
}

int RandomizedPartition(Type a[],int p,int r, int type)
{	
	int i = Random(p,r);
	Swap(a,i,p);
	return Partition(a, p, r, type);
}

void Swap(Type a[], int i, int j)
{
	Type temp;
	temp = a[i];
	a[i] = a[j];
	a[j] = temp;
}

int Random(int p, int r)
{
	return rand()%(r-p)+p;
}

注意:

string file = "D://text.txt";   //需要自行修改需要分析的英文文本文件


本文作者原创,转载请标明文章出处。

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Trie树是一种用于高效存储和检索字符串数据集的树形数据结构。在实现Trie树的过程中,我们需要使用一个数组来存储节点信息,而不是链。 以下是C语言实现遍历Trie树生成单词字典序的示例代码,其中使用了数组来存储节点信息: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define ALPHABET_SIZE 26 // Trie树节点结构体 struct TrieNode { struct TrieNode *children[ALPHABET_SIZE]; int isEndOfWord; }; // 创建Trie树节点 struct TrieNode *createNode() { struct TrieNode *node = (struct TrieNode *)malloc(sizeof(struct TrieNode)); node->isEndOfWord = 0; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = NULL; } return node; } // 插入单词到Trie树 void insert(struct TrieNode *root, char *word) { struct TrieNode *node = root; int len = strlen(word); for (int i = 0; i < len; i++) { int index = word[i] - 'a'; if (!node->children[index]) { node->children[index] = createNode(); } node = node->children[index]; } node->isEndOfWord = 1; } // 遍历Trie树并生成单词字典序 void traverse(struct TrieNode *node, char *word, int level) { if (node->isEndOfWord) { word[level] = '\0'; printf("%s\n", word); } for (int i = 0; i < ALPHABET_SIZE; i++) { if (node->children[i]) { word[level] = i + 'a'; traverse(node->children[i], word, level + 1); } } } int main() { char *words[] = {"hello", "world", "he", "hi", "there", "their", "they", "them"}; int n = sizeof(words) / sizeof(words[0]); // 创建Trie树并插入单词 struct TrieNode *root = createNode(); for (int i = 0; i < n; i++) { insert(root, words[i]); } // 遍历Trie树并生成单词字典序 char word[100]; traverse(root, word, 0); return 0; } ``` 上述代码中,我们首先定义了一个Trie树节点结构体,其中包含一个指向子节点的指针数组和一个示当前节点是否为单词结尾的标志位。然后,我们定义了几个函数来创建节点、插入单词以及遍历Trie树并生成单词字典序。 在`main`函数中,我们首先定义了一个字符串数组来存储要插入Trie树的单词。然后,我们创建了一个Trie树并依次插入这些单词。最后,我们调用了`traverse`函数来遍历Trie树并生成单词字典序。在遍历的过程中,我们使用一个字符数组`word`来存储当前遍历到的单词,`level`示当前遍历到的层数(即单词的长度)。如果当前节点为单词结尾,我们就将`word`数组中的字符打印出来。最后,我们递归遍历当前节点的所有子节点,并将当前节点的字符加入`word`数组中,继续向下遍历。 需要注意的是,如果要生成单词字典序,我们需要在遍历Trie树时按照字典序的顺序遍历子节点,即先遍历a、b、c……等字母节点,再遍历下一个节点。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值