我使用DOSBox Turbo C编译器。
我编写了一个程序,用于从文本文件中读取字符,该文本文件包含一个小说,在过程中按字母顺序分隔单词,并将这些单词添加到各个字母表节点。当一个单词重复时,“计数”会计算出来。包含该单词的节点的增量。我的想法是确定每个字母表中最常用的单词。
但是,我发现由于此编译器可能存在内存限制,在一定数量的单词之后,只有(null)存储在节点中。输出文件(显示单词及其相应的出现次数)很好,直到读取的单词数(程序计算)接近12500(接近2500个节点)。在此之上,输出文件没有意义。
我尝试在Code :: blocks和Visual Studio Express 2010中运行该程序,但程序在两种情况下都崩溃了(可能是我的编码非常错误)。
我是业余程序员,发现这个问题极难解决。
附加内容(根据评论中的建议编辑):' n'在7754个单词后返回NULL。
请为我提供一个解决方案,通过代码或使用其他编译器来解决此问题。
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#include<ctype.h>
FILE *wp=fopen("OUTPUT.txt","w");
FILE *cp=fopen("Taken.txt","w");
char c,tword[25],t=0;
struct words* create(char tword[]);
int addword(char tword[]);
void display(int i);
int most(int i);
int sort(int i);
int main()
{
int i=0;
long int total=0;
FILE *fp=fopen("NOVEL.txt","r");
do
{
c=fgetc(fp);
if((c!=' ')&&(c!=EOF)&&(c!='.')&&(c!='\n'))
{
if((isalpha(c)||c=='-'))
{
if(t!=0)
{
tword[i++]=t;
t=0;
}
tword[i]=c;
i++;
}
else
{
if(i!=0)
t=c;
continue;
}
}
else
{
total++;
tword[i]=0;
strupr(tword);
addword(tword);
i=0;
t=0;
}
}while(!feof(fp));
fclose(fp);
for(i=0;i<26;i++)
{
sort(i);
display(i);
most(i);
}
fprintf(wp,"\nTotal number of words: %ld",total);
fclose(wp);
fclose(cp);
return 0;
}
struct words
{
char word[15];
long int count;
struct words *next;
}*n,*temp,*curr,*prev,*alph[26];
struct words* create(char tword[])
{
n=(struct words*)malloc(sizeof(struct words));
strcpy(n->word,tword);
n->count=1;
return n;
}
int addword(char tword[])
{
if((tword[0]=='\0')||(!isalpha(tword[0])))
{
if(tword[0]!='\0')
fprintf(cp,"The discarded word: %s\n",tword);
return 0;
}
temp=alph[tword[0]%'A'];
if(temp==0)
{
alph[tword[0]%'A']=create(tword);
alph[tword[0]%'A']->next=0;
}
else
{
curr=temp;
while((strcmp(curr->word,tword)<0)&&curr)
{
prev=curr;
curr=curr->next;
}
if(strcmp(curr->word,tword)==0)
{
curr->count++;
return 0;
}
if(curr==temp)
{
n=create(tword);
alph[tword[0]%'A']=n;
alph[tword[0]%'A']->next=curr;
}
else if(curr==0)
{
n=create(tword);
prev->next=n;
n->next=0;
}
else
{
n=create(tword);
prev->next=n;
n->next=curr;
}
fprintf(cp,"Added word: %s\n",n->word);
}
return 0;
}
void display(int i)
{
int space=0;
curr=alph[i];
fprintf(wp,"\t\t\t\t\t\t\t\tWords beginning with %c\n",'A'+i);
while(curr)
{
if(space%5==0)
{
fprintf(wp,"\n");
}
else fprintf(wp,"\t");
space++;
fprintf(wp,"%25s :%4ld",curr->word,curr->count);
curr=curr->next;
}
fprintf(wp,"\n\n\t\t\t\t\t\t\t\tNumber of words "
"beginning with %c : %3d",'A'+i,space);
}
int most(int i)
{
char tword[25];
curr=alph[i];
if(curr==0)
{
fprintf(wp,"\n\n\t\t\t\t\t\t\t\tNo words"
" starting with %c\n\n",'A'+i);
return 0;
}
long int grt=curr->count;
strcpy(tword,curr->word);
while(curr!=0)
{
if(grt<(curr->count))
{
grt=curr->count;
strcpy(tword,curr->word);
}
curr=curr->next;
}
fprintf(wp,"\n\n\t\t\t\t\t\t\t\tMost repeated "
"%c word is %s (%ld times)\n\n",'A'+i,tword,grt);
return 0;
}
int sort(int i)
{
char tempword[25];
long int tempint;
curr=alph[i];
while(curr)
{
temp=curr->next;
while(temp)
{
if(temp->count>curr->count)
{
strcpy(tempword,curr->word);
strcpy(curr->word,temp->word);
strcpy(temp->word,tempword);
tempint=temp->count;
temp->count=curr->count;
curr->count=tempint;
}
temp=temp->next;
}
curr=curr->next;
}
return 0;
}