#include <iostream.h>
#include <string.h>
#include
<malloc.h>
typedef struct link
{
char*
ch;
int num;
link*
next;
}link;
int main(int argc, char* argv[])
{
FILE *fp;
fp=fopen("d:\\test.txt","r");
char
word[100000];
int pos=0;
char
c;
link *head, *pnow, *ptmp;
head=NULL;
pnow=NULL;
ptmp=NULL;
while (!feof(fp))
{
c=fgetc(fp);
if
((c>=‘a‘&&c<=‘z‘)||(c>=‘A‘&&c<=‘Z‘)||(c==‘\‘‘))
word[pos++]=c;
else if
(pos>0)
{
word[pos] =
‘\0‘;
ptmp=head;
while
(ptmp)
{
if (strcmp(word,
ptmp->ch)==0)
{
ptmp->num++;
break;
}
ptmp=ptmp->next;
}
if
(ptmp==NULL)
{
ptmp=(link*)malloc(sizeof(link));
ptmp->ch=(char*)malloc(pos);
strcpy(ptmp->ch,
word);
ptmp->num=1;
ptmp->next=NULL;
if
(pnow)
{
pnow->next=ptmp;
pnow=ptmp;
}
else
head=pnow=ptmp;
}
pos=0;
}
fclose(fp);
ptmp=head;
FILE
*fp1=fopen("d:\\text.txt","w");
while
(ptmp)
{
fprintf(fp1,"%d\t%s\n", ptmp->num,
ptmp->ch);
ptmp=ptmp->next;
}
fclose(fp1);
return 0;
}
}
原文:http://www.cnblogs.com/zuoyan123/p/3577453.html