设计程序,统计并输出一篇英文文章(文本文件)中的各个单词出现的次数。
#include <stdio.h>
#include <stdlib.h>
struct string
{
char data;
string *next;
};
#define str string
struct wordbook
{
str *c;
int time;
struct wordbook *next;
};
#define wb wordbook
void insert(str *a,wb *p)
{
wb *temp;
temp=(wb*)malloc(sizeof(wb));
temp->next=p->next;
p->next=temp;
temp->time=1;
temp->c=a;
}
void add(char a,str *p)
{
str *temp;
temp=(str*)malloc(sizeof(str));
temp->next=p->next;
p->next=temp;
temp->data=a;
}
str* readword(FILE *f)
{
char va;
str *p,*head;
head=(str*)malloc(sizeof(str));
head->next=NULL;
p=head;
va=fgetc(f);
if(va!=EOF)
{
while((va<65&&va>90)||(va<97&&va>122))
{
va=fgetc(f);
}
while((va>=65&&va<=90)||(va>=97&&va<=122))
{
add(va,p);
p=p->next;
va=fgetc(f);
}
p->next=NULL;
}
else
{
p->data=va;
p->next=NULL;
}
return head;
}
bool strcmp(str *a,wb *p)
{
int i,j;
for(i=0,j=0;a+i!=NULL&&(p->c)+j!=NULL;)
{
if((a+i)->data==((p->c)+j)->data)
i++,j++;
else
break;
}
if(a+i==NULL&&(p->c)+j==NULL)
return 0;
else
return 1;
}
bool search(str *a,wb *p)
{
for(;p->next!=NULL;)
{
p=p->next;
if(strcmp(a,p)==0)
return true;
}
return false;
}
void conduct(str *a,wb *p)
{
int flag;
flag=search(a,p);
if(flag==1)
p->time++;
else
insert(a,p);
}
void print (wb *p)
{
for(;p->next!=NULL;)
{
int i;
p=p->next;
for(i=0;(p->c)+i!=NULL;i++)
printf("%c ",((p->c)+i)->data);
printf("%d\n",p->time);
}
}
void main()
{
wb *head,*p;
str *a;
FILE* f = fopen("d:\\abc.txt","r");
head=(wb*)malloc(sizeof(wb));
head->next=NULL;
if(f==NULL)
printf("文本为空,无法完成统计");
else
{
for(a=readword(f);a->data!=EOF;)
{
p=head;
conduct(a,p);
a=readword(f-1);
}
print(head);
}
fclose(f);
system("pause");
}
#include <stdio.h>
#include <stdlib.h>
struct string
{
char data;
string *next;
};
#define str string
struct wordbook
{
str *c;
int time;
struct wordbook *next;
};
#define wb wordbook
void insert(str *a,wb *p)
{
wb *temp;
temp=(wb*)malloc(sizeof(wb));
temp->next=p->next;
p->next=temp;
temp->time=1;
temp->c=a;
}
void add(char a,str *p)
{
str *temp;
temp=(str*)malloc(sizeof(str));
temp->next=p->next;
p->next=temp;
temp->data=a;
}
str* readword(FILE *f)
{
char va;
str *p,*head;
head=(str*)malloc(sizeof(str));
head->next=NULL;
p=head;
va=fgetc(f);
if(va!=EOF)
{
while((va<65&&va>90)||(va<97&&va>122))
{
va=fgetc(f);
}
while((va>=65&&va<=90)||(va>=97&&va<=122))
{
add(va,p);
p=p->next;
va=fgetc(f);
}
p->next=NULL;
}
else
{
p->data=va;
p->next=NULL;
}
return head;
}
bool strcmp(str *a,wb *p)
{
int i,j;
for(i=0,j=0;a+i!=NULL&&(p->c)+j!=NULL;)
{
if((a+i)->data==((p->c)+j)->data)
i++,j++;
else
break;
}
if(a+i==NULL&&(p->c)+j==NULL)
return 0;
else
return 1;
}
bool search(str *a,wb *p)
{
for(;p->next!=NULL;)
{
p=p->next;
if(strcmp(a,p)==0)
return true;
}
return false;
}
void conduct(str *a,wb *p)
{
int flag;
flag=search(a,p);
if(flag==1)
p->time++;
else
insert(a,p);
}
void print (wb *p)
{
for(;p->next!=NULL;)
{
int i;
p=p->next;
for(i=0;(p->c)+i!=NULL;i++)
printf("%c ",((p->c)+i)->data);
printf("%d\n",p->time);
}
}
void main()
{
wb *head,*p;
str *a;
FILE* f = fopen("d:\\abc.txt","r");
head=(wb*)malloc(sizeof(wb));
head->next=NULL;
if(f==NULL)
printf("文本为空,无法完成统计");
else
{
for(a=readword(f);a->data!=EOF;)
{
p=head;
conduct(a,p);
a=readword(f-1);
}
print(head);
}
fclose(f);
system("pause");
}
