2016-04-28 99 views
-4

我想在C 编写一个图灵机,但我的程序不能正常工作,它就会陷入无限循环。 这里是我的代码有一些解释:ç图灵机无限循环

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#define N 3 //number of different states for the cells 
#define K 20 //length of the tape 


typedef struct 
{ 
int state; 
int head; 
char tape[]; 
}mt; //machine 

void init_mt(mt* machine, char val[], int n) 
{ 
machine->state=1; //edited mistake 
machine->head=0; // edited mistake 
int i; 
for(i=0;i<n;i++) 
    { 
     machine->tape[i]=val[i]; 
    } 
}; //initialization of a machine 


typedef struct 
{ 
char write; 
char direction; 
int state; 
}actions; //actions composed of three instructions 

typedef struct 
{ 
actions exec01; 
actions exec02; 
actions exec11; 
actions exec12; 
}program; //program composed of four actions 

void execute(actions exec, mt mach) 
{ 
    mach.tape[mach.head] = exec.write; 
    mach.state = exec.state; 

    if(exec.direction == 'R') 
    { 
     mach.head++; 
    } 
    else 
    { 
     mach.head--; 
    } 
} //class that follows the instructions from the actions 

void execute2(mt mach, program p) 
{ do{ 
printf("%c %d %d \n", mach.tape[mach.head], mach.head, mach.state); 

if(mach.tape[mach.head] == 0) 
{ 

    if(mach.state == 1) 
    { 
     execute(p.exec01, mach); 
    } 
    else if(mach.state == 2) 
    { 
     execute(p.exec02,mach); 
    } 
} 
else if(mach.tape[mach.head] == 1) 
{ 
    if(mach.state == 1) 
    { 
     execute(p.exec11,mach); 
    } 
    else if(mach.state == 2) 
    { 
     execute(p.exec12,mach); 

    } 
} 

}while((mach.head<K) && (mach.state != 3)); 
} // class that read the program and act according to the states of the cells, 
//keeps going until the machine is at the third state or if it reaches the end of the tape 


int main(){ 
mt machine; 
char t[10]={'1','1','1','0','0','1','0','1','0','1'}; 
init_mt(&machine, t, 10); 
program p ={ {'0','R',1}, {'0','R',1}, {'1','R',2}, {'0','L',3} }; 
execute2(machine, p); 
return 0; 
} //main with a tape composed of 10 cells and a program composed of four actions 

这个程序一直显示“0,0,1”无限期,我无法找出错误。 感谢您的帮助和抱歉,如果这不清楚。

+2

这并不编译。 'mt'没有成员'etat'和'tete'。如何发布您实际使用的代码? –

+0

大概你想'执行'来修改“图灵机”,而不是你传递给它的**拷贝**,对吧?为什么不'void f(int x){x ++;} int main(){int i = 5; F(1); printf(“%i \ n”,i);返回0;}'print 6? mt machine; – immibis

+0

'mt machine; .. init_mt(&machine,t,10);'但'machine.tape'没有内存。 – BLUEPIXY

回答

0

这里有几个问题:

  1. 在某些情况下你传递你的结构作为参数,而不是指向他们。这会在被调用的函数中创建整个结构的本地副本。当函数返回时,对这些副本所做的任何更改都将丢失。这也是低效的。只需传递结构指针即可。

  2. 你没有在你的结构中声明空间为tape,所以它基本上是一个零长度的数组。任何形式的访问都会破坏内存并导致未定义的行为。你在这里有几个选择。您可以为其选择一些固定大小,并将其用于数组大小,也可以将其更改为指针并为其动态分配存储空间。无论如何,你必须分配存储空间。

  3. execute2中,它将mach.tape[mach.head]01的整数进行比较。但磁带不包含这些值。它包含字符'0''1'。所以用单引号括住这些常量。如果遇到意外的值,则打印错误也是一个好主意。那会立即发现这个问题。

+0

1.我改变了结构的指针结构,它现在显示1,0,1 2.我该怎么做?我试图把'char tape [10];'放在'typedef struct {} mt'中,但它不会改变任何东西。 – gzzzzz

+0

太棒了,它的工作。 – gzzzzz

0

在功能execute,按值传递结构mach。在该功能还执行

mach.head++ 

这,大概价值应该返还给功能execute2。所以你必须通过引用这个函数来传递结构mach

+0

我想我明白你的意思了。如果使用指向'mach'的指针,它会起作用吗? – gzzzzz