⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cgrid.cpp

📁 四元图灵机程序
💻 CPP
字号:
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include"CGrid.h"

CGrid::CGrid()
{
	ReSet();
	four_team="";
	len_state=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++)
	{
		tmp=four_team.GetAt(i);
		if(tmp=='\r'||tmp=='\n')
		{
			if(str.GetLength()>0)
			{
				trans_state(str,state[index_state]);
				index_state++;
				str="";
			}
		}
		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_str1=get_string(local_str,index);
			local_str.Delete(0,index);
			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_output(local_str1);
				else
					state.set_flag(local_flag);
				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;
	for(int i=0;i<len;i++)
	{
		for(int j=0;j<i;j++)
		{
			if(tuple[i].cmp(tuple[j])==-1)
				loc_list.AddHead(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;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 0;
}
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();
	char a;
	for(int i=0;i<len;i++)
	{
		a=str.GetAt(i);
		if('0'<=a&&a<='9')
		{
			out*=10;
			out+=a-'0';
		}
	}
	return out;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -