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

📄 2285dlg.cpp

📁 编译原理:构造LR(0)项目并输出其NFA状态转换矩阵 要求:构造一程序
💻 CPP
字号:
// 2285Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "2285.h"
#include "2285Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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()

/////////////////////////////////////////////////////////////////////////////
// CMy2285Dlg dialog

CMy2285Dlg::CMy2285Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy2285Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy2285Dlg)
	m_edit2 = _T("");
	m_edit1 = _T("");
	m_edit4 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy2285Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy2285Dlg)
	DDX_Control(pDX, IDC_LIST3, m_list);
	DDX_Text(pDX, IDC_EDIT2, m_edit2);
	DDX_Text(pDX, IDC_EDIT1, m_edit1);
	DDX_Text(pDX, IDC_EDIT4, m_edit4);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy2285Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy2285Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy2285Dlg message handlers

BOOL CMy2285Dlg::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
/*	
	m_list.SetExtendedStyle(LVS_EX_GRIDLINES);

	m_list.InsertColumn(0,"sfds",LVCFMT_CENTER,60);
	m_list.InsertColumn(1,"sfds",LVCFMT_CENTER,60);
	m_list.InsertColumn(2,"sfds",LVCFMT_CENTER,60);

*/
    /*
	int iii=100;
	char ch[3];
	_itoa(iii,ch,10);
	m_list.InsertItem(0,ch);
	m_list.SetItemText(0,1,"2");
	m_list.SetItemText(0,2,"d2");
	*/
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMy2285Dlg::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 CMy2285Dlg::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
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMy2285Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMy2285Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CString a[10];//获取文法
	CString str[20];//保存构造成功的项目集
	char ch[20][20];
	CString b;
	int i=0;
	int j=1;
	for(int i1=0;i1<20;i1++)
		for(int j1=0;j1<20;j1++)
	     ch[i1][j1]=' ';//保存项目之间的关系
	UpdateData(TRUE);
	b=m_edit1;
	while(-1!=b.Find("\n"))
	{
		a[i]=b.Left(b.Find("\n"));
		CString a0=a[i];
	    int len=a[i].GetLength();
		//CString strff;
		//strff.Format("%d",len);
		//MessageBox(strff);
        for(int n=a[i].Find('>')+1;n<len;n++)
		 {
		  a[i]=a0;
		  a[i].Insert(n, "·");
 		  str[j]=a[i];
		  char ss=str[j].GetAt(n+2);
		  if(ss=='$')
		  {
			  str[j].Delete(n+2,1);
			  j++;
			  break;
		  }
		  else 
		  if(ss!='\r')
		  {
			  ch[j][j+1]=ss;
		  }
		  j++;
		 }
		i++;
		b=b.Mid(b.Find("\n")+1);
	}
	
	for(int i0=1;i0<j;i0++)
	{
		CString sss=str[i0].Mid(str[i0].Find("·")+2,1);
        if(sss>='A'&&sss<='Z')
		{
			for(int j0=1;j0<j;j0++)
			{
				if(str[j0].Left(1)==sss&&str[j0].Mid(str[j0].Find('>')+1,2)=="·")
					ch[i0][j0]='$';
				
			}
		}
	}
	Print(j,str);//输出所有项目

    Print2(j,ch);//输出 状态矩阵   
	UpdateData(FALSE);
}

