词频统计程序(C++)
做一个词频统计程序,该程序具有以下功能
(1)可导入任意英文文本文件
(2)统计该英文文件中单词数和各单词出现的频率(次数),并能将单词按字典顺序输出。
(3)将单词及频率写入文件。
采用二叉树查找的方法,定义结构体及指针,实现查找单词及排序功能。
以下为源代码:
#include<iostream>
#include<string>
#include<ctype.h>
#include<fstream>
#define N 50
using namespace std;
/*定义结构体*/
typedef struct Tree{
char data[N];
int count;
struct Tree *lchild;//定义左子树指针
struct Tree *rchild;//定义右子树指针
}Tree,*bTree;//定义结构体指针
class Word{
public:
Word();
int get_word(int start, int end, char* p, char* word);//获取单词
void create_Tree(char* m, bTree& b); //创建二叉树进行查找
void Order(bTree b, FILE* q);//创建排序函数,FILE类指针
int sum;//总数
char temp[N];
char temp1[N];
char word[N];
char file_Name[20];
int j;
int i;//用于循环
};
/*初始化*/
Word::Word()
{
sum = 0;
j = 0;
i = 0;
}
/*定义获取单词数的函数*/
int Word::get_word(int start, int end, char* p, char* word)
{
memset(word, 0, sizeof(char));//获取字符串类型
for ( i = start; i<end; i++)//遍历文章
{
if (isalpha(p[i]))
{
word[j] = p[i];
j++;
}
else if (j == 0)
{
continue;
}
else
{
word[j] = '\0';
j = 0;
sum++;
break;
}
}
return i;
}
/*创建二叉树进行查找*/
void Word::create_Tree(char* m, bTree& b)
{
strcpy(temp, m);
temp[0] = tolower(m[0]);
if (b == NULL)
{
b = (bTree)malloc(sizeof(Tree));//给bTree动态创建堆栈
strcpy(b->data, m);
b->count = 1;
b->lchild = NULL;
b->rchild = NULL;
}
else
{
strcpy(temp1, b->data);
temp1[0] = tolower(b->data[0]);
if (strcmp(temp, temp1) == -1)
{
create_Tree(m, b->lchild);
}
else if (strcmp(temp, temp1) == 1)
{
create_Tree(m, b->rchild);
}
else
{
b->count++;
}
}
}
/*定义排序函数,通过中序遍历查找并写入文件*/
void Word::Order(bTree b, FILE* q)
{
if (b != NULL)
{
Order(b->lchild, q);
fprintf(q, "出现的词汇:%-30s 频率:%-9d\t\n", b->data, b->count);
printf("出现的词汇:%-30s 频率:%-9d\t\n", b->data, b->count);
Order(b->rchild, q);
}
}
int main()
{
Word w;//定义Word类对象
while (true)
{
memset(w.file_Name, 0, sizeof(w.file_Name));
/*文件保存在该工程文件中,以in.txt为例*/
cout << "请输入要进行词频统计的文件(本程序以in.txt为测试文件)" << endl;
cin >> w.file_Name;
FILE *q;
//ifstream f1;//说明输入文件流对象f1
//ofstream f2;//说明输出文件流对象f2
q=fopen(w.file_Name,"rb");
if (q==NULL)
{
cout << "找不到文件" << endl;
return false;
}
cout << "成功打开文件" << w.file_Name << endl;
/*读取文件*/
fseek(q, 0, SEEK_END);
int len = ftell(q);
rewind(q);
char *p = new char[len + 1];
p[len] = 0;
fread(p, 1, len, q);
fclose(q);
//读取单词
cout << p << endl;
cout << "*****************************************************************************\n\n" << endl;
bTree b = NULL;
while (w.i<len)
{
w.i = w.get_word(w.i, len, p, w.word);
if (strlen(w.word) == 0)
{
break;
}
w.create_Tree(w.word, b);
}
//写入文件并显示在屏幕上
memset(w.file_Name, 0, sizeof(w.file_Name));
/*写出文件名可自己定义*/
cout<<"请输入写出文件名(例如out.txt)可以自己定义: "<<endl;
cin >> w.file_Name;
q=fopen(w.file_Name,"w");
if (q == NULL)
{
cout << "不能写入文件" << endl;
return false;
}
cout<<"******************************************************************************\n"<<endl;
fprintf(q, "\t\t词频统计结果: \n");
cout<<"\t\t词频统计结果:\n"<<endl;
fprintf(q, "*********************************************************\n");
w.Order(b,q);
fprintf(q, "*********************************************************\n");
fprintf(q, "这篇文章单词总数: %d\n", w.sum);
printf("这篇文章单词总数:%d\n", w.sum);
fclose(q);
cout<<"******************************************************************************\n"<<endl;
cout << "统计结果写入文件:" << w.file_Name << endl;
}
return 0;
}