2015-07-12 30 views
2

我想实现DFA(Deterministic Finite Automaton)在C,到目前为止我试图存储DFA在变量transitions转换如下代码所示。但由于某种原因, odd指数transitions中的数据被破坏,我认为这是因为我用分隔\0的方式。我在代码后面附加了输出,谢谢。数据损坏,同时存储在字符***

#include<stdio.h> 
#include<string.h> 
#define MAX_INPUTS 10 
#define MAX_INPUT_SIZE 5 
#define MAX_STATES 20 
#define MAX_STATE_SIZE 5 
#define MAX_ID_SIZE 100 
int get_id(char str[],char str_arr[][MAX_STATE_SIZE],int len) 
{ 
    int i; 
    for(i=0;i<len;i++) 
    { 
     if(strcmp(str,str_arr[i]) == 0) 
      return i; 
    } 
    return -1; 
} 
int main() 
{ 
    char inputs[MAX_INPUTS][MAX_INPUT_SIZE]; 
    char states[MAX_STATES][MAX_STATE_SIZE]; 
    char*** transitions; 
    char temp_state[MAX_STATE_SIZE],temp_input[MAX_INPUT_SIZE],id_string[MAX_ID_SIZE],cur_inp[MAX_INPUT_SIZE],cur_state[MAX_STATE_SIZE]; 
    int i,j,k,numinps,numstates,row_id,col_id; 
    printf("Enter number of input states: "); 
    scanf("%d",&numstates); 
    printf("Enter number of input symbols: "); 
    scanf("%d",&numinps); 
    transitions = (char ***) malloc(sizeof(char**)*numstates); 
    printf("Enter the input states: \n"); 
    for(i=0;i<numstates;i++) 
    { 
     scanf(" %s",&temp_state); 
     strcpy(states[i],temp_state); 
    } 
    printf("Enter the input symbols: \n"); 
    for(i=0;i<numinps;i++) 
    { 
     scanf(" %s",temp_input); 
     strcpy(inputs[i],temp_input); 
    } 
    printf("Enter the transitions:\n"); 
    for(i=0;i<numstates;i++) 
    { 
     for(j=0;j<numinps;j++) 
     { 
      transitions[i] = (char **) malloc(sizeof(char)*MAX_INPUT_SIZE*numinps); 
      transitions[i][j] = (char *) malloc(sizeof(char)*MAX_STATE_SIZE); 
      printf("\n(%s , %s) => ",states[i],inputs[j]); 
      scanf(" %s",temp_state); 
      strcpy(transitions[i][j],temp_state); 
     } 
    } 
    for(i=0;i<numstates;i++) 
     for(j=0;j<numinps;j++) 
      printf("\n TRANSITION: (%s , %s) => %s\n",states[i],inputs[j],transitions[i][j]); 
    printf("Enter a String to find ID: "); 
    scanf(" %s",id_string); 
    strcpy(cur_state,"q0"); 
    for(i=0;i<strlen(id_string);i++) 
    { 
     cur_inp[0] = id_string[i]; 
     cur_inp[1] = '\0'; 
     row_id = get_id(cur_inp,inputs,numinps); 
     if(row_id == -1) 
     { 
      printf("Input symbol is not present in sigma"); 
      exit(0); 
     } 
     col_id = get_id(cur_state,states,numstates); 
     strcpy(cur_state,transitions[row_id][col_id]); 
     printf("\n---- %s => %s ----\n",cur_inp,cur_state); 
    } 
} 

OUTPUT

+0

关于如下行:'scanf(“%s”,&temp_state);' temp_state只有5个字符长,所以最大输入长度为4(允许%s附加终止字节),但是没有任何东西阻止用户输入'abcde',这会使输入缓冲区溢出,导致未定义的行为并可能是一个seg故障事件。建议:每次调用带有%s格式参数的scanf(),都要包含一个长度修饰符(在本例中为4)。另外,始终检查scanf()返回的值(不是参数值),以确保操作是成功 – user3629249

+0

当调用malloc(和函数族)时1)sizeof(char)始终为1,对返回值没有影响,并且只会混淆C中的代码2)不要从malloc()返回返回的值。它已经是一个'void *',所以可以保存到任何其他指针) – user3629249

+0

在这行之前:'exit(0);'和程序中的任何其他退出点,将所有分配的内存指针传递给free()。否则会有(很多)内存泄漏。 – user3629249

回答

4

这可能是这个问题:

for(i=0;i<numstates;i++) 
{ 
    for(j=0;j<numinps;j++) 
    { 
     transitions[i] = (char **) malloc(sizeof(char)*MAX_INPUT_SIZE*numinps); // <- this line 
     transitions[i][j] = (char *) malloc(sizeof(char)*MAX_STATE_SIZE); 
     printf("\n(%s , %s) => ",states[i],inputs[j]); 
     scanf(" %s",temp_state); 
     strcpy(transitions[i][j],temp_state); 
    } 
} 

您在j每个循环分配transitions[i],而不是使用:

for(i=0;i<numstates;i++) 
{ 
    transitions[i] = (char **) malloc(sizeof(char)*MAX_INPUT_SIZE*numinps); 
    for(j=0;j<numinps;j++) 
    { 
     transitions[i][j] = (char *) malloc(sizeof(char)*MAX_STATE_SIZE); 
     printf("\n(%s , %s) => ",states[i],inputs[j]); 
     scanf(" %s",temp_state); 
     strcpy(transitions[i][j],temp_state); 
    } 
} 

j循环您realocate整个transitions[i]这将导致只有最后一个字符串将被存储

+1

啊非常感谢你:) –

+0

没问题@PruthviRaj – maskacovnik