📄 turingmachine.m
字号:
function varargout = TuringMachine(varargin)
% TURINGMACHINE M-file for TuringMachine.fig
% TURINGMACHINE, by itself, creates a new TURINGMACHINE or raises the existing
% singleton*.
%
% H = TURINGMACHINE returns the handle to a new TURINGMACHINE or the handle to
% the existing singleton*.
%
% TURINGMACHINE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TURINGMACHINE.M with the given input arguments.
%
% TURINGMACHINE('Property','Value',...) creates a new TURINGMACHINE or raises the
% existing singleton*. Starting from the left, property value pairs
% are
% applied to the GUI before TuringMachine_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to TuringMachine_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help TuringMachine
% Last Modified by GUIDE v2.5 26-Mar-2007 23:34:48
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @TuringMachine_OpeningFcn, ...
'gui_OutputFcn', @TuringMachine_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before TuringMachine is made visible.
function TuringMachine_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to TuringMachine (see VARARGIN)
% Choose default command line output for TuringMachine
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes TuringMachine wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = TuringMachine_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pbtnEnter.
function pbtnEnter_Callback(hObject, eventdata, handles)
% hObject handle to pbtnEnter (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
num1=str2num(get(handles.edtInput1,'string'));
num2=str2num(get(handles.edtInput2,'string'));
j=0;%j=2;
i=0;
nn=zeros(22,1);
global mm
mm=zeros(22,1);
%nn(1)=2;
%while num>=1 %把输入的十进制数转化为二进制数并存到一个数组(向量)里
% num=fix(num/2);
% nn(j)=n;
% j=j+1;
%end
%nn(j)=2;
%for k=1:j
% mm(j-k+1)=nn(k);
%end
mm(1)=2;
for j=2:num1+1
mm(j)=1;
end
mm(num1+2)=2;
for k=num1+3:num1+num2+2
mm(k)=1;
mm(k+1)=2;
cla(handles.axes1);
%clf(handles.axes1);
draw(j,handles.axes1);
for k=1:j
textShow(k,mm(k));
end
set(handles.pbtnStart,'enable','on');
% --- Executes on button press in pbtnStart.
function pbtnStart_Callback(hObject, eventdata, handles)
% hObject handle to pbtnStart (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.pbtnStart,'enable','inactive');
%set(handles.axes1,'enable','on');
func1;
%set(handles.text4,'');
function edtInput1_Callback(hObject, eventdata, handles)
% hObject handle to edtInput1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edtInput1 as text
% str2double(get(hObject,'String')) returns contents of edtInput1 as a double
% --- Executes during object creation, after setting all properties.
function edtInput1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edtInput1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in pmFuncChoose.
function pmFuncChoose_Callback(hObject, eventdata, handles)
% hObject handle to pmFuncChoose (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns pmFuncChoose contents as cell array
% contents{get(hObject,'Value')} returns selected item from pmFuncChoose
global autorun
autorun=get(handles.pmFuncChoose,'value');
for k=1:j
textShow(k,mm(k));
end
% --- Executes during object creation, after setting all properties.
function pmFuncChoose_CreateFcn(hObject, eventdata, handles)
% hObject handle to pmFuncChoose (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% ********************Functions of my own**************************
function func1
fin=1;
turing_q=1;
i=2;
global mm autorun
% autorun=get(handles.pmFuncChoose,'value');
while fin
line([i+0.5,i+0.5],[4,2.3],'color','blue','linewidth',2,'erasemode','xor');
text(10,5,'Q','BackgroundColor','g');
text(11,5,num2str(turing_q),'BackgroundColor','g');
switch turing_q
case 1
if mm(i)==1
arrowMove(i,1,mm(i));
i=i+1;
turing_q=2;
elseif mm(i)==0
arrowMove(i,1,mm(i));
i=i+1;
turing_q=1;
else
mm(i)=1;
arrowMove(i,-1,mm(i));
i=i-1;
turing_q=7;
end;
case 2
if mm(i)==1
arrowMove(i,1,mm(i));
i=i+1;
turing_q=2;
elseif mm(i)==0
arrowMove(i,1,mm(i));
i=i+1;
turing_q=2;
else
mm(i)=2;
arrowMove(i,1,mm(i));
i=i+1;
turing_q=3;
end;
case 3
if mm(i)==1
error('Error in q3!');
elseif mm(i)==0
arrowMove(i,1,mm(i));
i=i+1;
turing_q=3;
else
mm(i+1)=2;
mm(i)=0;
arrowMove(i,-1,mm(i));
i=i-1;
turing_q=4;
end;
case 4
if mm(i)==1
error('Error in q4!');
elseif mm(i)==0
arrowMove(i,-1,mm(i));
i=i-1;
turing_q=4;
else
mm(i)=2;
arrowMove(i,-1,mm(i));
i=i-1;
turing_q=5;
end;
case 5
if mm(i)==1
mm(i)=0;
arrowMove(i,-1,mm(i));
i=i-1;
turing_q=6;
elseif mm(i)==0
mm(i)=1;
arrowMove(i,-1,mm(i));
i=i-1;
turing_q=5;
else
error('Error in q5!');
end;
case 6
if mm(i)==1
arrowMove(i,-1,mm(i));
i=i-1;
turing_q=6;
elseif mm(i)==0
arrowMove(i,-1,mm(i));
i=i-1;
turing_q=6;
else
arrowMove(i,1,mm(i));
i=i+1;
turing_q=1;
end;
case 7
if mm(i)==1
error('Error in q7!');
elseif mm(i)==0
mm(i)=2;
arrowMove(i,-1,mm(i));
i=i-1;
turing_q=7;
else
fin=0;
text(10,5,'HLT','BackgroundColor','g');
line([i+0.5,i+0.5],[4,2.3],'color','blue','linewidth',2);
end;
otherwise
error('Error!');
end
if autorun==2
pause(0.5);
else pause;
end
end
function draw(j,handles)
%DRAW Summary of this function goes here
% 画出基本的图灵机框架图
% Detailed explanation goes here
x=0;
title('功能:求2的x次幂');
text(6,10,'单步运行时,按任意键继续');
axis([0 24 0 10],'equal','off');
rectangle('position',[0,4,22,5]);
text(8,8,'Turing Machine');
text(5,7,'All Status: Q1 Q2 Q3 Q4 Q5 Q6 Q7');
text(8,6,'Status right now:');
text(10,5,'Q','BackgroundColor','g');
text(11,5,'1','BackgroundColor','g');
line([2+0.5,2+0.5],[4,2.3],'color','blue','linewidth',2);%,'erasemode','xor');
for k=1:22
rectangle('position',[x,0.8,1,1.5]);
text(x+.3,1.5,'B','BackgroundColor','g');
x=x+1;
end
function arrowMove(ax,flag,mm)
%ARROWMOVE Summary of this function goes here
% 对磁头的左右移动作图
% Detailed explanation goes here
% 参数作用:ax是作图的起始位置(x轴坐标);
% flag是代表移动方向(1则右移,-1则左移)
% mm是磁头移动到对应位置后应该写入的值
Speed=200;
textShow(ax,mm);
ax=ax+0.5;
arrow=line([ax,ax],[4,2.3],'color','blue','erasemode','xor','linewidth',2);
for k=1:Speed
set(arrow,'xdata',[ax,ax],'ydata',[4,2.3]);
drawnow;
ax=ax+flag/Speed;
end
function textShow(tx,mm)
%DRAW Summary of this function goes here
% 在磁带的对应位置写入当前值
% Detailed explanation goes here
% 参数作用:tx是写入的位置(x轴坐标);
% mm是要写入的值;
if mm==1
text(tx+0.3,1.5,'1','BackgroundColor','g');
elseif mm==0
text(tx+0.3,1.5,'0','BackgroundColor','g');
elseif mm==2
text(tx+0.3,1.5,'B','BackgroundColor','g');
end
function edtInput2_Callback(hObject, eventdata, handles)
% hObject handle to edtInput2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edtInput2 as text
% str2double(get(hObject,'String')) returns contents of edtInput2 as a double
% --- Executes during object creation, after setting all properties.
function edtInput2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edtInput2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -