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

📄 fifoexedlg.cpp

📁 chifafenxiqi chifafenxiqi chifafenxiqi chifafenxiqi chifafenxiqi chifafenxiqi
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// fifoexeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "fifoexe.h"
#include "fifoexeDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char firstvt[100][100];
char lastvt[100][100];
char lr[100][100];
int flagfirstvt=0,flaglastvt=0,flaglr=0;

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

/////////////////////////////////////////////////////////////////////////////
// CFifoexeDlg dialog

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

void CFifoexeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFifoexeDlg)
	DDX_Control(pDX, IDC_LIST3, m_ListTableLR);
	DDX_Control(pDX, IDC_LIST2, m_ListTableLastVT);
	DDX_Control(pDX, IDC_LIST1, m_ListTable);
	DDX_Text(pDX, IDC_EDIT1, m_Source);
	DDX_Text(pDX, IDC_EDIT2, m_sentence);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFifoexeDlg, CDialog)
	//{{AFX_MSG_MAP(CFifoexeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON2, OnButtonFirst)
	ON_BN_CLICKED(IDC_BUTTON3, OnButtonFollow)
	ON_BN_CLICKED(IDC_BUTTON4, OnLR)
	ON_BN_CLICKED(IDC_BUTTONAnalyse, OnBUTTONAnalyse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFifoexeDlg message handlers

BOOL CFifoexeDlg::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_ListTable.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
	m_ListTableLastVT.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
	m_ListTableLR.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFifoexeDlg::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 CFifoexeDlg::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 CFifoexeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CFifoexeDlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	char str[500],*p;
	CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,"Text Files (*.txt;*.wf)|*.txt;*.wf");//设置读取的文件类型, 弹出打开文件对话框
	if(!m_Source.IsEmpty())
	{
		m_Source.Empty();
	}
	if(dlg.DoModal() == IDOK)
	{
		m_strFileNamePath = dlg.GetPathName();  //取文件的路径
		m_FileInput.open((char*)m_strFileNamePath.GetBuffer(0),ios::nocreate);//得到文件的带路径的文件名
		if(m_FileInput.fail())
			AfxMessageBox("无法打开指定的文件!\n请检查您输入的文件名是否正确!", MB_YESNO|MB_ICONSTOP);
		else
			do{
				m_FileInput.getline(str,128,'\n');//读入一行,或者最大128个字符
				m_Source += str;//读入的内容加到对应源文件的变量最后
		    	p=str;
				while(*(p+1)!='\0')
					p++;
				if(*p=='\r')
					m_Source += '\n';//处理回车和换行标志
				else
					m_Source += "\r\n";
			}	while(!m_FileInput.eof()); //没有结束继续读

			UpdateData(FALSE);//写入窗口控件中
			m_FileInput.close();//关闭文件
	}
	
}

/***************************************************************************/
/*  预处理函数:将文法改造,去掉多余的空格和回车,结果仍然放在m_source中   */
/***************************************************************************/
CString Pre_Process(CString m_Source)
{
	char str[100];
    int SourceLen,SourcePointer=0;  //用于扫描源符号串
	int i=0;
    SourceLen=m_Source.GetLength();
	while( isspace(m_Source[SourcePointer]))  
		SourcePointer++; //去掉产生式前面的多余的回车
	while(SourcePointer < SourceLen) //去掉产生式中多余的空格
		{
			while( (m_Source[SourcePointer]==' ')||(m_Source[SourcePointer]=='\t'))  SourcePointer++;
			while ( (m_Source[SourcePointer]=='\r')&&( (SourcePointer+2 < SourceLen)&&(m_Source[SourcePointer+2]=='\r')  ) )
				SourcePointer+=2;
			str[i++] = m_Source[SourcePointer++];
		}
	str[i]='\0';
	m_Source = str;
	return m_Source;
}


/***************************************************************************/
/*                   查找函数:再指定的字符串中查找给定的字符,            */
/*                  返回字符在字符串中的位置,为0表示没有查找到            */
/***************************************************************************/

