首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >马尔可夫链测试及其实现

马尔可夫链测试及其实现
EN

Stack Overflow用户
提问于 2010-12-06 03:35:58
回答 2查看 2.5K关注 0票数 1

因此,我想读入一个包含随机数字的文本文件,并使用这些数字创建后缀列表。它基本上需要使用算法重新生成"alice30.txt“中给出的文本。我现在拥有的代码是这样的。它编译得很好,但没有为我创建输出文件。我需要反转后缀的数组,这样输出才是正确的。任何帮助都将不胜感激。我已经在eprint f头文件中声明了所有必要的函数,比如emalloc。

代码语言:javascript
复制
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
 #include "eprintf.h"

  enum {
NPREF   = 2,    /* number of prefix words */
NHASH   = 4093, /* size of state hash table array */
MAXGEN  = 10000 /* maximum words generated */
   };

 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  *lookup(char *prefix[], int create);
 void   build(char *prefix[], FILE* myfile); /*assing the input stream*/
 void       generate(int nwords, int random_num[], FILE* outfile);
  /* passing the array and the output stream*/
 void   add(char *prefix[], char *word);

 State  *statetab[NHASH];   /* hash table of states */

 char NONWORD[] = "\n";  /* cannot appear as real word */


  /* markov main: markov-chain random text generation */
 int main(void)
 {
int i, nwords = MAXGEN;
char *prefix[NPREF];        /* current input prefix */

FILE* random_reader;
FILE* myfile;
FILE* outfile;

int c;
int element;
int random_num[10000];
char* line;
int j=0;
random_reader = fopen("../random_num.txt","r");
myfile = fopen("../alice30.txt","r");
outfile = fopen("../output/markov_c_out.txt","w");
while(fgets(line,20,random_reader)!=NULL)
{
    sscanf(line,"%o",&element);
    random_num[j]= element;
    j++;
}

setprogname("markov");


for (i = 0; i < NPREF; i++) /* set up initial prefix */
    prefix[i] = NONWORD;
build(prefix, myfile);
add(prefix, NONWORD);
generate(nwords, random_num, outfile); // calling the updated function
return 0;
    }   

   const int MULTIPLIER = 31;  /* for hash() */

   /* 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; create if requested. */
   /*  returns pointer if present or created; NULL if 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)     /* found it */
        return sp;
}
if (create) {
    sp = (State *) emalloc(sizeof(State));
    for (i = 0; i < NPREF; i++)
        sp->pref[i] = prefix[i];
    sp->suf = NULL;
    sp->next = statetab[h];
    statetab[h] = sp;
}
return sp;
      }

     /* addsuffix: add to state. suffix must not change later */
      void addsuffix(State *sp, char *suffix)
     {
Suffix *suf;

suf = (Suffix *) emalloc(sizeof(Suffix));
suf->word = suffix;
suf->next = sp->suf;
sp->suf = suf;
  }

     /* add: add word to suffix list, update prefix */
   void add(char *prefix[NPREF], char *suffix)
    {
State *sp;

sp = lookup(prefix, 1);  /* create if not found */
addsuffix(sp, suffix);
/* move the words down the prefix */
memmove(prefix, prefix+1, (NPREF-1)*sizeof(prefix[0]));
prefix[NPREF-1] = suffix;
        }

       /* build: read input, build prefix table */
         void build(char *prefix[NPREF], FILE *myfile)
        {
char buf[100], fmt[10];

/* create a format string; %s could overflow buf */
sprintf(fmt, "%%%ds", sizeof(buf)-1);
while (fscanf(myfile, fmt, buf) != EOF)
    add(prefix, estrdup(buf));
         }

       /* generate: produce output, one word per line */
     void generate(int nwords, int random_num[], FILE* outfile)
     {
State *sp;
Suffix *suf;
char *prefix[NPREF], *w;
int i, nmatch;

for (i = 0; i < NPREF; i++) /* reset initial prefix */
    prefix[i] = NONWORD;

for (i = 0; i < nwords; i++) {
    sp = lookup(prefix, 0);
    if (sp == NULL)
        eprintf("internal error: lookup failed");
    nmatch = 0;
    for (suf = sp->suf; suf != NULL; suf = suf->next)
        if (random_num[i] % ++nmatch == 0) /*using the elements of the array  */
            w = suf->word;
    if (nmatch == 0)
        eprintf("internal error: no suffix %d %s", i, prefix[0]);
    if (strcmp(w, NONWORD) == 0)
        break;

    fprintf(outfile, w);
    memmove(prefix, prefix+1, (NPREF-1)*sizeof(prefix[0]));
    prefix[NPREF-1] = w;

}

}

EN

回答 2

Stack Overflow用户

发布于 2010-12-06 04:59:05

您正在向fgets传递一个未初始化的变量。

声明如下:

代码语言:javascript
复制
char* line;

以及稍后的呼叫:

代码语言:javascript
复制
while(fgets(line,20,random_reader)!=NULL)

但是您从未创建空间来存储字符串read。

票数 2
EN

Stack Overflow用户

发布于 2010-12-06 03:42:10

除了代码中使用的算法之外,您的程序不会生成输出,因为您没有关闭文件。在main函数的末尾添加以下几行:

代码语言:javascript
复制
fclose( outfile );
fclose( myfile );
fclose( randome_reader );

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4360723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档