请c语言高手帮我看看我这道词频统计哪里出问题了?
#include<stdio.h>#include<string.h>#include<stdlib.h>#definetolower(c)(c>='A'&&c<='Z'...
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define tolower(c)(c>='A'&&c<='Z'?'a'-'A'+c:c)
struct st{
char word[32];
int count;
struct st *link;
};
struct st *Wordlist=NULL;
int getWord(FILE *fp,char *w)
{
char c;
c=fgetc(fp);
if (c==EOF)
return 0;
while(!(tolower(c)>= 'a' && tolower(c)<= 'z'))
{
c=fgetc(fp);
if (c==EOF)
return 0;
} /* 跳过单词间的所有非字母字符 */
while(tolower(c)>= 'a' && tolower(c)<= 'z')
{
*w++=c;
c=fgetc(fp);
}
*w='\0';
return 1;
}
int searchWord(char *w)
{
struct st *p,*q=NULL;
for(p=Wordlist;p!=NULL;q=p,p=p->link)
{
if(strcmp(w,p->word)<0)
break;
else if(strcmp(w,p->word)==0)
{
p->count++;
return 0;
}
}
return insertWord(q,w);
}
int insertWord(struct st *r,char *w)
{
struct st *t;
t=(struct st*)malloc(sizeof(struct st));
if(t==NULL)
return -1;
strcpy(t->word,w);
t->count=1;
t->link=NULL;
if(Wordlist==NULL)
Wordlist=t;
else if(r==NULL)
{
t->link=Wordlist;
Wordlist=t;
}
else{
t->link=r->link;
r->link=t;
}
return 0;
}
int main()
{
char filename[32],word[32];
FILE *fp;
struct st *p;
if((fp=fopen("article.txt","r"))==NULL)
{
printf("article can't open!\n'");
return -1;
}
while(getWord(fp,word)!=EOF)
if(searchWord(word)==-1)
{
printf("Memory is full!\n");
fclose(fp);
return -1;
}
for(p=Wordlist;p!=NULL;p=p->link)
printf("%s %d\n",p->word,p->count);
fclose(fp);
return 0;
} 展开
#include<string.h>
#include<stdlib.h>
#define tolower(c)(c>='A'&&c<='Z'?'a'-'A'+c:c)
struct st{
char word[32];
int count;
struct st *link;
};
struct st *Wordlist=NULL;
int getWord(FILE *fp,char *w)
{
char c;
c=fgetc(fp);
if (c==EOF)
return 0;
while(!(tolower(c)>= 'a' && tolower(c)<= 'z'))
{
c=fgetc(fp);
if (c==EOF)
return 0;
} /* 跳过单词间的所有非字母字符 */
while(tolower(c)>= 'a' && tolower(c)<= 'z')
{
*w++=c;
c=fgetc(fp);
}
*w='\0';
return 1;
}
int searchWord(char *w)
{
struct st *p,*q=NULL;
for(p=Wordlist;p!=NULL;q=p,p=p->link)
{
if(strcmp(w,p->word)<0)
break;
else if(strcmp(w,p->word)==0)
{
p->count++;
return 0;
}
}
return insertWord(q,w);
}
int insertWord(struct st *r,char *w)
{
struct st *t;
t=(struct st*)malloc(sizeof(struct st));
if(t==NULL)
return -1;
strcpy(t->word,w);
t->count=1;
t->link=NULL;
if(Wordlist==NULL)
Wordlist=t;
else if(r==NULL)
{
t->link=Wordlist;
Wordlist=t;
}
else{
t->link=r->link;
r->link=t;
}
return 0;
}
int main()
{
char filename[32],word[32];
FILE *fp;
struct st *p;
if((fp=fopen("article.txt","r"))==NULL)
{
printf("article can't open!\n'");
return -1;
}
while(getWord(fp,word)!=EOF)
if(searchWord(word)==-1)
{
printf("Memory is full!\n");
fclose(fp);
return -1;
}
for(p=Wordlist;p!=NULL;p=p->link)
printf("%s %d\n",p->word,p->count);
fclose(fp);
return 0;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询