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

📄 automtestview.cpp

📁 NFA自动机的实验
💻 CPP
字号:
// AutomTestView.cpp : implementation of the CAutomTestView class
//

#include "stdafx.h"
#include "AutomTest.h"

#include "AutomTestDoc.h"
#include "AutomTestView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAutomTestView

IMPLEMENT_DYNCREATE(CAutomTestView, CFormView)

BEGIN_MESSAGE_MAP(CAutomTestView, CFormView)
	//{{AFX_MSG_MAP(CAutomTestView)
	ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAutomTestView construction/destruction

CAutomTestView::CAutomTestView()
	: CFormView(CAutomTestView::IDD)
{
	//{{AFX_DATA_INIT(CAutomTestView)
	m_testStr = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CAutomTestView::~CAutomTestView()
{
}

void CAutomTestView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAutomTestView)
	DDX_Text(pDX, IDC_EDIT_TEST, m_testStr);
	//}}AFX_DATA_MAP
}

BOOL CAutomTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CAutomTestView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	
	bAutomReady=false;

	pos[0].x= 70;
	pos[0].y=180 ;
	for(int i=1;i<30;i++)
	{
		pos[i].x= 50+100*(1+(i-1)%3 )+30 * (i%2-1);
		pos[i].y=150*(1+(i-1)/3)+30 * (i%2-1) ;
	}
/*
	if( !autom.InitFormFile("a.txt") )
		return ;	
	else
		autom.CreateGraphic();
*/
}

/////////////////////////////////////////////////////////////////////////////
// CAutomTestView printing

BOOL CAutomTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CAutomTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CAutomTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CAutomTestView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CAutomTestView diagnostics

#ifdef _DEBUG
void CAutomTestView::AssertValid() const
{
	CFormView::AssertValid();
}

void CAutomTestView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CAutomTestDoc* CAutomTestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAutomTestDoc)));
	return (CAutomTestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAutomTestView message handlers

void CAutomTestView::OnButtonTest() 
{
	// TODO: Add your control notification handler code here
	if( !bAutomReady)
	{	
		MessageBox("Please input the statesTable file at first ! \n File-->Open");
		return;
	}

	UpdateData(true);
	
//	Invalidate();

	autom.Proc(m_testStr.GetBuffer(m_testStr.GetLength()),m_testStr.GetLength());
	
	CDC *pDC=GetDC();
	OnDraw(pDC);
	pDC->TextOut(580,50,autom.GetErrors().c_str() );
//	pDC->TextOut(480,50,autom.GetPosInfo().c_str() );
//	CString info=autom.GetErrors().c_str();
	
	int states[50];
	string str=autom.GetErrors().c_str();
	int n=1,mh=0,mt=0;
	while ( (mt=str.find(',',mh)) >= 0 )
	{
		cout<<str.substr(mh,mt-mh)<<',' ;
		string strState=str.substr(mh,mt-mh);
		mh=mt+1;
		int gi=atoi( strState.c_str() );
		if ( gi <= 0 ) 
		{
			continue;
		}
		states[n]=gi;
		n++;
	}
	mt=str.length();
	string strState=str.substr(mh,mt-mh);
	int gi=atoi( strState.c_str() );
	if ( gi >= 0 ) 
	{
		states[n]=gi;
	}
	else
		n--;
	states[0]=n;
	CPen pen(PS_SOLID,2,RGB(255,0,0)),* pOldPen;
	pOldPen=pDC->SelectObject(&pen);	
	for(int ipos=1;ipos<=states[0];ipos++)
	{
		int px=pos[states[ipos]].x,py=pos[states[ipos]].y;
		pDC->Ellipse(px-18,py-18,px+18,py+18 );
		CString str;
		str.Format("%d",states[ipos]);
		pDC->TextOut(px-5,py-5,str);
	}
	pDC->SelectObject(pOldPen);
}

void CAutomTestView::OnDraw(CDC* pDC) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	if( !bAutomReady)
		return;

	CPen pen(PS_SOLID,1,RGB(255,0,0)),* pOldPen;
	pOldPen=pDC->SelectObject(&pen);
	

	int rgb=0;
	for( int n=1;n<=autom.GetLine();n++)
	{
		for( int m=1; m<=autom.GetLine(); m++)
		{	
			if (autom.GetGraphic(n,m) != '~' )
			{
				rgb++;
				CPen pen(PS_SOLID,1,RGB(50*(rgb%5),50*(rgb%3),255-50*(rgb%5)) );
				pDC->SelectObject(&pen);
				pDC->MoveTo( pos[n-1] );
				pDC->LineTo( pos[m-1] );				
				pDC->TextOut( (pos[n-1].x +pos[m-1].x)/2 , (pos[n-1].y +pos[m-1].y)/2 -18,(char)autom.GetGraphic(n,m) );
			}
		}
	}
	pDC->SelectObject(pOldPen);
	
	for( int i=0;i<autom.GetLine();i++)
	{
		int px=pos[i].x,py=pos[i].y;
		if((char)autom.GetValue(i+1,0,0) =='*' )
			pDC->Ellipse(px-20,py-20,px+20,py+20 );
		pDC->Ellipse(px-18,py-18,px+18,py+18 );
		CString str;
		str.Format("%d",i);
		pDC->TextOut(px-5,py-5,str);
	}
	
//	pDC->Ellipse(300,300,300+5,300+5 );
}

void CAutomTestView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	char szFilter[]="文本文件 (*.txt)|*.txt|";
	CFileDialog* dlg=new CFileDialog(TRUE,"*.txt",NULL,
		OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter,NULL);

	if (dlg->DoModal()==IDOK)
	{
		CString fileName=dlg->GetFileName();
		if( autom.InitFormFile( fileName.GetBuffer(fileName.GetLength()) ))
		{
			autom.CreateGraphic();
			string file;
			file += fileName.GetBuffer( fileName.GetLength() );	
			file +=".ini";
			autom.Output(file);
			bAutomReady=true;
		}
		else
			MessageBox("Open the file failed !");
	}
	Invalidate();
}

void CAutomTestView::OnFileNew() 
{
	// TODO: Add your command handler code here
	MessageBox("HAVE NOT BEEN READY!");
}

⌨️ 快捷键说明

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