本程序为使用快速排序实现的英文单词表生成大代码,可以按单词出现的频次以及按单词的字母进行排序,
#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"; //需要自行修改需要分析的英文文本文件
本文作者原创,转载请标明文章出处。