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

📄 codescandlg.cpp

📁 本软件是实现一个词法分析器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// CodeScanDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CodeScan.h"
#include "CodeScanDlg.h"
#include<fstream.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char *key[]={"asm","auto","bool","break","case","catch","char","const","close","continue",
             "default","do","double","else","enum","extern","false","float","for","friend",
			 "goto","if","int","long","register","return","short","signed","sizeof","static",
			 "struct","switch","typedef","this","main","throw","typedef","virtual","cin","cout",
              "union","unsigned","void","volatile","while","new","delete","endl","open",
			"class","define","ifdef","ifndef","endif","error","include"};   

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

/////////////////////////////////////////////////////////////////////////////
// CCodeScanDlg dialog

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

void CCodeScanDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCodeScanDlg)
	DDX_Control(pDX, IDC_LIST2, m_ListBox);
	DDX_Control(pDX, IDC_EDIT1, m_edit);
	DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCodeScanDlg, CDialog)
	//{{AFX_MSG_MAP(CCodeScanDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnOpenFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCodeScanDlg message handlers

BOOL CCodeScanDlg::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
	Init();
    OnInitailListCtrl();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CCodeScanDlg::OnOpenFile() 
{
	// TODO: Add your control notification handler code here
	int count=0;
	try 
	{ 
//	    CString str("",10000);//这是做为存放打开文件名和路径的字符串空间10000这个值的设置就觉得有些不妥当 
	    CFileDialog openfile(true,NULL,NULL, 
	    OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,"All Files(*.*)|*.*|",AfxGetMainWnd()); 
//	    openfile.m_ofn.lpstrFile=str.GetBuffer(10000); 
//		openfile.m_ofn.nMaxFile=10000; 
//		openfile.m_ofn.lStructSize=sizeof(openfile.m_ofn); 
//		CString pathname("",MAX_PATH); 
		CFileStatus status; 
	    openfile.DoModal(); 
		POSITION mPos=openfile.GetStartPosition(); 
	    while(mPos!=NULL){ 
		pathname=openfile.GetNextPathName(mPos); 
	//	CFile::GetStatus(pathname,status); 
  //  	this->m_showpath.InsertString(count++,pathname); //把包括路径在内的文件名写到一个list控件里
	   } 
	} 
	catch (CException* e) 
	{ 
		AfxMessageBox(_T("打开文件出错了!")); 
		e->Delete(); 
	} 
	OnScan();

}

void CCodeScanDlg::Init()
{ 
    pathname="";
	m_ListBox.InsertString(0,"    ");
    m_ListBox.InsertString(1,"    ");
	m_ListBox.InsertString(2,"    压缩文件被存   ");
	m_ListBox.InsertString(3,"    在当前文件夹   ");
	m_ListBox.InsertString(4,"    DebugTar.cpp   ");
	m_ListBox.InsertString(5,"       文件里      ");
    m_ListBox.InsertString(6,"     ");
    m_ListBox.InsertString(7,"     ");
	m_ListBox.InsertString(8,"    班级:05本4   ");
	m_ListBox.InsertString(9,"    姓名:邱名州  ");
	m_ListBox.InsertString(10,"  学号:20052102424   ");
}

void CCodeScanDlg::OnScan() 
{
	// TODO: Add your control notification handler code here
	m_ListCtrl.DeleteAllItems();
	char ch;//暂时保留单字符
	char* word=new char[10000];//暂时保留字
	char space[2]=" ";
	int i=0;
	bool flag=0;//用于标识是否为指针
	ifstream fs(pathname,ios::nocreate|ios::in);
    GetDlgItem(IDC_EDIT1)->SetWindowText(pathname);//打印路径名
	//在当前文件建立一个“debugtar.cpp”
	char szFileNames[100];
	CString strName;
	//得到当前路径
	DWORD dwLen = GetModuleFileName(NULL, szFileNames, sizeof(szFileNames));
	for(DWORD offset=dwLen; offset>=0; offset--)
	{
	   if(szFileNames[offset] == '\\')
	   {
		szFileNames[offset] = 0;
		break;
	   }
	}
	strName = szFileNames;
	//建立压缩文件名为Debugtar.cpp
	strName=strName+"tar.cpp";

    ofstream of(strName);
    if(!fs)
	{
		delete []word;
		AfxMessageBox("打不开该文件");
        return ;
	}
	fs.get(ch);
	while(ch!=EOF)
	{
		i=0;
		//删除空格、换行、回车
		if((ch=='\t')||(ch=='\n')||(ch==' '))
		{
			fs.get(ch);
		}
		//以字符为开头字符串
		else if((ch>='a')&&(ch<='z')||(ch>='A')&&(ch<='Z')||ch=='_')
		{
			word[i++]=ch;
			fs.get(ch);
			while((ch>='a')&&(ch<='z')||(ch>='A')&&(ch<='Z')||(ch>='0')&&(ch<='9')||(ch=='.'))
			{
				word[i++]=ch;
				fs.get(ch);
			}
			//头文件情况
			if(word[i-1]=='h'&&word[i-2]=='.')
			{
				word[i]='\0';
				of.write(word,strlen(word));
				n=m_ListCtrl.GetItemCount();
				m_ListCtrl.InsertItem(n,word);
		    	m_ListCtrl.SetItemText(n,1,"关键字");
			//	fs.get(ch);
			}
			//关键字或表示符情况
			else
			{ 
				word[i]='\0';
				of.write(word,strlen(word));
			    flag=KeyWord(word);	
				if(flag==1)
				of.write(space,1);
			}
		}
		//以数字为开头字符串
		else if((ch>='0')&&(ch<='9'))
		{
			while((ch=='.')||(ch>='0')&&(ch<='9'))
			{
				word[i++]=ch;
				fs.get(ch);
			}
			word[i]='\0';
			of.write(word,strlen(word));
			//AfxMessageBox(word);
            //of.write(space,1);
			Num(word);
		}
		else 
		{
			n=m_ListCtrl.GetItemCount();
			switch(ch)
			{
			//判断是除号还是解释符 
			case '/':
			   word[i++]=ch;
               fs.get(ch);
               if((ch=='/')||(ch=='*'))//如果是解释
               {
				   word[i++]=ch;
				   // cout<<ch;     			 

⌨️ 快捷键说明

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