2016-04-28 94 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