c语言怎么统计小说词频的程序,C语言 词频统计,怎么让程序跑的更快,还可以优化哪些?...

我已经读取了文本中的单词,并建了一棵二叉树,然后将二叉树的结点都推入指针栈中,然后运用快速排序的算法,最后输出结果,代码如下:

跑一篇单词很大的英文小说

平均占用内存:288.376K 平均CPU时间:0.891S 平均墙钟时间:0.419S

请问怎么改动程序,使其跑得更快?

#include

#include

#include

#include

#define MAX 50

struct tnode{

char word[MAX];

int count;

struct tnode *left,*right;

};

typedef struct sort{

int num;

char w[50];

}List;

List list[1000000];

struct tnode *arr[1000000];

int curr=0;

int cmp(const void *a,const void *b)

{

List *p1 = (List*)a;

List *p2 = (List*)b;

if(p1->num!=p2->num)

return p2->num-p1->num;

else

return strcmp(p1->w,p2->w);

}

struct tnode *treewords(struct tnode *,char *);

void treeprint(struct tnode *);

int main()

{

char word[MAX];

FILE *bfp;

FILE *out;

char c;

int i,k,n;

struct tnode *root;

root=NULL;

bfp=fopen("article.txt","r");

while((c=fgetc(bfp))!=EOF){

ungetc(c,bfp);

for(i=0;(c=fgetc(bfp))!=' '&&c!='\n'&&c!=EOF;i++){

if((c>='A'&&c<='Z')||(c>='a'&&c<='z')){

c=tolower(c);

word[i]=c;

}else

break;

}

word[i]='\0';

if(strlen(word)>0)

root=treewords(root,word);

}

treeprint(root);

for(i=0;i

list[i].num=arr[i]->count;

strcpy(list[i].w,arr[i]->word);

}

n=curr;

qsort(list,n+1,sizeof(list[0]),cmp);

out=fopen("wordfreq.txt","w");

for(k=0;k

fprintf(out,"%s %d\n",list[k].w,list[k].num);

}

for(k=0;k<100;k++){

printf("%s %d\n",list[k].w,list[k].num);

}

fclose(bfp);

fclose(out);

return 0;

}

struct tnode *treewords(struct tnode *p,char *w)

{

int cond;

if(p==NULL){

p=(struct tnode*)malloc(sizeof(struct tnode));

strcpy(p->word,w);

p->count=1;

p->left=p->right=NULL;

}

else if((cond=strcmp(w,p->word))==0){

p->count++;

}

else if(cond<0){

p->left=treewords(p->left,w);

}

else

p->right=treewords(p->right,w);

return (p);

}

void treeprint(struct tnode *p)

{

if(p!=NULL){

treeprint(p->left);

arr[curr++]=p;

treeprint(p->right);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值