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

📄 ncffxview.cpp

📁 本人用VC自己做的词法分析器,
💻 CPP
字号:
// NCFFXView.cpp : implementation of the CNCFFXView class
//

#include "stdafx.h"
#include "NCFFX.h"

#include "NCFFXDoc.h"
#include "NCFFXView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CNCFFXView

IMPLEMENT_DYNCREATE(CNCFFXView, CView)

BEGIN_MESSAGE_MAP(CNCFFXView, CView)
	//{{AFX_MSG_MAP(CNCFFXView)
	ON_COMMAND(ID_MENUITEM32771, OnMenuitem32771)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNCFFXView construction/destruction

CNCFFXView::CNCFFXView()
{
	// TODO: add construction code here

}

CNCFFXView::~CNCFFXView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CNCFFXView drawing

void CNCFFXView::OnDraw(CDC* pDC)
{
	CNCFFXDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CNCFFXView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CNCFFXView diagnostics

#ifdef _DEBUG
void CNCFFXView::AssertValid() const
{
	CView::AssertValid();
}

void CNCFFXView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CNCFFXView message handlers

void CNCFFXView::OnMenuitem32771() 
{
	// TODO: Add your command handler code here
	CString FilePathName;
	CString   bufFile=""; 
	CFileDialog dlg(TRUE);
	CString szchd="";
	CString m_lett="";
	CString le;
	if(dlg.DoModal()==IDOK) FilePathName=dlg.GetFileName();
	CStdioFile   file(FilePathName, CFile::modeRead  |  CFile::typeText); //打开文件  
    int x=0;
	
	CString YSF="";
	CString Rig="\")\r";
	CString Out="";
	CString KEY[8]={{"if"},{"int"},{"for"},{"while"},{"do"},{"return"},{"break"},{"continue"}};
	CString Limit[6]={{","},{";"},{"{"},{"}"},{"("},{")"}};
	CString Sign[10]={{"+"},{"-"},{"*"},{"/"},{"="},{"<"},{">"},{"="},{"!"},{">"}};
	CClientDC dc(this);
  while(file.ReadString(bufFile)) //从文件读单词
  {
	int p=0;
	bufFile.TrimLeft();//去左空格
	bufFile.TrimRight();//去右空格
	x=bufFile.GetLength();
	int x4=bufFile.Find('/');
	
	if(bufFile.Mid(x4+1,1)=='/' || bufFile.Mid(x4+1,1)=='*') 
	{
		bufFile=bufFile.Left(x4-1);
	bufFile.TrimRight();
	x=bufFile.GetLength();
	}
//MessageBox(bufFile.Mid(xxx,1));
	  //MessageBox(bufFile);
	//x=bufFile.GetLength();
	for(int i=0;i<x;i++)
	{
		m_lett=bufFile.Mid(i,1);
	
		p=0;
		if(m_lett>='a' && m_lett<='z' || m_lett>='A' && m_lett<='Z' ) //判断是否是字母
		{
			szchd+=m_lett;
		
		
		}
		else
		{
			
			if(m_lett !=" ") le+=m_lett;
			if(le>='0' && le<='9') //判断是否是数字
			{
				int sz=i;
				CString Lef="(3,\"";
				while(1)
				{
					if(bufFile.Mid(sz+1,1)>='0' && bufFile.Mid(sz+1,1)<='9') 
					{
						le+=bufFile.Mid(sz+1,1);
						sz+=1;
					}
					else
						break;
				}
				i=sz;
				le=Lef+le+Rig;
				Out=Out+le;
			
				le="";
			}
			if(szchd !="")
			{
				CString Lef="(1,\"";
				for(int j=0;j<=7;j++)//进行一一比较
				{
					
					if(szchd==KEY[j]) 
						{
						szchd=Lef+szchd+Rig;
						Out=Out+szchd;
						szchd="";
						p=1;	
						}
					
				}
				if(p==0)
					{
						CString Lef="(2,\"";
						szchd=Lef+szchd+Rig;
						Out=Out+szchd;
						szchd="";
					}
			}
			for(int k=0;k<=9;k++)//判断是否是运算符
			{
			//	if(le=="") break;
				int zs=i;
				if(le==Sign[k])
				{	
					YSF=YSF+le;
				
					le="";
				}
				if(YSF!="")
				{
				
				for(int k1=0;k1<=9;k1++)
				{
				if(bufFile.Mid(zs+1,1)==Sign[k1])
				{
				
					
					YSF=YSF+bufFile.Mid(zs+1,1);
					zs+=1;
					k=9;
				}
				}
				}
				if(YSF.GetLength()>0 && k==9)
				{
				CString Lef="(4,\"";
				YSF=Lef+YSF+Rig;
				Out=Out+YSF;
				YSF="";
				i=zs;
				}
				
			}
			for(int K=0;K<=5;K++)//分隔符
				{
				if(le=="") break;
				if(le==Limit[K])
				{
					CString Lef="(5,\"";
					le=Lef+le+Rig;
					Out=Out+le;

					le="";
				
				}
			

				}
		}
		
	}
	
  }
  MessageBox(Out);
  
  CFile file2("d:\Save.txt", CFile::modeCreate|CFile::modeReadWrite|CFile::shareExclusive);
  
  file2.Write(Out,Out.GetLength());
  MessageBox("文件已经保存到D盘");
  file2.Close();
  
	file.Close();
}

⌨️ 快捷键说明

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