2012-05-08 4 views
-6

나는 이런 식 입력 파일에 걸리는 C에서 튜링 기계 시뮬레이터 ++ 디자인해야 Z : q1,0, Q2, X, R
T : 0,1,
T를 X Q1,1, Q2, X, R
T : q2,0, q2,0, R
..
S : q1
F : q3, q4
튜링 기계 시뮬레이터

Q는 상태, A는 입력 값, Z는 테이프 알파벳, S는 시작 상태, F는 수락 및 거부 상태입니다.

입력 수, 입력 문자열 및 허용 또는 거부에서 입력을 처리해야합니다.

따라서 입력 인 경우

0, # 1,1

1,1- 0,1,0

출력 단계를 인쇄하고 수락 여부 것 또는 거부합니다.

산술 연산을 수행하는 TM, 문자열 연산을 수행하는 TM 및 다른 것을 선택해야합니다.

시작하는 방법에 대한 도움을 주시면 감사하겠습니다. 이 같은

+3

지금까지 무엇을 얻었습니까? –

+0

지금까지 어떤 접근 방법을 취 했습니까? 당신은 무엇을 제안하고 도움이 필요합니까? 정말로 ... 우리가 당신의 숙제를하기를 바라는 것이 무엇입니까? – ScarletAmaranth

답변

1

시도 뭔가 :

#include <iostream> 
#include <string.h> 
#include <vector> 
#include <fstream> 
#include <cstdlib> 
#include <stdio.h> 
#include "tm.h" 

using namespace std; 

tm::tm(char *fn) 
{ 
    current = ""; 
    readFile(fn); 
} 

void tm::readFile(char *fn) 
{ 
    char temp; 
    string word = ""; 
    ifstream indata; 
    bool blank = false; 
    indata.open(fn); //opens the file 
    if(!indata) //error message if the file is unable to be opened 
    { 
     cout << "Could not open the specified file \"" << fn << "\"" << endl; 
     exit(1); 
    } 
    indata >> temp; 
    while (!indata.eof()) 
    { 
     if (temp == 'Q') 
     { 
      //cout << "Q" << endl; 
      indata >> temp; 
      indata >> temp; //skip the : 
      while (temp != 'A') 
      { 
       if (temp != ',') word += temp; 
       else 
       { 
        QQ.push_back(word); 
        word = ""; 
       } 
       indata >> temp; 
      } 
      QQ.push_back(word); 
      word = ""; 
     } 
     else if (temp == 'A') 
     { 
      //cout << "A" << endl; 
      indata >> temp; 
      indata >> temp; //skip the : 
      while (temp != 'Z') 
      { 
       if (temp != ',') word += temp; 
       else 
       { 
        AA.push_back(word); 
        word = ""; 
       } 
       indata >> temp; 
      } 
      AA.push_back(word); 
      word = ""; 
     } 
     else if (temp == 'Z') 
     { 
      //cout << "Z" << endl; 
      indata >> temp; 
      indata >> temp; //skip the : 
      while (temp != 'T') 
      { 
       if (temp != ',') word += temp; 
       else 
       { 
        ZZ.push_back(word); 
        word = ""; 
       } 
       indata >> temp; 
      } 
      ZZ.push_back(word); 
      word = ""; 
      for (int i = 0; i < (int)ZZ.size(); i++) 
       if (ZZ[i].compare(" ") == 0) 
        blank = true; 
      if (blank == false) //no blanks were found in the tape alphabet 
       ZZ.push_back(" "); 
     } 
     else if (temp == 'T') 
     { 
      //cout << "T" << endl; 
      indata >> temp; 
      indata >> temp; //skip the : 
      bool wasComma = false; 
      vector<string> row; 
      while (temp != 'T' && temp != 'S') 
      { 
       if (wasComma && temp == ',') //the last one was a comma 
       { 
        row.push_back(" "); 
        wasComma = false; 
       } 
       else if (temp != ',') 
       { 
        word += temp; 
        wasComma = false; 
       } 
       else 
       { 
        row.push_back(word); 
        word = ""; 
        wasComma = true; 
       } 
       indata >> temp; 
      } 
      row.push_back(word); 
      TT.push_back(row); 
      word = ""; 
     } 
     else if (temp == 'S') 
     { 
      //cout << "S" << endl; 
      indata >> temp; 
      indata >> temp; //skip the : 
     while (temp != 'F') 
      { 
       if (temp != ',') word += temp; 
       else 
       { 
        SS = word; 
        word = ""; 
       } 
       indata >> temp; 
      } 
      SS = word; 
      word = ""; 
     } 
     else if (temp == 'F') 
     { 
      //cout << "F" << endl; 
      indata >> temp; 
      indata >> temp; //skip the : 
      while (!indata.eof()) 
      { 
       if (temp != ',') 
        word += temp; 
       else 
       { 
        FF.push_back(word); 
        word = ""; 
       } 
       indata >> temp; 
      } 
      FF.push_back(word); 
      word = ""; 
     } 
    } 
    indata.close(); 
    readInput(); 
    runAll(); 
    return; 
} 

