📄 tut_machine.m.txt
字号:
function Tut_Machine()
global buffer %建立数组
global pos %磁头位置
global status
global unit
global data
global s_flag
global ox
global oy
global h_data
global s_text
global size
global inc
global disp_sta
global disp_end
global trans_arry
global buf_end
global ysize
size=18 %数组长度会变,但显示的永远是下标为1:size的内容
ysize=floor(size/3) %取整数部分
inc=floor(size/3) %数组不够时增加的数目
trans_array=[struct('write_data',0,'direction',1,'new_status',1),struct('write_data',1,'direction',1,'new_status',2),struct('write_data',1,'direction',0,'new_status',7);
struct('write_data',0,'direction',1,'new_status',2),struct('write_data',1,'direction',1,'new_status',2),struct('write_data',2,'direction',1,'new_status',3);
struct('write_data',0,'direction',1,'new_status',3),struct('write_data',3,'direction',3,'new_status',-1),struct('write_data',0,'direction',0,'new_status',4);
struct('write_data',0,'direction',0,'new_status',4),struct('write_data',4,'direction',3,'new_status',-1),struct('write_data',2,'direction',0,'new_status',5);
struct('write_data',1,'direction',0,'new_status',5),struct('write_data',0,'direction',0,'new_status',6),struct('write_data',5,'direction',3,'new_status',-1);
struct('write_data',0,'direction',0,'new_status',6),struct('write_data',1,'direction',0,'new_status',6),struct('write_data',2,'direction',1,'new_status',1);
struct('write_data',2,'direction',0,'new_status',7),struct('write_data',6,'direction',3,'new_status',-1),struct('write_data',2,'direction',3,'new_status',-1)]
bin_num=['1','0','0','0'] %存储自变量的二进制数
%对窗口初始化,包括画矩阵,对每个待显示字符赋给句柄
axis([0,size,0,ysize])
hold on
Y=3
ox=0.3;oy=0.5; %分别是数字位置的偏移
for i=1:size
unit(i)=rectangle('Position',[i-1,Y,1,1])
h_data(i)=text(i+ox-1,Y+oy,' ')
end
%对buffer数组初始化
buf_end=size
disp_sta=1
disp_end=size
buffer=init_space(size)
pos=floor(size/2)
for i=1:length(bin_num)
buffer(pos)=bin_num(i)
pos=pos+1
end
pos=floor(size/2) %初始化后,指示变量的首位
%从头到尾显示所有的数字
for k=disp_sta:disp_end %从头到尾显示所有的数字
if strcmp(buffer(k),'2')==1 %如果是2,显示为B
set(h_data(k),'string','B')
else
set(h_data(k),'string',buffer(k))
end
end
pause %进入键盘主控状态
status=1
s_text=text(10,5,' ')
%set(s_text,'String','status:S1')
while(status>0)
st=strcat('status:S',int2str(status))
set(s_text,'String',st)
r_data=str2num(buffer(pos)) %读数据
write_data=trans_array(status,r_data+1).write_data
move_direction=trans_array(status,r_data+1).direction
write(write_data)
pause(0.1)
if move_direction==0
move_left
elseif move_direction==1
move_right
end
status=trans_array(status,r_data+1).new_status
end
stop=0 %控制循环结束
flag=0 %标记是否需要重新显示
%最左端值不为空,则往右移
if (strcmp(buffer(disp_sta),'2')==0)&(disp_sta>1)
flag=1
k=disp_sta-1
while (k>=1)&( ~stop)
if strcmp(buffer(k),'2')==0
stop=1
end
if( ~stop)
k=k-1
end
end
m_bits=disp_sta-k-1
disp_sta=disp_sta-m_bits
disp_end=disp_end-m_bits
end
%最右端值不为空,则往左移
if (strcmp(buffer(disp_end),'2')==0)&(disp_end<buf_end)
flag=1
k=disp_end+1
while (k<=buf_end)&( ~stop)
if strcmp(buffer(k),'2')==0 %查找最右端第一个空值的位置
stop=1
end
if( ~stop)
k=k+1
end
end
m_bits=k-1-disp_end
disp_sta=disp_sta+m_bits
disp_end=disp_end+m_bits
end
if (flag>0) %最左端和最右端如果有从头到尾显示所有的数字
c=2
for k=disp_sta:disp_end
f=1
if strcmp(buffer(k),'2')==1 %如果是2,显示为B
set(h_data(k-disp_sta+1),'string','B')
else
set(h_data(k-disp_sta+1),'string',buffer(k))
end
end
end
function space=init_space(num) %初始化数组
space(1:num)='2'
function write(x)
global buffer
global pos
global h_data
global disp_sta
buffer(pos)=int2str(x);
switch x
case 0
set(h_data(pos-disp_sta+1),'string','0')
case 1
set(h_data(pos-disp_sta+1),'string','1')
case 2
set(h_data(pos-disp_sta+1),'string','B')
end
end
function r=read()
global pos
global buffer
r=str2num(buffer(pos))
function move_left()
global pos
global disp_sta
global disp_end
global size
global h_data
global buffer
global buf_end
global inc
global inc_buffer
if(pos>disp_sta) pos=pos-1
else
if(pos>1) %pos==disp_sta的情况
disp_sta=disp_sta-inc
disp_end=disp_end-inc
pos=pos-1
else
inc_buffer(1:inc)='2'
buffer=[inc_buffer,buffer] %每次pos=1时,就增加数组长度,两边各增加inc_buffer个单位
buf_end=buf_end+inc
disp_sta=1
disp_end=disp_sta+size-1
pos=inc+pos-1
end
for k=disp_sta:disp_end %从头到尾显示所有的数字
if strcmp(buffer(k),'2')==1 %如果是2,显示为B
set(h_data(k-disp_sta+1),'string','B')
else
set(h_data(k-disp_sta+1),'string',buffer(k))
end
end
end
function move_right()
global pos
global disp_sta
global disp_end
global size
global h_data
global buffer
global buf_end
global inc
global inc_buffer
if(pos<disp_end) pos=pos+1
else
if pos<buf_end
pos=pos+1
disp_sta=disp_sta+inc
disp_end=disp_end+inc
else %如果pos达到buf_end,就要扩充数组
inc_buffer(1:inc)='2'
buffer=[buffer,inc_buffer]
buf_end=buf_end+inc
%pos=pos+inc+1
disp_end=buf_end
disp_sta=disp_end-size+1
end
for k=disp_sta:disp_end %从头到尾显示所有的数字
if strcmp(buffer(k),'2')==1 %如果是2,显示为B
set(h_data(k-disp_sta+1),'string','B')
else
set(h_data(k-disp_sta+1),'string',buffer(k))
end
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -