简体   繁体   English

图灵机二进制计数器

[英]Turing Machine Binary Counter

I decided for practice that I would create a binary counter simulating the methodology of a Turing Machine. 为了实践,我决定创建一个二进制计数器来模拟图灵机的方法。 To be specific, I plan to emulate the first example from this: ( https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/turing-machine/four.html ) Then I will go farther to create more for my machine! 具体来说,我打算从中模拟第一个示例:( https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/turing-machine/four.html )然后,我将进一步进行创建我的机器还有更多!

My adaption to this example is that I wish to have a set number to count up to, then stop - instead of incrementing 1 at a time. 我对本示例的适应是,我希望有一个设定的数字可以累加,然后停止-而不是一次增加1。

I am using switch cases because I haven't used them in awhile, earlier, I had the same (adapted) code for if-else blocks. 我之所以使用开关盒,是因为我已经有一段时间没有使用它们了,更早的时候,我为if-else块使用了相同的(适应的)代码。

The issue I'm having at the moment is that my counter doesn't seem to go past 2 advances on the "tape." 我目前遇到的问题是,我的计数器似乎没有超过“磁带”上的2个预付款。 Below shows that it possibly gets stuck somewhere. 下面显示它可能卡在某处。 I suspect it is here: 我怀疑它在这里:

case 2:
    switch (symbol) {
        case ' ':
            s[i] = ' ';
            state = 0;
            i++;
            break; // here? 

Would it be wiser to put an increment function in this class and call that in the while (c<8) to increment by one as the example above shows? 如上例所示,将增量函数放在此类中并在while (c<8)调用该函数将其递增一会更明智吗? But wouldn't that need to call for static variables? 但是那不需要调用静态变量吗? which later, when I create more operations for my machine, would create issues, yes? 以后,当我为机器创建更多操作时,会出现问题吗?

public class TuringMachineSimulations {

    private static void print(char[] s) {
        for (int i = 0; i < s.length; i++) {
            System.out.print(s[i] + "_");
        }
        System.out.println();
    }

    public static void main(String[] args) {

        int n = 8; // number to count up to in binary 
        int c = 0; // counter
        int i = 0; // index counter within 'string'
        int state = 0; // state control
        char symbol = ' '; // what symbol is currently being read - starting is blank
        char[] s = new char[4]; // 4 bits needed to hold the integer "8" in binary

        while (c < 8) {
            switch (state) {
            case 0:
                switch (symbol) {
                case ' ':
                    s[i] = ' ';
                    state = 1;
                    i++;
                    break;
                case '0':
                    s[i] = '0';
                    state = 0;
                    i--;
                    break;
                case '1':
                    s[i] = '1';
                    state = 0;
                    i--;
                    break;
                default:
                    break;
                }
            case 1:
                switch (symbol) {
                case ' ':
                    s[i] = '1';
                    state = 2;
                    i--;
                    break;
                case '0':
                    s[i] = '1';
                    state = 2;
                    i++;
                    break;
                case '1':
                    s[i] = '0';
                    state = 1;
                    i++;
                    break;
                default:
                    break;
                }
            case 2:
                switch (symbol) {
                case ' ':
                    s[i] = ' ';
                    state = 0;
                    i++;
                    break;
                case '0':
                    s[i] = '0';
                    state = 1;
                    i--;
                    break;
                case '1':
                    s[i] = '1';
                    state = 1;
                    i--;
                    break;
                default:
                    break;
                }
            }
            symbol = s[i];
            print(s);
            c++;
        }
    }
}

PS I hope to get it such that the least significant bit is the first bit. PS我希望得到它,以便最低有效位是第一位。

It looks to me as though you are assuming that each step will involve an increment of the binary number. 在我看来,您似乎假设每个步骤都将涉及二进制数的增量。 You are running your 'steps' only 8 times. 您的“步骤”仅运行8次。 But many of those steps don't involve changing the 'tape' at all - in states 0 and 2 the 'head' is moved and the state is changed but there are no changes to the tape. 但是其中许多步骤根本不涉及更改“磁带”-在状态0和2中,“磁头”已移动,状态已更改,但磁带没有更改。

If you know the final state of 's' that you want (presumably "1000") then the easiest thing would be to replace while (c < 8) with while (!String.valueOf(s).equals("1000")) . 如果知道所需的's'的最终状态(大概是“ 1000”),那么最简单的方法是将while (c < 8)替换为while (!String.valueOf(s).equals("1000"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM