//Universal Turing Machine_3 Tapes Operation //Kwang J. Yi //CIS 203 //Intro To Artificial Intelligence //11/27/05 //Rough Draft #include #include #define MAX 100 char tape[3][MAX]; char temp[MAX]; int set(void); int right_find(int tnum, int hloc, char key); int left_find(int tnum, int hloc, char key); int right_not_find(int tnum, int hloc, char key); int left_not_find(int tnum, int hloc, char key); int shift_right(int tnum, int hloc); int shift_left(int tnum, int hloc); int print_result(void); int start(void); int copydata(void); int find_current_state(void); int check_current_symbol(void); int change_state(void); int exe_transition(void); int write_symbol(void); int check_halt(void); int loc0, loc1, loc2; FILE *fp; void main(void) { fp=fopen("turingout.txt","w"); printf("input binary code\n"); scanf("%s",temp); fprintf(fp,"###############################################\n"); fprintf(fp," UNIVERSAL TURING MACHINE \n"); fprintf(fp," Code by Kwang J. Yi \n"); fprintf(fp," 2005.11.27 \n"); fprintf(fp,"###############################################\n"); fprintf(fp,"\nTapes\n"); fprintf(fp,"\ntape0 inputed code\n"); fprintf(fp,"tape1 data code by tape0's data part\n"); fprintf(fp,"tape2 current state\n"); set(); start(); fclose(fp); } int set(void) { //setting of tapes. 'd' is delta. int i,j; tape[0][0]='d'; //tape0's first blank is delta. this delta represent starting point of tape. for(i=1; i=0 ; n--) { if(tape[tnum][n] == key) { find_loc=n; break; } } if(find_loc == -1) { fprintf(fp,"right not found error!! \n"); exit(0); } return find_loc; } int left_not_find(int tnum, int hloc, char key) { //search its tape 'tnum' to the right of the current //location 'hloc' for a symbol other than 'key' int n, find_loc = -1; for( n=hloc-1 ; n>=0 ; n--) { if(tape[tnum][n] != key) { find_loc=n; break; } } if(find_loc == -1) { fprintf(fp,"left not found error!!\n"); exit(0); } return find_loc; } int shift_right(int tnum, int hloc) { //shifts the string of nonblank symbols found at //the left of the current cell one cell to the right. char val1,val2; tape[tnum][hloc]='d'; hloc--; if(tape[tnum][hloc] == 'd') { hloc++; return hloc; } val1=tape[tnum][hloc]; while(1) { hloc--; if(hloc <0 ) { hloc++; tape[tnum][hloc]='d'; hloc=right_find(tnum,hloc,'d'); tape[tnum][hloc]=val1; return hloc; } if(tape[tnum][hloc]=='d'){ hloc++; tape[tnum][hloc]='d'; hloc=right_find(tnum,hloc,'d'); tape[tnum][hloc]=val1; return hloc; } val2=tape[tnum][hloc]; hloc++; tape[tnum][hloc]=val2; hloc--; } } int shift_left(int tnum, int hloc) { //shifts the string of nonblank symbols found at //the right of the current cell one cell to the left. char val1,val2; tape[tnum][hloc]='d'; hloc++; if(tape[tnum][hloc] == 'd') { hloc--; return hloc; } val1=tape[tnum][hloc]; while(1) { hloc++; if(hloc==MAX ) { hloc--; tape[tnum][hloc]='d'; hloc=left_find(tnum,hloc,'d'); tape[tnum][hloc]=val1; return hloc; } if(tape[tnum][hloc]=='d'){ hloc--; tape[tnum][hloc]='d'; hloc=left_find(tnum,hloc,'d'); tape[tnum][hloc]=val1; return hloc; } val2=tape[tnum][hloc]; hloc--; tape[tnum][hloc]=val2; hloc++; } } int print_result(void) { //prints current tapes, header's location int m,n; fprintf(fp,"\nloc0=%d loc1=%d loc2=%d\n",loc0,loc1,loc2); for(m=0; m<3 ; m++) { for(n=0; n