void tm::readInput() 
{ 
    int num, k; 
    cin >> num; 

    string temp; 
    getline(cin,temp); 
    getline(cin,temp); 
    for (int i = 0; i < num; i++) //num is the number of rows of input to the machine 
{ 
     vector<string> row; 
     string word = ""; 
     for(k = 0; k < (int)temp.size(); k++) 
     { 
      if (temp[k] != ',') 
       word += temp[k]; 
      else if ((int)word.size() > 0) 
      { 
       row.push_back(word); 
       word = ""; 
      } 
      if (k == (int)temp.size() -1 && (int)word.size() > 0) 
      { 
       row.push_back(word); 
       word = ""; 
      } 
    } 
     if (k > 0) 
      input.push_back(row); 
     else //if there is an empty row of input to the machine 
     { 
      vector<string> row; 
      row.push_back("e"); 
      input.push_back(row); 
     } 
     getline(cin,temp); 
    } 

    return; 
} 

void tm::runAll() 
{ 
    checkForAlphabetBlanks(); 
    checkTransitions(); 
    checkStart(); 
    checkFinal(); 
    checkDeterministic(); 
    checkAcceptReject(); 
    checkInput(); 

    int currentPosition; 
    string currentState, currentSymbol; 
    bool found, first = true; 
    for (int i = 0; i < (int)input.size(); i++) //for each row of the input 
    { 
     if (first != true) 
      cout << endl; 
     first = false; 
     currentPosition = 0; 
     currentState = SS; 
     for (int k = 0; k < 1000; k++) //for each character of input, then up to 1000 
     { 
      if (k == 0) //the first time 
      { 
       if (0 < (int)input[i].size()) //we are not past the right of the tape 
        currentSymbol = input[i][0]; 
       else 
        currentSymbol = " "; 
       cout << "()" << SS << "("; 
       for (int g = 0; g < (int)input[i].size(); g++) 
       { 
        cout << input[i][g]; 
        if (g != (int)input[i].size() - 1) //it is not the last input 
         cout << ","; 
        else 
         cout << ")" << endl; 
       } 
      } 
      if (currentState.compare(FF[0]) == 0) //check if we are accept 
      { 
       cout << "ACCEPT" << endl; 
       break; 
      } 
      else if (currentState.compare(FF[1]) == 0) //check if we are reject 
      { 
       cout << "REJECT" << endl; 
       break; 
      } 
      found = false; 
      for (int g = 0; g < (int)TT.size(); g++) 
      { 
       if (TT[g][0].compare(currentState) == 0 && TT[g][1].compare(currentSymbol) == 0) //same state and symbol as the transition line 
       { 
        found = true; 
        currentState = TT[g][2]; 
        input[i][currentPosition] = TT[g][3]; 
        if (TT[g][4].compare("R") == 0) currentPosition++; 
        else currentPosition--; 
        //check for out of bounds to the left 
        if (currentPosition < 0) currentPosition = 0; 
        cout << "("; 
        for (int t = 0; t < currentPosition; t++) 
        { 
         cout << input[i][t]; 
         if (t != currentPosition - 1) cout << ","; //not the last one 
        } 
        cout << ")" << currentState << "("; 
        for (int t = currentPosition; t < (int)input[i].size(); t++) 
        { 
         cout << input[i][t]; 
         if (t != (int)input[i].size() - 1) cout << ","; //not the last one 
        } 
        cout << ")" << endl; 
        if (currentPosition < (int)input[i].size()) currentSymbol = input[i][currentPosition]; //not past the right side of the tape 
        else 
        { 
         currentSymbol = " "; 
         input[i].push_back(" "); 
        } 
        break; 
       } 
      } 
      if (found == true) //a transition was found 
      { 
       if (currentState.compare(FF[0]) == 0) //check if accept 
       { 
        cout << "ACCEPT" << endl; 
        break; 
       } 
       else if (currentState.compare(FF[1]) == 0) //check if reject 
       { 
        cout << "REJECT" << endl; 
        break; 
       } 
      } 
      else 
      { 
       currentPosition++; 
       cout << "("; 
       for (int t = 0; t < currentPosition; t++) 
       { 
        cout << input[i][t]; 
        if (t != currentPosition - 1) cout << ","; //not the last one 
       } 
       cout << ")" << FF[1] << "("; 
       for (int t = currentPosition; t < (int)input[i].size(); t++) 
       { 
        cout << input[i][t]; 
        if (t != (int)input[i].size() - 1) cout << ","; //not the last one 
       } 
       cout << ")" << endl; 
       cout << "REJECT" << endl; 
       break; 
      } 
      if (k == 999) 
       cout << "DID NOT HALT" << endl; 
     } 
    } 

    return; 
} 

