【问题标题】:C Turing Machine infinite loopC图灵机无限循环
【发布时间】:2016-04-28 01:15:02
【问题描述】:

我正在尝试用 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”,我找不到错误。 感谢您的帮助,如果不清楚,我们深表歉意。

【问题讨论】:

  • 这不会编译。 mt 没有成员 etattete。发布您实际使用的代码怎么样?
  • 大概您希望exec 修改“the”图灵机,而不是您传递给它的 copy,对吧?为什么void f(int x) {x++;} int main() {int i = 5; f(i); printf("%i\n", i); return 0;} 不打印 6?
  • mt machine; .. init_mt(&amp;machine, t, 10);machine.tape 没有内存。
  • 感谢您的回答。
  • @TomKarzes 抱歉,我认为翻译类的名称以使其更清晰会更容易,我对其进行了编辑,现在可以使用。

标签: c turing-machines


【解决方案1】:

这里有几个问题:

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

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

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

【讨论】:

  • 1.我更改了指向结构的指针中的结构,它现在显示 1,0,1 2。我该怎么做?我尝试将char tape[10]; 放入typedef struct{}mt,但它并没有改变任何东西。
【解决方案2】:

在函数execute 中,您通过值传递结构mach。在该功能中,您还可以执行

mach.head++

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

【讨论】:

  • 我想我明白你在说什么。如果使用指向mach 的指针,它会工作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 2016-08-27
  • 2015-04-29
  • 1970-01-01
相关资源
最近更新 更多