数据结构课程设计--单词频率统计.docx

上传人:wj 文档编号:1971187 上传时间:2023-05-02 格式:DOCX 页数:5 大小:78.75KB
下载 相关 举报
数据结构课程设计--单词频率统计.docx_第1页
第1页 / 共5页
数据结构课程设计--单词频率统计.docx_第2页
第2页 / 共5页
数据结构课程设计--单词频率统计.docx_第3页
第3页 / 共5页
数据结构课程设计--单词频率统计.docx_第4页
第4页 / 共5页
数据结构课程设计--单词频率统计.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

数据结构课程设计--单词频率统计.docx

《数据结构课程设计--单词频率统计.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计--单词频率统计.docx(5页珍藏版)》请在冰点文库上搜索。

数据结构课程设计--单词频率统计.docx

课程设计5 单词频率统计

基本功能要求:

(1)从文件中读出一篇英文文章,将不同的单词存入堆中,建立相应的

索引表确定各单词在堆中存储的位置及出现的次数。

(参考第四章应用举例—图书馆关键字或教材P86)

(2)按出现的频率递减的次序输出。

实验源程序:

#include#include#include#defineMax50typedefstructxcc

{

intn;

charp[Max];

}Word;

typedefstructxiang

{

Wordw;

structxiang*next;

}LinkWord;

LinkWord*Createnote(void);

voiddealwith(LinkWord*L,charc[]);

intcompare(charconstp[],charconstc[]);voidstorage(charp[],charc[]);

voiddisplay(LinkWordconst*L);voidclear(charp[],intn);

voidDeletNode(LinkWord*s,LinkWord*p);intmain(void)

{

charc[Max]="";inti=0,max=0;charch='\0';

charsource[Max]="";charend[Max]="";

LinkWord*L=Createnote();//头结点LinkWord*s=L,*p=NULL,*q=NULL;FILE*fp1=NULL,*fp2=NULL;

printf("请输入源文件路径:

");gets(source);

printf("请输入输出文件路径:

");

gets(end);fp1=fopen(source,"r");

if(fp1==NULL){printf("Can'topenthisfile!

\n");exit(0);}while((ch=fgetc(fp1))!

=EOF)

{

if((ch>=65&&ch<=90)||(ch>=97&&ch<=122))

{

c[i++]=ch;continue;

}

if((c[0]>=65&&c[0]<=90)||(c[0]>=97&&c[0]<=122))dealwith(L,c);//处理有效单词clear(c,Max);

i=0;

}

fclose(fp1);fp2=fopen(end,"w");

if(fp2==NULL){printf("Can'topenthisfile!

\n");exit(0);}while(q=p=s->next)

{

if(p==NULL)break;max=p->w.n;while(q!

=NULL)

{

if(q->w.n>p->w.n)p=q;q=q->next;

}

fprintf(fp2,"%s. %d\n",p->w.p,p->w.n);

DeletNode(s,p);//删除s链表中的p指向的结点

}

fclose(fp2);free(L);

printf("\n\n操作成功,请在%s中查看结果!

\n\n",end);

printf("按任意键结束!

\n");getch();

return0;

}

voidDeletNode(LinkWord*s,LinkWord*p)//删除s链表中的p指向的结点

{

LinkWord*q=s;

while(q->next!

=p)q=q->next

;

q->next=p->next;free(p);

}

voidclear(charp[],intn)//清空c数组

{

inti=0;

for(i=0;i

;

}

voiddealwith(LinkWord*L,charc[])//处理c中单词

{

LinkWord*p=L;

while(p->next!

=NULL)//查找链表中是否存在这个单词

{

p=p->next;

if(compare(p->w.p,c))

{

p->w.n++;return;

}

}

if(p->next==NULL)//不存在就存放在最后

{

p->next=Createnote();p->next->w.n=1;

storage(p->next->w.p,c);//将c中的单词存到结点中

}

}

intcompare(charconstp[],charconstc[])//判断p中单词与c中单词是否相同

{

for(;*p!

='\0'&&*c!

='\0';)if(*p++!

=*c++)return0;

return(*p=='\0'&&*c=='\0')?

1:

0;

}

LinkWord*Createnote(void)

{

LinkWord*L=(LinkWord*)malloc(sizeof(LinkWord));L->next=NULL; L->w.n=0;

return(L);

}

voidstorage(charp[],charc[])//将数组c中的单词存到数组p中

{

while(*p++=*c++)

;

}

voiddisplay(LinkWordconst*L)

{

LinkWordconst*p=NULL;

for(p=L;p->next!

=NULL;p=p->next)printf("%s..%d\n",p->w.p,p->w.n);

}

实验结果截图:

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2