クイックソートに基づく英語書籍単語表生成(C/C++実装)
4840 ワード
このプログラムでは、高速ソートで実装された英語の単語テーブルに大きなコードを生成し、単語の出現頻度や単語のアルファベットでソートすることができます.
注意:
本文の作者はオリジナルで、転載は文章の出所を明記してください.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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("============================================================
");
printf(" :
");
printf("%d \t%d \t%d \t%d
",0,1,2,3 );
printf("============================================================
");
printf(" :");
scanf( "%d", &nOperType );
switch( nOperType )
{
case 0:
exit(0);
break;
case 1:
system("cls");
printf(" :
" );
for( i=0;i='a')&&( text1[i][k]<='z'))||((text1[i][k]>='A')&&(text1[i][k]<='Z')))
{
word1 += text1[i][k];
}
if(((text1[i][k]'Z'))&&((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= 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"; //
本文の作者はオリジナルで、転載は文章の出所を明記してください.