void tm::checkForAlphabetBlanks() 
{ 
    for (int i = 0; i < (int)AA.size(); i++) 
    { 
     if (AA[i].compare(" ") == 0) 
     { 
      cout << "The alphabet has a blank space." << endl; 
      exit(1); 
     } 
    } 
    return; 
} 

void tm::checkTransitions() 
{ 
    bool state1, state2, character1, character2; 

    for (int i = 0; i < (int)TT.size(); i++) 
    { 
    //check the direction 
     if (TT[i][4].compare("R") != 0 && TT[i][4].compare("L") != 0) 
     { 
      cout << "The only valid directions are R and L." << endl; 
      exit(1); 
     } 
     //check the states 
     state1 = false; state2 = false; 
     for (int k = 0; k < (int)QQ.size(); k++) 
     { 
      if (TT[i][0].compare(QQ[k]) == 0) state1 = true; 
      if (TT[i][2].compare(QQ[k]) == 0) state2 = true; 
     } 
     if (state1 == false) 
     { 
      cout << "The state " << TT[i][0] << " in transition function number " << i << " is not in the list of states." << endl; 
      exit(1); 
     } 
     if (state2 == false) 
     { 
      cout << "The state " << TT[i][2] << " in transition function number " << i << " is not in the list of states." << endl; 
      exit(1); 
     } 
     //check the characters 
     character1 = false; character2 = false; 
     for (int k = 0; k < (int)ZZ.size(); k++) 
     { 
      if (TT[i][1].compare(ZZ[k]) == 0) character1 = true; 
      if (TT[i][3].compare(ZZ[k]) == 0) character2 = true; 
     } 
     if (character1 == false) 
     { 
      cout << "The character '" << TT[i][1] << "' in transition function number " << i << " is not in the tape alphabet." << endl; 
      exit(1); 
     } 
     if (character2 == false) 
     { 
      cout << "The character '" << TT[i][3] << "' in transition function number " << i << " is not in the tape alphabet." << endl; 
      exit(1); 
     } 
} 
    return; 
} 

void tm::checkStart() 
{ 
    bool in = false; 
    for (int i = 0; i < (int)QQ.size(); i++) 
    { 
     if (SS.compare(QQ[i]) == 0) in = true; 
    } 
    if (in == false) 
    { 
     cout << "The start state " << SS << " is not in the list of states." << endl; 
     exit(1); 
    } 
} 

void tm::checkFinal() 
{ 
    if (FF[0].compare(FF[1]) == 0) 
    { 
     cout << "The accept and reject states cannot be the same." << endl; 
     exit(1); 
    } 
    bool accept = false, reject = false; 
    for (int i = 0; i < (int)QQ.size(); i++) 
    { 
     if (FF[0].compare(QQ[i]) == 0) accept = true; 
     if (FF[1].compare(QQ[i]) == 0) reject = true; 
     } 
     if (accept == false) 
     { 
     cout << "The accept state " << FF[0] << " is not in the list of states." << endl; 
     exit(1); 
    } 
    if (reject == false) 
    { 
     cout << "The reject state " << FF[1] << " is not in the list of states." << endl; 
     exit(1); 
    } 
} 

void tm::checkDeterministic() 
{ 
    for (int i = 0; i < (int)TT.size(); i++) 
     for (int k = i+1; k < (int)TT.size(); k++) 
      if (TT[i][0].compare(TT[k][0]) == 0 && TT[i][1].compare(TT[k][1]) == 0) 
      { 
      cout << "The machine cannot proceed deterministically, transitions " << i << " and " << k << " are the same." << endl; 
      exit(1); 
     } 
} 

