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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
// TuringMachine.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
////////////////////////////////////////////////////TURING MACHINE///////////////////////////////////////////////////////////
//std libraries
#include <iostream>
#include <cstdlib>
#include <ctime>
//using namespace
using namespace std;
/*
//my files
#include "stateOne.cpp"
#include "stateTwo.cpp"
//my headers
#include "definitions.h"
#include "stateOne.h"
#include "stateTwo.h"
*/
//DEFINITIONS
#define READ cout << "\nMACHINE IS IN STATE " << state <<"! On place " << position << " the machine reads a " << tape[position] << ".";
#define PLACE cout << "\nPosition = " << position;
#define STEPS ;
//constants
const char HALT[] = "\n\n\t\t\t\tThe Turing Machine halts! Press any key for exit.";
const char LEFTEND[] = "\nThats the very end of the tape. The TM can't move left to place -1!";
const char RIGHTEND[] = "\nThats the very end of the tape. The TM can't move right to place 100!";
const char CONFINAL[] = "\n\t\t\t\tThe last 5 readings were a '1'."; //condition when the machine is finished
//functions
//
//
int main()
{
//variables
int tape[100];
int i = 0, n = 0;
int START;
char exit;
//functions
int stateOne(int tape[], int position);
int stateTwo(int tape[], int position);
srand(time(NULL));
cout << "\n\t\t\t\tCreate a random Turing Machine tape with length 100:\n";
for(n = 0; n <= 99; n++) //fill array with random integers from 0 to 1.
{
i = rand() % 2;
tape[n] = i;
cout << tape[n];
}
for (n = 0; n <= 2; n++) //3 trys to find a nice place to start.
{
cout << "\n\nOn what tape number should the TM start? (0-99)\n";
cin >> START;
while (START > 99 | START < 0)
{
cout << "\nThe entered number is either to big or to small. Please enter a number between 0 and 99.";
cin >> START;
}
cout << "\nPlace " << START << " is a " << tape[START] << "\n";
}
cout << "\nMachine starts in S1.";
stateOne(tape, START); //here we go!!!
cin >> exit;
return 0;
}
//STATE 1
//
//
int stateOne(int tape[], int position)
{
//vars
int conFive = 0;
int state = 1;
//functions
int stateTwo(int tape[], int position);
while ((tape[position] == 1 | tape[position] == 0) && (position >= 0 || position <= 99)) //while reading 1 or 0 and array values are in scope...
{
//Dieser Code macht deutlich warum sich zweiwertige Variablen nicht selbst durch Ausschliessung bedingen. Obwohl der Ausdruck nur die Werte 1 und 0 bearbeitet, sagt
//die Funktion <while (tape[position] == 1) ...>, obwohl logisch äquivalent zu <while (tape[position] != 0) ...>, nicht das Gleiche aus!
//Im ersten Fall läuft die Schleife nur 1x, weil die Bedingung immer wahr. Im Zweiten aber solange sie ungleich 0 ist. Schleifen sollten nicht POSITIVE Bedingungen erwarten!
/*while (tape[position] == 1)
{
cout << "\nMACHINE IS IN STATE 1! On place " << position << " the machine reads a " << tape[position] << ".";
position++;
cout << "\nCOMMAND => move to place " << position << " and stay in S1.";
}*/
while (tape[position] != 0 && position <= 99) //and while the TM is reading 1 ...
{
//cout << "\nMACHINE IS IN STATE 1! On place " << position << " the machine reads a " << tape[position] << ".";
READ;
cout << "\n\t=> COMMAND: move to place " << position + 1 << " and stay in State " << state << ".";
position++; //move right
conFive++; //increment final halt condition
if (position == 99 || position > 99) //if the tape ends...
{
PLACE;
cout << RIGHTEND;
cout << HALT;
return 0;
break;
}
if (conFive == 5) //or if final halt condition is true
{
cout << CONFINAL;
cout << HALT;
return 0;
break;
}
break;
}
while (tape[position] != 1) //or while the TM is readin 0s...
{
if (position > 0) //and position is not 0
{
READ;
cout << "\n\t=> COMMAND: replace place " << position << " with 1, move to place " << position - 1 << " and move to S2.";
tape[position] = 1; //overwrite the 0 with a 1
position--; //move left
stateTwo(tape, position); //and move to state 2.
break;
}
else if (position == 0 && tape[position] == 1) //else, if the very first number is a 1
{
while (tape[position] != 0 && position <= 99) //start to repeat again final halt condition (=read 5x 1 in a row)
{
READ;
cout << "\n\t=> COMMAND: move to place " << position + 1 << " and stay in S1.";
position++; //move right
conFive++; //increment final halt condition
if (position == 99 || position > 99) //but when the tape ends
{
PLACE;
cout << RIGHTEND;
cout << HALT; //the TM halts.
break;
return 0;
}
if (conFive == 5) //or the final halt condition is true
{
cout << CONFINAL;
cout << HALT; //and the TM halts.
break;
return 0;
}
break;
}
}
else //if all conditions above are not true, then we read in position 0 a 0
{
PLACE;
cout << LEFTEND;
cout << HALT; //and the TM halts. (because it can't move right under such conditions)
break;
return 0;
}
}
//break;
return 0;
}
return 0;
}
//STATE 2
//
//
int stateTwo(int tape[], int position)
{
//var
int conFive = 0;
int state = 2;
//functions
int stateOne(int tape[], int position);
while ((tape[position] == 1 | tape[position] == 0) && (position >= 0 || position <= 99)) //while reading 1 or 0 and array values are in scope...
{
while (tape[position] != 0 && position <= 99) //while reading 1
{
READ;
cout << "\n\t=> COMMAND: move to place " << position - 1 << " and stay in S2."; //move left and stay in state 2
position--;
conFive++; //increment final halt condition
if (position < 0) //if the TM reads a number below 0
{
//cout << "\nMACHINE IS IN STATE 2! On place " << position << " the machine reads a " << tape[position] << ".";
PLACE;
cout << LEFTEND;
cout << HALT; //it halts
break;
return 0;
}
if (position == 0 && tape[position] == 0) //if position 0 is a zero (and the TM is in state 2)
{
PLACE;
cout << LEFTEND;
cout << HALT; //it halts
break;
return 0;
}
if (conFive == 5) //if final halt condition is true
{
cout << CONFINAL;
cout << HALT; //halt
break;
return 0;
}
//break;
}
while (tape[position] != 1 && position <= 99) //while reading 0s
{
//cout << "\nMACHINE IS IN STATE 2! On place " << position << " the machine reads a " << tape[position] << ".";
READ;
cout << "\n\t=> COMMAND: move to place " << position + 1 << " and move to S1."; //move right and go to state 1
position++;
stateOne(tape, position); //go to state 1
/*
HERE IS THE PROBLEM! after compiling and executing the code, going to state 1 doesn't behave as it has to.
it writes only 1 line and the program "stops".
*/
break;
}
return 0;
}
return 0;
}
|