文本文件单词的检索与计数
三、文本文件单词的检索与计数
要求:
1) 具有图形菜单界面;
2) 查找,替换(等长,不等长),插入(插串,文本块的插入)、块移动(行块,列块移动),删除
3) 可正确存盘、取盘;
4) 正确显示总行数。
[解决办法]
呵呵,作业还是自己做的好
[解决办法]
// 统计一篇文章中每个单词出现的次数#include<stdio.h> #include<string.h> #include<conio.h> #include<stdlib.h> struct node{ char *s; int num; node *next; }; node * insert(node *&head,node *p) //构建链表 { node *q,*q1; if(!head) //建头 { head=p; p->next=0; p->num=1; return head; } q1=q=head; while(q) //对比单词 { if(strcmp(q->s,p->s)) { q1=q;q=q->next; } else { q->num+=1; return head; } } if(!q)q1->next=p; //插入尾部 return head; }node *readfile() //读文件{ char *p=NULL,*q=p,*q1,e; node *head=0,*head1=0,*word; FILE *fp; fp = fopen("word.txt","r"); if(fp == NULL) { printf("file open error"); exit(0); } while(fgets(p,100,fp)!=NULL) { while(!isalpha(*p))++p; //指向单词开始处 q1=p; while(isalpha(*q1)&&*q1)++q1; //指向单词尾处 e=*q1; //留COPY *q1='{post.content}'; //置结尾符 word=new node; word->s=new char[strlen(p)+1]; strcpy(word->s,p); //放入单词 word->num=1; word->next=0; head=insert(head,word); //插入链表 word=0;p=q1; *p=e; } //完成循环后所有单词被无序插入head中. node *hh=head; while(hh) { word=new node; word->s=new char[strlen(hh->s)+1]; strcpy(word->s,hh->s); word->num=hh->num; word->next=0; hh=hh->next; } while(head) //回收空间 { hh=head; head=hh->next; delete hh->s; delete hh; } return head1; } int main() { node *result = readfile(); //读文件 node *p=result; while(p) { printf("单词:[%s]出现次数:[%d]", p->s, p->num); p=p->next; } return 0; }