制造图灵机但不能结束

问题描述

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
        
using namespace std;
        
struct rule {
    string currentState;
    char currentSymbol;
    char newSymbol;
    char direction;
    string newState;
};
        
void read(string tape,int head,vector<rule>A){
        
    //string filename;
    //cin>>filename;
    ifstream ifs ("1.txt");
    ifs>>tape;
    ifs>>head;
        
    vector<char> ttape;
        
    char char1,char2,char3;
    string string1,string2;
    rule temp;
    while (ifs >> temp.currentState) {
        
        ifs >> temp.currentSymbol;
        ifs >> temp.newSymbol;
        ifs >> temp.direction;
        ifs >> temp.newState;
        //cout<<temp.currentState<<temp.currentSymbol<<temp.newSymbol<<temp.direction<<temp.newState<<endl;
        A.push_back(temp);
        //cout<<A[0].currentState<<A[0].currentSymbol<<A[0].newSymbol<<A[0].direction<<A[0].newState<<endl;
        
    }
    ifs.close();
        
    head--;
    string sstate = "0";
    while(true){
        
        cout<<tape<<endl;
        
        for(int cycle=0;cycle<A.size();cycle++){
        
            if(A[cycle].currentState == sstate) {
        
                if(tape[head] == A[cycle].currentSymbol){
        
                    tape[head] = A[cycle].newSymbol;
                    sstate = A[cycle].newState;
                    //cout<<"1"<<endl;
                    if(A[cycle].direction == 'L'){
                        head--;
    
                        //this doesnt work at all still goes unlimited cycle when it shouldn't
                        if(head < 1 || head>tape.length()){
                            cout<<"Out of range"<<endl;
                            break;
                        }
                    }
                    else
                        head++;
                }
            }
        }
    }
}
        
int main(int argc,char *argv[])
{
    string tape;
    int head;
    vector<rule>A;
    read(tape,head,A);
        
    return 0;
}

当我的算法结束从文件解决问题时,我无法理解如何停止这个循环。以及如何找出它的“头部”何时超出范围。有人可以为这两个问题提供解决方案吗?

当它达到“H”时让它停止不算是解决它。任何人都知道如何在循环中停止这种情况?我想我需要一个来让它停止。

下面是我正在阅读的文件

M00110100N00000000P

2


0 0 0 R 0
0 1 1 R 0
0 N N L 1

1 1 0 R 2
1 0 1 L 1
1 M M R 7

2 0 0 R 2
2 1 1 R 2
2 2 2 R 2
2 3 3 R 2
2 4 4 R 2
2 5 5 R 2
2 6 6 R 2
2 7 7 R 2
2 8 8 R 2
2 9 9 R 2
2 A A R 2
2 B B R 2
2 C C R 2
2 D D R 2
2 E E R 2
2 F F R 2
2 N N R 2
2 P P L 3

3 0 1 L 4
3 1 2 L 4
3 2 3 L 4
3 3 4 L 4
3 4 5 L 4
3 5 6 L 4
3 6 7 L 4
3 7 8 L 4
3 8 9 L 4
3 9 A L 4
3 A B L 4
3 B C L 4
3 C D L 4
3 D E L 4
3 E F L 4
3 F 0 L 3

4 0 0 L 4
4 1 1 L 4
4 2 2 L 4
4 3 3 L 4
4 4 4 L 4
4 5 5 L 4
4 6 6 L 4
4 7 7 L 4
4 8 8 L 4
4 9 9 L 4
4 A A L 4
4 B B L 4
4 C C L 4
4 D D L 4
4 E E L 4
4 F F L 4
4 N N L 1

7 0 0 R 7 
7 1 0 R 7
7 N N L H

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)