Garmaine Staff asked 4 months ago

I am creating a Turing machine simulator for a class. It takes in a files of transition functions, loads those transition functions into vectors, then takes in a word and checks if that word is accepted or rejected. The program works off a while loop in the operate function that handles the calculation. We are required to implement a button of our choice that will end the loop during calculation. How would I go about this?

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <thread>
using namespace std;

bool able_to_stop = true;

class turing_sim{
     public:
          void fill_vecs(int & num, string & word){
               switch(num){
                    case 0: curr_states.push_back(word);
                            break;
                    case 1: curr_tape_sym.push_back(word);
                            break;
                    case 2: new_state.push_back(word);
                            break;
                    case 3: new_tape_sym.push_back(word);
                            break;
                    case 4: direction.push_back(word);
                            break;
               }
          }

          void fill_tape(string & n){
               vector<string> newtape;
               tape = newtape;
               for(int i = 0; i < n.length(); i++){
                    auto it = tape.begin();
                    tape.insert(it + i, n.substr(i, 1));
               }
               tape_ptr = 0;
               current_state = "0";
          }

          string operate(){
               bool rejected = false;
               while(current_state != "f" && !rejected && !stop_operate){
                    int sub = 0;
                    for(int i = 0; i < curr_states.size(); i++){
                         if(curr_states[i] == current_state){
                              if(curr_tape_sym[i] == tape[tape_ptr]){
                                   cout<<current_state<<" ";
                                   for(int j = tape_ptr; j < tape.size(); j++){
                                        cout<<tape[j]<<" ";
                                   }
                                   cout<<endl;
                                   current_state = new_state[i];
                                   tape[tape_ptr] = new_tape_sym[i];
                                   if(direction[i] == "R"){
                                        tape_ptr++;
                                        if(tape_ptr > (tape.size() - 1)){
                                             tape.push_back("B");
                                        }
                                   }
                                   else{
                                        tape_ptr--;
                                        if(tape_ptr < 0){
                                             auto it = tape.begin();
                                             tape.insert(it, "B");
                                             tape_ptr = 0;
                                        }
                                   }
                                   sub = 2;
                              }
                         }
                    }
                    if(sub == 0){
                         rejected = true;
                         cout<<current_state<<" ";
                         for(int i = tape_ptr; i < tape.size(); i++){
                              cout<<tape[i]<<" ";
                         }
                         cout<<endl;
                    }
               }
               if(current_state == "f") return "Accepted!";
               else return "Rejected...";
          }

     private:
          vector<string> curr_states;
          vector<string> curr_tape_sym;
          vector<string> new_state;
          vector<string> new_tape_sym;
          vector<string> direction;

          string current_state = "0";

          int tape_ptr = 0;

          vector<string> tape;

          bool stop_operate = false;
};

void Decompose_sentence(string& sentence, turing_sim & sim){//separates each line
     string wrd = "";                                       //into individual elements
     int count = 0;                                         //and stores them in vectors
     for(int i = 0; i < sentence.size(); i++){
          if(sentence.substr(i, 1) != " "){
               wrd += sentence.substr(i, 1);
          }
          else if(wrd != ""){
               sim.fill_vecs(count, wrd);
               wrd = "";
               count++;
          }
          if(sentence.substr(i + 1, 1) == "/") i = sentence.size() + 1;
     }
     if(!wrd.empty()){
          sim.fill_vecs(count, wrd);
     }
}

void read_file(string & file, turing_sim & sim){//loads important lines to decompose
     string sentence;
     ifstream infile;
     infile.open(file);
     while (getline(infile, sentence)) {
          while(sentence.back() == '\r' || sentence.back() == '\n'
               || sentence.back() == '.' || sentence.back() == char(34)){
                    sentence.erase(sentence.size() - 1);
          }
          if(sentence[0] != '/' && !sentence.empty()){
               Decompose_sentence(sentence, sim);
          }
     }
     infile.close();
}

int main(){
     turing_sim machine;
     string file, word;
     cout<<"Enter file name: ";
     cin>>file;
     read_file(file, machine);
     bool x = true;
     while(x){
          cout<<"Enter word to test: ";
          cin>>word;
          machine.fill_tape(word);
          cout<<machine.operate()<<endl;
     }
     return 0;
}