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

📄 fenxidlg.cpp

📁 本程序实现词法分析能够把源代码中的语法分离出单词
💻 CPP
字号:
// fenxiDlg.cpp : implementation file
//

#include "stdafx.h"
#include "fenxi.h"
#include "fenxiDlg.h"
#include "afxtempl.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()

/////////////////////////////////////////////////////////////////////////////
// CFenxiDlg dialog

CFenxiDlg::CFenxiDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFenxiDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFenxiDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFenxiDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFenxiDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFenxiDlg, CDialog)
	//{{AFX_MSG_MAP(CFenxiDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFenxiDlg message handlers

BOOL CFenxiDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFenxiDlg::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 CFenxiDlg::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();
	}
}
CList   <CString,CString&>   mykey; 
CList   <CString,CString&>   jiangefu;
CList   <CString,CString&>   ysf;
// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFenxiDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//---------全局
int curszpos=1; 
int curlinelen=0;     
int curlinepos=1;
//CString mykey[10];
CString fengefu[10];
CString myoperator[10];
CString strtoken;
//---------
void readkey()
{
	CString strkey="";
	CStdioFile mf;
	mf.Open("key.txt",CFile::modeRead);
	if(mf==NULL) 
	{
		AfxMessageBox("open error!");
		return;
	}
	while(mf.ReadString(strkey))
	{
		mykey.AddTail(strkey);
	}
}
void readjiangefu()
{
	CString strjgf="";
	CStdioFile mf;
	mf.Open("jgf.txt",CFile::modeRead);
	if(mf==NULL) 
	{
		AfxMessageBox("open error!");
		return;
	}
	if(mf==NULL) 
	{
		AfxMessageBox("open error!");
		return;
	}
	while(mf.ReadString(strjgf))
	{
		jiangefu.AddTail(strjgf);
	}	
}
void readoperator()
{
	CString strysf="";
	CStdioFile mf;
	mf.Open("ysf.txt",CFile::modeRead);
	if(mf==NULL) 
	{
		AfxMessageBox("open error!");
		return;
	}
	if(mf==NULL) 
	{
		AfxMessageBox("open error!");
		return;
	}
	while(mf.ReadString(strysf))
	{
		ysf.AddTail(strysf);
	}	
}
void init()
{
	readkey();
	readjiangefu();
	readoperator();

}

bool isletter(char ch)
{
	if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
		return true;
	else return false;
}

bool isnum(char ch)
{
	if(ch>='0'&&ch<='9')
		return true;
	else return false;
}
bool iskey(CString strtarget)
{
	if(mykey.Find(strtarget)) return true;
	else return false;
}
bool isoperator(CString strtarget)
{
	if(ysf.Find(strtarget)) return true;
	else return false;
}
bool isfengefu(CString strtarget)
{
	if(jiangefu.Find(strtarget)) return true;
	else return false;
	/*for(int i=0;i<6;i++)
		if(strtarget==fengefu[i]) return true;
	return false;*/
}
int identify(CString target)
{
	CString temp_str="";
	char temp_ch;
	int temp_len=strlen(target);
	int temp_pos=1;
	if(isnum(target[0]))      //数字
	{
		for(int i=1;i<=temp_len;i++)
		{
			temp_ch=target[i-1];
			if(isletter(temp_ch)) return -1;  // 非法
			temp_str+=temp_ch;
		}
		return 3;
	}
	else if(isletter(target[0]))     //首为字母
	{
		if(iskey(target)) return 1;
		for(int i=1;i<=temp_len;i++)
		{
			temp_ch=target[i-1];
			temp_str+=temp_ch;
		}
		return 2;
	}
	else if(isoperator(target)) return 4;
	else
	{
		int tempp=curszpos;         //后退
		tempp--;
		if(isoperator(target[0]))
		{
			curszpos=tempp;
			strtoken=target[0];
			return 4;
		}
		if(isfengefu(strtoken))	return 5;
		else
		{
			tempp=curszpos;          //后退
			tempp--;
			//strtoken=target[0];
			if(isfengefu(target[0]))
			{
				curszpos=tempp;
				strtoken=target[0];
				return 5;
			}
		}
	}
	return -1;
}
void getstr(CString mystr,int &pos,int strlen)
{
	strtoken="";
	char temp;
	if(pos>strlen) return;
	while(mystr[pos-1]==' ')
	{
		pos++;
		if(pos==strlen) return;
	}

	while((temp=mystr[pos-1])!=' ')
	{
		if(isletter(mystr[pos-1])||isnum(mystr[pos-1]))    //非运算符非分隔符
		{
			if(isletter(temp)||isnum(temp))
			{
				strtoken+=temp;
				pos++;
				if(pos>strlen) return;
				temp=mystr[pos-1];  //超前搜索下一个字符
				if(isletter(temp)==false&&isnum(temp)==false) return;  
			}
			else return;
		}
		else
		{
			if(isletter(temp)||isnum(temp)) return;
			else
			{
				strtoken+=temp;
				pos++;
				if(pos>strlen) return;
				if(isletter(mystr[pos-1])||isnum(mystr[pos-1])) return;
			}
		}		
	}
}
void CFenxiDlg::OnOK() 
{
	//HWND hParent=::GetParent(hwndChild);
	CWnd* wParent=CWnd::FromHandle(GetSafeHwnd()); 
	CListBox* showlist=(CListBox*)wParent->GetDlgItem(ShowList);
	showlist->ResetContent();
	CString myresult="",tem="",curlinestr="";
	char szchange[10];
	init();
	//------
	CString filename;
    CFileDialog opendlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,".(*.txt)|*.txt|All Files(*.*)|*.*||",NULL);
	if(opendlg.DoModal()==IDOK)
        filename=opendlg.GetPathName();
	else return;
	CStdioFile mf;
	mf.Open(filename,CFile::modeRead);
	CStdioFile rs;
	rs.Open("save.txt",CFile::modeCreate);
	rs.Close();
	rs.Open("save.txt",CFile::modeWrite);
	if(mf==NULL) 
	{
		MessageBox("open error!");
		return;
	}
	while(mf.ReadString(curlinestr))
	{
		curszpos=1; 
		curlinelen=0;
		curlinepos=1;
		myresult="";
		//-------处理得到的一行字符串
		curlinestr.TrimLeft(" ");
		curlinestr.TrimRight(" ");
		curlinelen=strlen(curlinestr);
		rs.WriteString(curlinestr+"\n");
		//----
		while(curszpos<=curlinelen)
		{
			tem="";
			getstr(curlinestr,curszpos,curlinelen);
			itoa(identify(strtoken),szchange,10);
			tem+='(';
			tem+=szchange;
			tem+=',';
			tem+='\"';
			tem+=strtoken;
			tem+='\"';
			tem+=')';
			tem+='\n';
			showlist->AddString(tem);
			rs.WriteString("\t\t│"+tem);
			//Sleep(100);
		}
		rs.WriteString("\n\n");
		//myresult+=tem;
		//MessageBox(myresult,NULL,MB_OK);
	}
	mf.Close();
	rs.Close();
}

⌨️ 快捷键说明

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