void tm::checkAcceptReject() 
{ 
    for (int i = 0; i < (int)TT.size(); i++) 
    { 
     if (TT[i][0].compare(FF[0]) == 0 || TT[i][0].compare(FF[1]) == 0) 
     { 
      cout << "The machine cannot transitions from the accept or reject states." << endl; 
     } 
    } 
} 

void tm::checkInput() 
{ 
    bool exists; 
    for (int i = 0; i < (int)input.size(); i++) 
    { 
     for (int k = 0; k < (int)input[i].size(); k++) 
     { 
      exists = false; 
      for (int g = 0; g < (int)AA.size(); g++) 
      { 
       if (input[i][k].compare(AA[g]) == 0) exists = true; 
      } 
      if (exists == false) 
      { 
       if (input[i][0].compare("e") != 0) //it is not 'e' 
       { 
        cout << "The input character " << input[i][k] << " is not in the input alphabet." << endl; 
        exit(1); 
       } 
      } 
     } 
    } 
} 
+0

Jake에게 감사드립니다. 바로 시작해야합니다. – wDC

+0

다른 TM 응용 프로그램에 대한 제안 사항이 있습니까? – wDC

+4

이것이 숙제 인 경우 조심하세요. Jake는 선생님이 될 것입니다. 위의 코드를 사용하면 이미 실패한 것입니다. –

0

당신이 할 수있는 파일의 압축을 모방 : 그것은 문자열을 제공하고 더의 선형 시간이 걸릴 것입니다 8 (추가 큰 그룹의 그룹으로 반복적 인 시퀀스를 압축 코드 줄을 추가 할 경우 가능한 문자 하나 하나에 반응해야하는 각 문자 때문에 더 많은 문자가 추가됩니다.)

기계는 테이프의 앞쪽을 기록한 다음 뒤로. 그것은 캐릭터를 먹는다. 마치 캐릭터를 확대 풀에 넣는다. 한도에 도달하면 테이프의 끝 부분에 도달하거나 다른 문자 유형에 도달하면 기계는 그것이 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T:a5,b,b1,a5,L 
T:a5,y,Fs,a6,R 
T:a5,z,a5z,b,R 
T:a5z, ,Fs,a5,R 
T:b5,b,b6, ,L 
T:b5,a,a1,b5,L 
T:b5,z,Fs,b6,R 
T:b5,y,b5y,a,R 
T:b5y, ,Fs,b5,R  
T:a6,a,a7, ,L 
T:a6,b,b1,a6,L 
T:a6,y,Fs,a7,R 
T:a6,z,a6z,b,R 
T:a6z, ,Fs,a7,R 
T:b6,b,b7, ,L 
T:b6,a,a1,b6,L 
T:b6,z,Fs,b7,R 
T:b6,y,b6y,a,R 
T:b6y, ,Fs,b7,R 
T:a7,a,q2,a8,L 
T:a7,b,b1,a7,L 
T:a7,y,Fs,a8,R 
T:a7,z,a7z,b,R 
T:a7z, ,Fs,a7,R 
T:b7,b,q2,b8,L 
T:b7,a,a1,b8,L 
T:b7,z,Fs,b8,R 
T:b7,y,b7y,a,R 
T:b7y, ,Fs,b7,R 

S:q0 
F:Fs,Rs 

세포 생물학에서 아미노산을 만들기 위해 RNA 염기쌍의 판독을 모방 할 수 있습니다. 사용자는 a, c, g 및 u로만 구성된 문자 스트링을 제공합니다. 기계는 시작 코돈 "a, u, g"를 읽을 때까지 줄을 읽습니다.이 시점에서 RNA가 아미노산으로 번역되기 시작합니다. 그것은 문자열의 끝에 도달하거나 정지 코돈에 도달 할 때까지 ("UAA", "UGA", "UAG")이 작업을 수행합니다. 정지 코돈에 도달하면 다른 시작 코돈을 읽거나 문자열이 종료 될 때까지 문자열을 계속합니다. 어떤 아미노산 서열을 읽는 것은 받아 들여지고, (생물학에서와 같이) 정지 코돈에 도달 할 필요는 없다. 그러나 시작 코돈이없는 문자열은 거부 명령문이됩니다.

첫 번째 예는 몇 가지 문자 지연, 시작 코돈, 그리고 종료 코돈까지의 아미노산, 그리고 더 많은 사용되지 않는 문자들, 그리고 완료를 보여줍니다. 두 번째 예는 문자열 완성까지 시작 코돈, 중간 코돈, 정지 코돈, 필러, 다른 시작 코돈 및 코돈을 보여줍니다. 세 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