/*函数print:实现所有项目的输出*/
void CMy2285Dlg::Print(int n, CString s[])
{
	//UpdateData();
	CString strtmp;
	m_edit2="";
	for(int i=1;i<n;i++)
	{
		strtmp.Format("%d",i);
		m_edit2+=strtmp;
		m_edit2+='.';
		m_edit2+=s[i];
		m_edit2+="\r\n";
	}
	UpdateData(false);

}
/*函数print2:实现状态矩阵的输出*/
void CMy2285Dlg::Print2(int s, char str2[][20])
{   
    CString c;
	c=m_edit4;
	char fuhao[10];
    int F[20][10];
	int l,m;
	int len1=c.GetLength();
    for(int f=0;f<len1;f++)
		fuhao[f]=c.GetAt(f);
  
	for(int i2=1;i2<s;i2++)
	{	
		l=len1;
		for(m=0;m<10;m++)
		{
			F[i2][m]=0;
		}
		   for(int j2=1;j2<s;j2++)
		   {
			   for(int k=0;k<l;k++)
			   {
				   if(str2[i2][j2]=='$')
				   {
					F[i2][l-1]=j2;
					l++;
                    break;
				   }
				   if(str2[i2][j2]==fuhao[k])
				   {
					F[i2][k]=j2;
					break;
				   }
				
			   }
		   }
	}

    int sign0=0; 
	m_list.SetExtendedStyle(LVS_EX_GRIDLINES);
    m_list.InsertColumn(0,"状态",LVCFMT_CENTER,40);
	char ch111[2];
	ch111[1]=0;
    for(int f1=0;f1<len1;f1++)
	{
		ch111[0]=c.GetAt(f1);
		m_list.InsertColumn(f1+1,ch111,LVCFMT_CENTER,40);
	}
    for(int ia1=1;ia1<s;ia1++)
	{   
		
		for(int sign=0,ib1=len1;ib1<10;ib1++)
		{
         if(F[ia1][ib1]!=0)
			 sign++;
             
		}
		if(sign0<sign)
		sign0=sign;
   	}
	if(sign0!=0)
	{
		for(int iaa=0;iaa<sign0;iaa++)
        m_list.InsertColumn(len1+iaa,"$",LVCFMT_CENTER,40);
    }
	char ch1[1],ch2[1];
	for(int f2=1;f2<s;f2++)
	{   
		_itoa(f2,ch1,10);
		m_list.InsertItem(f2-1,ch1);
	}

	for(int ia=1;ia<s;ia++)
		for(int ib=0;ib<10;ib++)
		{   
			
			if(F[ia][ib]!=0)
			{   
				if(ib<len1)
				{
					_itoa(F[ia][ib],ch2,10);
				    m_list.SetItemText(ia-1,ib+1,ch2);
				}
			    if(ib>=len1)
                {   
					_itoa(F[ia][ib],ch2,10);
                                    
					m_list.SetItemText(ia-1,ib+1,ch2);
				}
			}
			  
		}
}

void CMy2285Dlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

/*清空所有控件区域*/
void CMy2285Dlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	m_edit1.Empty();
    m_edit2.Empty();
    m_edit4.Empty();
    for(int ic=0;ic<10;ic++)
	m_list.DeleteColumn(0);
	m_list.DeleteAllItems();
	UpdateData(FALSE);
}

/*读入一个指定格式的文件*/
void CMy2285Dlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	char szFilters[]=
      "文法文件 (*.txt)|*.txt|All Files (*.*)|*.*||";
	CFileDialog fdlg (TRUE, "txt", "*.txt",
      OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
	if(fdlg.DoModal() == IDOK)
	{
		OnClear();
		CString pathName = fdlg.GetPathName();
		CStdioFile InFile;
		InFile.Open(pathName,CFile::modeRead);
		CString temp;
		InFile.ReadString(temp);
		if (temp != "[Grammar]")
		{
			MessageBox("文法文件有误!","错误",MB_OK|MB_ICONSTOP);
			InFile.Close();
			return;
		}
		do
		{
			InFile.ReadString(temp);
			if(temp=="") 
				break;
			m_edit1+=temp;
			m_edit1+="\r\n";
		}while(true);
		
		InFile.ReadString(temp);
		if (temp != "[Character]")
		{
			MessageBox("文法文件有误!","错误",MB_OK|MB_ICONSTOP);
			InFile.Close();
			return;
		}
		do
		{
			InFile.ReadString(temp);
			if(temp=="") 
				break;
			m_edit4=temp;

		}while(true);
		InFile.ReadString(temp);
		InFile.Close();
	}
        UpdateData(false);
}

void CMy2285Dlg::OnClear()
{
    m_edit1="";
	m_edit2="";
    m_edit4="";
	for(int ic=0;ic<10;ic++)
	m_list.DeleteColumn(0);
	m_list.DeleteAllItems();
}

/*将从控件区中输入的文法保存为txt文件*/
void CMy2285Dlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	char szFilters[]=
      "文法文件 (*.txt)|*.txt|All Files (*.*)|*.*||";

	CFileDialog fdlg (FALSE, "txt", "*.txt",
       OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilters, this);
	if(fdlg.DoModal()==IDOK)
	{
		CString pathName = fdlg.GetPathName();
		CStdioFile OutFile;
		OutFile.Open(pathName,CFile::modeCreate | CFile::modeWrite);
		CString t;
		OutFile.WriteString("[Grammar]\n");
        CString tt;
		tt=m_edit1;
		//int i=0;
    	while(-1!=tt.Find('\n'))
		{
		
			t+=tt.Left(tt.Find('\n')-1);
			t+="\n";
			tt=tt.Mid(tt.Find('\n')+1);
		}
        OutFile.WriteString(t);

		OutFile.WriteString("\n[Character]\n");
        CString ttt;
		ttt=m_edit4;
      	OutFile.WriteString(ttt);

		OutFile.Close();
	} 
	
}


⌨️ 快捷键说明

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