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

📄 cgrid.h

📁 四元图灵机程序
💻 H
字号:
#pragma once
#ifndef _CGrid
#define _CGrid
class CState;
class CTuple;
#include <afxtempl.h>
class COperate
{
private:
	CString input;
	int flag;//flag=-1 left;  flag=1 right; flag=0 set
	CString output;
	CState *next_state;
public:
	COperate(CTuple tuple);
	~COperate();
	void set_input(CString str){input=str;}
	void set_flag(int l){flag=l;}
	void set_output(CString a){output=a;}
	void set_net_state(CState *next){next_state=next;}
};
class CState
{
private:
	CString state;
	CList<COperate *,COperate*> list;
public:
	CState(){};
	~CState(){};
	CString get_state(){return state;}
	void set_state(CString str){state=str;}
	CList<COperate *,COperate*> *get_list(){return &list;}
};
class CTuple
{
private:
	CString state;
	CString input;
	int flag;//flag=-1 left;  flag=1 right; flag=0 set
	CString output;
	CString next_state;
public:
	CTuple(){};
	~CTuple(){};	
	void set_state(CString str){state=str;}
	void set_input(CString a){input=a;}
	void set_flag(int tras){flag=tras;}
	void set_output(CString a){output=a;}
	void set_next_state(CString str){next_state=str;}
	int cmp(CTuple tmp);
	CString get_state(){return state;}
	CString get_input(){return input;}
	int get_flag(){return flag;}
	CString get_output(){return output;}
	CString get_next_state(){return next_state;}
	void copy(CTuple tuple);
};
class CGrid
{
private:
	char tape[20];
	CString four_team;
	CState g_state[100];
	int len_state;
	CString start_state;
	int start_state_index;
public:
	CGrid();
	~CGrid(){};
	void ReSet();
	void input(CString str){four_team=str;}
	void Draw();
	void trans_state();
	void trans_state(CString str,CTuple state);
	CString get_string(CString str,int len);
	void check_tuple(CTuple tuple[],int &len);
	int tuple_state(CTuple tuple[],int len_tuple,CState state[]);
	int get_state_index(CTuple tuple,CState state[],int len);
	int get_num_state(CTuple tuple[],int len_tuple,CState state[]);
	void insert_line(CTuple tuple,CState* state,int num_state);
	int string_int(CString str);
};
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;
}

#endif

⌨️ 快捷键说明

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