Hello, This is my second semester taking c++ and one of our final projects is to make a Turing Machine Simulator in C++ but I need help with some things which really shouldn't be holding me up so much!
First let me give you an example of what the input file looks like:
10 if 1 goto 15
right
goto 10
15 halt
The alphabet of the turing machine tape is{1,0,b} and the control unit has a RWH and a ROH. RWH points to tape, ROH points to the Turing Program.
The way I am going about doing this by first prompting the user to input through command then storing the input into a doubly linked list pointer struct.
From there I want to make an array of structs with the label (10, 15 or whatever int they put in) and a pointer that points to the corresponding textfile line.
For example:
label 10 -> if 1 goto 15
This is where I run into trouble. I have another struct with instructions. Its members are opCode, string alphabet, ptr branch, ptr next.
My main question is, How can I take that input, and store then in their corresponding structs. I know there is an easier way of doing this that I am missing and any help would be appreciated. Heres what Ive got so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
//////////////////////////Specification////////////////////
class Turing
{
public: void turingMachineSim (char);
Turing (); //default
private: struct tape; // tape
typedef tape* ptr;
struct tape
{
string name;
ptr left;
ptr right;
};
struct Label;
typedef label* ptr;
struct Label
{
int labelName;
ptr instruction;
}label [20];
struct iCode; //instructions
typedef iCode* ptr;
struct iCode
{
char opCode;
string alpha;
ptr branch;
ptr nextOp;
}
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include "Turing.h"
void Turing::turingMachineSim (char fileName)
{
ptr RHW, ROH, temp1, temp2;
int k = 0;
char input;
string line;
fin.open (fileName);
RWH = new (tape);
RWH -> name = b;
RWH -> left = NULL;
RWH -> right = NULL;
temp2 = RWH;
do
{
system ("cls");
SkipLine (9, 'c');
cout << setw (54) << "Please enter input for turing program: ";
SkipLine (2, 'c');
cout << setw (58) << "Input a '~' when you are done: ";
cin >> input;
temp1 = new (tape);
temp1 -> name = input
temp1 -> left = temp2;
temp1 -> right = NULL;
temp2 -> right = temp1;
temp2 = temp1;
}
while (input != '~');
//detach last node
temp2 -> left -> right = NULL;
temp2 -> left = NULL;
delete temp1;
delete temp2;
RWH = RWH -> right; // makes RWH the point to leftmost nonblank
// No idea what I'm doing here. Just tossing ideas
while (getline(fin,line,' ') // while not end of file
{
if (
fin >>label[k].labelName;
label [k].instruction = new (iCode);
fin >> label [k].instruction -> opCode;
fin >> label [k].instruction -> alpha;
fin >> label [k].instruction -> branch;
}
|