📄 tringdlg.cpp
字号:
// tringDlg.cpp : implementation file
//
#include "stdafx.h"
#include "tring.h"
#include "tringDlg.h"
#include"MaxStep.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int down=15;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#include"InputDialog.h"
#include"SetState.h"
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTringDlg dialog
//#include"CGrid.h"
CTringDlg::CTringDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTringDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTringDlg)
m_input = _T("");
start_state="q1";
flag=0;
maxstep=500;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTringDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTringDlg)
DDX_Text(pDX, IDC_EDIT1, m_input);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTringDlg, CDialog)
//{{AFX_MSG_MAP(CTringDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONUP()
ON_WM_TIMER()
ON_COMMAND(ID_Run, OnRun)
ON_COMMAND(ID_Set_Tape, OnSetTape)
ON_COMMAND(ID_MENUITEM32771, OnMenuitem32771)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_Run_Step, OnRunStep)
ON_COMMAND(ID_reset, Onreset)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_HELP, OnHelp)
ON_COMMAND(ID_MaxStep, OnMaxStep)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTringDlg message handlers
BOOL CTringDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CTringDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTringDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this),*mydc;
mydc=&dc;
draw_grid(mydc);
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTringDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTringDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
/* CString str;
str+=int_string(point.x);
str+=",";
str+=int_string(point.y);
MessageBox(str);
*/
CDialog::OnLButtonUp(nFlags, point);
}
CString CTringDlg::modify(CString str)
{
CString out;
int flag=0;
char a;
for(int i=0;i<str.GetLength();i++)
{
a=str.GetAt(i);
if(flag==0)
{
if(a!='B')
{
flag=1;
}
}
if(flag==1)
{
if(a=='B')
{
flag=0;
break;
}
out+=a;
}
}
return out;
}
CString CTringDlg::int_string(int lo)
{
CString str="";
int t=lo;
int i;
while(t!=0)
{
i=t%10;
str+='0'+i;
t=t/10;
}
str.MakeReverse();
return str;
}
void CTringDlg::draw_grid(CPaintDC *dc)
{
/* int x,y,off_len;
for(int i=0;i<20;i++)
{
if(i<8)
off_len=0;
else
off_len=64;
/* if(i==8)
{
x=12+34*i;
y=17+down;
dc->MoveTo(x,y);
y=51+down;
dc->LineTo(x,y);
}*/
/* x=12+34*i+off_len;
y=17+down;
dc->MoveTo(x,y);
y=51+down;
dc->LineTo(x,y);
}
dc->MoveTo(12,17+down);
dc->LineTo(6+34*8,17+down);
dc->MoveTo(12+34*8+64,17+down);
dc->LineTo(696+64,17+down);
// dc->LineTo(696,17);
dc->LineTo(696+64,51+down);
dc->LineTo(12+34*8+64,51+down);
dc->MoveTo(6+34*8,51+down);
dc->LineTo(12,51+down);
dc->LineTo(12,17+down);*/
grid.draw_grid(dc);
grid.draw_box(dc);
grid.draw_tape(dc);
}
void CTringDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this),*mydc;
mydc=&dc;
CString str,local_str1;
if(grid.get_run_over())
{
KillTimer(1);
str+="运行";
str+=int_string(grid.GetNumStep());
str+="步;输出:";
local_str1=grid.get_input_tape();
local_str1=modify(local_str1);
str+=local_str1;
AfxMessageBox(str);
}
else
{
if(grid.GetNumStep()>=maxstep)
{
KillTimer(1);
str="运行超过了规定的步数,系统退出。可以在菜单中设置更大的运行步数";
AfxMessageBox(str);
}
else
grid.run(mydc);
}
CDialog::OnTimer(nIDEvent);
}
void CTringDlg::OnRun()
{
// TODO: Add your command handler code here
UpdateData(true);
if(m_input.GetLength()>0)
{
CClientDC dc(this),*mydc;
mydc=&dc;
grid.draw(mydc);
grid.set_four_team(m_input);
SetTimer(1,1000,NULL);
}
else
{
AfxMessageBox("机器没准备好");
}
}
CGrid::CGrid()
{
ReSet();
four_team="";
len_state=0;
tape_index=0;
start_state_index=0;
run_over=false;
input_tape="BBBBBBBBBBBBBBBBBBBBBB";
synchronization();
current_state=100;
next_state=100;
graph_flag=0;
start_state="q1";
tape_index_lasttime=0;
left_tape=0;
NumStep=0;
}
void CGrid::ReSet()
{
for(int i=0;i<20;i++)
tape[i]='B';
}
void CGrid::trans_state()
{
CTuple state[400];
int index_state=0;
CString str;
char tmp;
int len=four_team.GetLength();
for(int i=0;i<=len;i++)
{
if(i<len)
tmp=four_team.GetAt(i);
if(tmp=='\r'||tmp=='\n'||i==len)
{
// int len=str.GetLength();
if(str.GetLength()>0)
{
trans_state(str,state[index_state]);
index_state++;
str.Empty();
}
}
else
str+=tmp;
}
check_tuple(state,index_state);
len_state=tuple_state(state,index_state,g_state);
}
void CGrid::trans_state(CString str,CTuple &state)
{
int index,local_index=0,local_flag;
CString local_str=str,local_str1;
while(local_index<4)
{
index=local_str.Find(' ');
if(index>0||(local_index==3&&local_str.GetLength()>0))
{
if(index<0&&local_index==3)
local_str1=local_str;
else
{
local_str1=get_string(local_str,index);
local_str.Delete(0,index+1);
}
switch(local_index)
{
case 0:
state.set_state(local_str1);
break;
case 1:
state.set_input(local_str1);
break;
case 2:
local_flag=string_int(local_str1);
if(local_flag==0)
{
state.set_flag(local_flag);
state.set_output(local_str1);
}
else
{
state.set_flag(local_flag);
state.set_output(local_str1);
}
break;
case 3:
state.set_next_state(local_str1);
break;
default:
AfxMessageBox("function: trans_state CString str,CState state wrong");
break;
}
local_index++;
}
else
{
local_str.Delete(0,1);
}
}
}
CString CGrid::get_string(CString str,int len)
{
CString local;
for(int i=0;i<len;i++)
local+=str.GetAt(i);
return local;
}
void CGrid::check_tuple(CTuple tuple[],int &len)
{
CList<int,int> loc_list;
int tmp;
if(len>=0)
loc_list.AddHead(0);
for(int i=0;i<len;i++)
{
for(int j=0;j<i;j++)
{
if(tuple[i].cmp(tuple[j])==-1)
{
if(loc_list.Find(i)==NULL)
loc_list.AddTail(i);
}
}
}
len=loc_list.GetCount();
for(i=0;i<len;i++)
{
POSITION pos;
pos=loc_list.FindIndex(i);
tmp=loc_list.GetAt(pos);
if(tmp!=i)
tuple[i].copy(tuple[tmp]);
}
}
int CTuple::cmp(CTuple tmp)
{
CString str1,str2;
str1=tmp.get_state();
str2=tmp.get_input();
if(state.Compare(str1)==0&&input.Compare(str2)==0)
return 0;
else
return -1;
}
int CGrid::tuple_state(CTuple tuple[],int len_tuple,CState state[])
{
int len=get_num_state(tuple,len_tuple,state);
int index;
for(int i=0;i<len_tuple;i++)
{
index=get_state_index(tuple[i],state,len);
insert_line(tuple[i],&state[index],len);
}
return len;
}
void CTuple::copy(CTuple tuple)
{
flag=tuple.get_flag();
input=tuple.get_input();
next_state=tuple.get_next_state();
output=tuple.get_output();
state=tuple.get_state();
}
int CGrid::get_state_index(CTuple tuple,CState state[],int len)
{
CString str=tuple.get_state();
for(int i=0;i<len;i++)
{
if(str.Compare(state[i].get_state())==0)
return i;
}
return i;
}
int CGrid::get_num_state(CTuple tuple[],int len_tuple,CState state[])
{
int index=0;
for(int i=0;i<len_tuple;i++)
{
if(get_state_index(tuple[i],state,index)>=index)
{
state[index].set_state(tuple[i].get_state());
index++;
}
}
return index;
}
void CGrid::insert_line(CTuple tuple,CState *state,int num_state)
{
COperate *local;
local=new COperate(tuple);
CTuple *l_tuple=new CTuple;
l_tuple->set_state(tuple.get_next_state());
int index=get_state_index(*l_tuple,g_state,num_state);
local->set_net_state(&g_state[index]);
state->get_list()->AddTail(local);
}
COperate::COperate(CTuple tuple)
{
flag=tuple.get_flag();
input=tuple.get_input();
output=tuple.get_output();
next_state=NULL;
}
int CGrid::string_int(CString str)
{
int out=0,len=str.GetLength(),flag=0;
char a;
/* for(int i=0;i<len;i++)
{
a=str.GetAt(i);
if('0'<=a&&a<='9')
{
out*=10;
out+=a-'0';
}
}*/
if(str.GetLength()==1)
{
a=str.GetAt(0);
if(a=='L'||a=='l')
flag=-1;
else
{
if(a=='R'||a=='r')
flag=1;
}
}
return flag;
}
void CTringDlg::OnSetTape()
{
// TODO: Add your command handler code here
CInputDialog input;
CString str;
CClientDC dc(this),*mydc;
mydc=&dc;
input.set_input(grid.get_string());
if(input.DoModal()==IDOK)
{
str=input.m_input;
tape=str;
grid.set_input(str);
grid.draw(mydc);
}
}
void CTringDlg::OnMenuitem32771()
{
// TODO: Add your command handler code here
CSetState state;
CClientDC dc(this),*mydc;
mydc=&dc;
if(state.DoModal()==IDOK)
{
start_state=state.m_start_state;
}
grid.set_start_state(start_state);
grid.draw(mydc);
}
void CGrid::set_tape()
{
}
void CGrid::run(CDC *dc)
{
tape_index_lasttime=tape_index;
initial_tape(left_tape);
CString str;
COperate *local;
CState *next;
if(current_state==100)
{
if(start_state.GetLength()==0)
start_state=g_state[0].get_state();
CTuple *tuple=new CTuple;
tuple->set_state(start_state);
current_state=next_state=get_state_index(*tuple,g_state,len_state);
delete tuple;
}
if(input_tape.GetLength()>0)
{
str+=input_tape.GetAt(left_tape+tape_index);
current_state=next_state;
local=g_state[current_state].find_next_state(str);
if(local!=NULL)
{
next=local->get_next_state();
next_state=find_index_of_state(next);
modify_tape(local);
NumStep++;
if(next_state==100)
{
run_over=true;
tape_index++;
initial_tape(left_tape);
}
}else
{
run_over=true;
// tape_index++;
// initial_tape(left_tape);
}
}
draw(dc);
}
COperate *CState::find_next_state(CString str)
{
int len=list.GetCount();
POSITION pos;
COperate * operate,*out=NULL;
for(int i=0;i<len;i++)
{
pos=list.FindIndex(i);
operate=list.GetAt(pos);
if(str.Compare(operate->get_input())==0)
out=operate;
}
return out;
}
int CGrid::find_index_of_state(CState *local)
{
int index=100;
for(int i=0;i<len_state;i++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -