大家帮忙看一下这个代码的问题出在哪?谢谢了!!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
enum
{
NPREF=2, //number of prefix words
NHASH=4093, //size of state hash table array
MAXGEN=10000, //maximum words generated
MULTIPLIER=37,
NLETTER=20,
};
typedef struct State State;
typedef struct Suffix Suffix;
struct State //prefix + suffix list
{
char *pref[NPREF]; //prefix words
Suffix *suf; //list of suffixes
State *next; //next in hash table
};
struct Suffix //list of suffixes
{
char *word; //suffix
Suffix *next; //next in list of suffixes
};
State *statetab[NHASH]; //hash table of states
char buf[MAXGEN][NLETTER];
char *prefix[NPREF];
//emalloc:allocate the memery ,if fail return NULL
void *emalloc(int a)
{
void *p;
p=malloc(a);
if(p=NULL)
{
printf("Defult!!!\n");
return NULL;
}
return p;
}
//hash:compute hash value for array of NPREF strings
unsigned int hash(char *s[NPREF])
{
unsigned int h;
unsigned char *p;
int i;
h=0;
for(i=0;i<NPREF;i++)
for(p=(unsigned char *)s[i];*p!='\0';p++)
h=MULTIPLIER*h+*p;
return h%NHASH;
}
//lookup:search for prefix;ceate if requested.
//returns pointer if present of created;NULLif not.
//creation doesn't strdup so strings mustn't change later.
State* lookup(char *prefix[NPREF],int create)
{
int i,h;
State *sp;
h=hash(prefix);
for(sp=statetab[h];sp!=NULL;sp=sp->next)
{
for(i=0;i<NPREF;i++)
if(strcmp(prefix[i],sp->pref[i])!=0)
break;
if(i==NPREF)
return sp;
}
if(create)
{
sp=(State *)emalloc(sizeof(State));
printf("%p\n",sizeof(sp->pref[0]));
for(i=0;i<NPREF;i++)
{
sp->pref[i]=NULL;
sp->pref[i]=prefix[i];
}
sp->suf=NULL;
sp->next=statetab[h];
statetab[h]=sp;
}
return sp;
}
//build:read input ,build prefix table
void build()
{
void add(char *prefix[NPREF],char *suffix); //fuction prototype declare
int i;
printf("Input the sentences:\n");
for(i=0;i<NPREF;i++)
{
scanf("%s",buf[i]);
prefix[i]=buf[i];
}
scanf("%s",buf[NPREF]);
for(i=NPREF+1;strcmp(buf[i],"\n");i++)
{
add(prefix,buf[i]);
scanf("%s",buf[i]);
}
}
//add:add word to suffix list,update prefix
void add(char *prefix[NPREF],char *suffix)
{
void addsuffix(State *sp,char *suffix);
State *spp;
spp=lookup(prefix,1);
addsuffix(spp,suffix);
//move the words down the prefix
memmove(prefix,prefix+1,(NPREF-1)*sizeof(prefix[0]));
prefix[NPREF-1]=suffix;
}
//addsuffix:add to state ,suffix must not change later
void addsuffix(State *spp,char *suffix)
{
Suffix *suf;
suf=(Suffix *)emalloc(sizeof(Suffix));
suf->word=suffix;
suf->next=spp->suf;
spp->suf=suf;
}
void PRINTF()
{
char *prefix[NPREF];
int i,j,h;
for(j=0;buf[j+1][0];j++)
{
for(i=0;i<NPREF;i++,j++)
prefix[i]=buf[j];
h=hash(prefix);
printf("statetab[%d]\t",h);
printf("The prefixes :%s\t",statetab[h]->pref[1]);
//printf("%s\t",statetab[h]->pref[2]);
//printf("and the sufix :%s",statetab[h]->suf->word);
}
}
//markov main:markov-chain random text generation
int main()
{
//int i,nwords=MAXGEN;
/*char *prefix[NPREF];
for(i=0;i<NPREF;i++)
prefix[i]=NONWORD;*/
build();
PRINTF();
//generate(nwords);
return 0;
}