int isin(char ch,char str[])  //查找某个符号是否是给定字符串中的一个符号,返回序号,
{
	int index = -1;
	int num = strlen(str);
	for( int j=0; j<num; j++)
	{
		if( ch == str[j] ) 
		{
			index = j; 
			break; 
		}
	}
	return index+1;
}
/***************************************************************************/
/*         求文法的产生式,存入数组中,返回产生式的个数                      */
/***************************************************************************/
int Generator(CString StrSource,char StrGen[10][20])
{
	
	int SourceLen,SourcePointer=0;	
	int i,j;
	int num_Gen=0;
	do 
	{
		SourceLen=strlen(StrSource);
		if(SourcePointer>=SourceLen) break;

		i=StrSource.Find('|', SourcePointer);//查找产生式中的|
		j=StrSource.Find('\r', SourcePointer);//查找产生式中的换行标志
		if(j<0) j = SourceLen;

		if(i>0) {//对有或的情况的处理
			strcpy(StrGen[num_Gen], StrSource.Mid(SourcePointer,i-SourcePointer));//存入或前的产生式
			num_Gen++;//存入或后的产生式
			StrGen[num_Gen][0] = StrSource[SourcePointer];
			StrGen[num_Gen][1] = '-';
			StrGen[num_Gen][2] = '>';
			StrGen[num_Gen][3] = '\0';
			strcat(StrGen[num_Gen], StrSource.Mid(i+1,j-i-1));
			SourcePointer = j+1;
		}
		else
			strcpy(StrGen[num_Gen], StrSource.Mid(SourcePointer,j-SourcePointer));
		SourcePointer = j+2;
		num_Gen++;
	} while (SourcePointer<SourceLen);
	return num_Gen;
}

/***************************************************************************/
/*         对输入文法进行处理,求出文法的终结符和非终结符的个数            */
/***************************************************************************/
void VtVn(char StrGen[10][20], int num_Gen,char VN[], char VT[])
{
	int i,SourceLen,SourcePointer=0;
	char ch;
	int num_VN,num_VT;
	num_VN = strlen(VN);//求非终结符的个数
	num_VT = strlen(VT);//求终结符的个数
	for (i=0;i<10;i++)//初始化VN,VT
	{
		VN[i]='\0';
		VT[i]='\0';
	}
	for( i= 0; i<num_Gen;i++)//对每条产生式处理
	{
		SourceLen = strlen(StrGen[i]);
		SourcePointer =0;
		do{
			ch = StrGen[i][SourcePointer];
			if ( isupper(ch) )
			{
				if ( !isin(ch,VN) )  //对非终结符进行处理,将不同的非终结符存放到VN的数组中
				{
					VN[num_VN] = ch;
					(num_VN)++;
				}
				SourcePointer++;
			}
			else if (ch == '-')  //处理产生式->
			{
				SourcePointer++; 
				if ( (ch = StrGen[i][SourcePointer] ) == '>' ) SourcePointer++;
			}
			else  //对终结符进行处理,将不同的终结符存放到VT的数组中
			{
				if ( !isin(ch,VT)) 
				{
					VT[num_VT] = ch;
					(num_VT)++;
				}
				SourcePointer++; 
			}
		}while (SourcePointer<SourceLen);
	} 
}



/***************************************************************************/
/*                        求FirstVT表格中的内容                              */
/***************************************************************************/
void Creat_FirstVT_Table(char first[][100],char StrGen[][20], int num_Gen, char VN[],char VT[])
{
	int num_VN,num_VT;
	int i,SourcePointer,SourceLen;

	struct stack {  
		char vn;
		char vt;
	}FL_Stack[100];//定义操作使用的堆栈

	int top=0;//栈顶指针	
    int row, col; //firstVT数组中的行列位置
	
	num_VN = strlen(VN);/*求出VN的长度,即非终结符的个数*/
	num_VT = strlen(VT);/*求出VT的长度,即终结符的个数*/

	for( row=0; row<num_VN+1; row++)  //初始化
		for(int col=0; col < num_VT+1; col++)
			first[row][col]=' ';
	for( row=0;row<num_VN+1;row++)  //初始化行头
		first[row+1][0]=VN[row];
	for(col=0;col < num_VT;col++)  //初始化列头
			first[0][col+1]=VT[col];

	for( i= 0; i<num_Gen;i++)  //用产生式对数组进行初始化
	{
		SourcePointer =0;
		SourceLen =strlen(StrGen[i]);//当前产生式
		row = isin(StrGen[i][0],VN); // 第一个非终结符所在数组的行数
		col = isin(StrGen[i][3],VT);// 第一个终结符所在数组的列数
		if ( col > 0 )//P->a...
		{
				FL_Stack[top].vn = StrGen[i][0];//存入堆栈
				FL_Stack[top].vt = StrGen[i][3];
				top++;
				first[row][col] = '1';
				
		}
		else if ( isin(StrGen[i][3],VN) && isin(StrGen[i][4],VT) ) //p->Qa...
		{
			FL_Stack[top].vn = StrGen[i][0];//存入堆栈
			FL_Stack[top].vt = StrGen[i][4];
			top++;
			col = isin(StrGen[i][4],VT);
			first[row][col] = '1';
		}
	}//end of for i

	while(top >0) //从堆栈中弹出修改数组
	{
		top--;

		for( i= 0; i<num_Gen;i++)
			if ( (StrGen[i][3] == FL_Stack[top].vn) && (StrGen[i][0] != FL_Stack[top].vn))//P->Q...
			{

⌨️ 快捷键说明

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