?? c++詞法分析器1dlg.cpp
字號(hào):
// C++詞法分析器1Dlg.cpp : 實(shí)現(xiàn)文件
//
#include "stdafx.h"
#include "C++詞法分析器1.h"
#include "C++詞法分析器1Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于應(yīng)用程序“關(guān)于”菜單項(xiàng)的 CAboutDlg 對(duì)話框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 對(duì)話框數(shù)據(jù)
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 實(shí)現(xiàn)
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CC詞法分析器1Dlg 對(duì)話框
CC詞法分析器1Dlg::CC詞法分析器1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CC詞法分析器1Dlg::IDD, pParent)
, stredit1(_T(""))
, stredit2(_T(""))
, stredit3(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CC詞法分析器1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, listcontrolll);
}
BEGIN_MESSAGE_MAP(CC詞法分析器1Dlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CC詞法分析器1Dlg::OnBnClickedButton1)
ON_BN_CLICKED(IDOK, &CC詞法分析器1Dlg::OnBnClickedOk)
END_MESSAGE_MAP()
// CC詞法分析器1Dlg 消息處理程序
BOOL CC詞法分析器1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 將“關(guān)于...”菜單項(xiàng)添加到系統(tǒng)菜單中。
// IDM_ABOUTBOX 必須在系統(tǒng)命令范圍內(nèi)。
/*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);
}
}*/
listcontrolll.InsertColumn(0,"");
listcontrolll.InsertColumn(1,"");
listcontrolll.SetColumnWidth(0,300);
listcontrolll.SetColumnWidth(1,90);
listcontrolll.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT );
return TRUE;
// 設(shè)置此對(duì)話框的圖標(biāo)。當(dāng)應(yīng)用程序主窗口不是對(duì)話框時(shí),框架將自動(dòng)
// 執(zhí)行此操作
SetIcon(m_hIcon, TRUE); // 設(shè)置大圖標(biāo)
SetIcon(m_hIcon, FALSE); // 設(shè)置小圖標(biāo)
// TODO: 在此添加額外的初始化代碼
return TRUE; // 除非將焦點(diǎn)設(shè)置到控件,否則返回 TRUE
}
void CC詞法分析器1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向?qū)υ捒蛱砑幼钚』粹o,則需要下面的代碼
// 來繪制該圖標(biāo)。對(duì)于使用文檔/視圖模型的 MFC 應(yīng)用程序,
// 這將由框架自動(dòng)完成。
void CC詞法分析器1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于繪制的設(shè)備上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使圖標(biāo)在工作矩形中居中
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;
// 繪制圖標(biāo)
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//當(dāng)用戶拖動(dòng)最小化窗口時(shí)系統(tǒng)調(diào)用此函數(shù)取得光標(biāo)顯示。
//
HCURSOR CC詞法分析器1Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CC詞法分析器1Dlg::OnBnClickedButton1()
{
listcontrolll.DeleteAllItems();
CString aa=_T("All Files(*.*)|*.*|cpp(*.cpp)|*.cpp|h(*.h)|*.h|txt(*.txt)|*.txt||");
CFileDialog dialog(true,NULL,NULL,OFN_OVERWRITEPROMPT,aa,this);
if(dialog.DoModal()==IDOK)
{
stredit1=dialog.GetPathName(); //
CC詞法分析器1Dlg::deal();
}// TODO: 在此添加控件通知處理程序代碼
}
void CC詞法分析器1Dlg::deal(void)
{
char last=' ';
char save[500];
int len=sizeof(save);
int readed=0;
int ptr=0;
int statu=0;
CString buffer;
CFile fp1;
if(!fp1.Open(stredit1,CFile::modeRead))
{
MessageBox("Can not open");
return;
}
readed=fp1.Read(save,len);
while(readed>0)
{
while(ptr<readed)
{
char current=save[ptr];
switch(statu)
{
case 0:
if((current>='a'&¤t<='z')||(current>='A'&¤t<='Z')||current=='_')
{
last=current;
buffer.AppendChar(current);
statu=1;
}
else if(current>='0'&¤t<='9')
{
last=current;
buffer.AppendChar(current);
statu=2;
}
else if(current=='\''||current=='\"')
{
last=current;
buffer.AppendChar(current);
statu=3;
}
else if((current>='!'&¤t<='/')||(current>=':'&¤t<='@')
||(current>='['&¤t<='^')||(current>='{'&¤t<='~'))
{
last=current;
buffer.AppendChar(current);
statu=4;
}
break;
case 1:
if((current>='a'&¤t<='z')||(current>='A'&¤t<='Z')
||current=='_'||(current>='0'&¤t<='9'))
{
last=current;
buffer.AppendChar(current);
}
else
{
if(isreserved(buffer))
show(buffer,5);
else show(buffer,1);
buffer="";
ptr--;
statu=0;
}
break;
case 2:
if(current>='0'&¤t<='9')
{
last=current;
buffer.AppendChar(current);
}
else
{
show(buffer,2);
buffer="";
ptr--;
statu=0;
}
break;
case 3:
if((current!='\''&¤t!='\"')||(last=='\\'))
{
if(current=='\''||current=='\"')
{
ptr--;
statu=0;
}
else
{
last=current;
buffer.AppendChar(current);
}
}
else
{
buffer.AppendChar(current);
show(buffer,3);
buffer="";
statu=0;
}
break;
case 4:
if((current>='!'&¤t<='/')||(current>=':'&¤t<='@')
||(current>='['&¤t<='^')||(current>='{'&¤t<='~'))
{
buffer.AppendChar(current);
if(last=='/'&¤t=='*')
{
buffer.Delete(buffer.GetLength()-2,2);
if(buffer!="")
show(buffer,4);
buffer="/*";
statu=6;
}
else if(last=='/'&¤t=='/')
{
buffer.Delete(buffer.GetLength()-2,2);
if(buffer!="")
show(buffer,4);
buffer="//";
statu=7;
}
else if(current=='\''||current=='\"')
{
ptr--;
buffer.Delete(buffer.GetLength()-1,1);
show(buffer,4);
buffer="";
statu=0;
}
last=current;
}
else
{
show(buffer,4);
buffer="";
ptr--;
statu=0;
}
break;
case 6:
if(!(last=='*'&¤t=='/'))
{
last=current;
buffer.AppendChar(current);
}
else
{
buffer.AppendChar(current);
show(buffer,6);
buffer="";
statu=0;
}
break;
case 7:
if(current!='\n')
{
last=current;
buffer.AppendChar(current);
}
else
{
show(buffer,7);
buffer="";
statu=0;
}
break;
default:
MessageBox("error statu");
}
ptr++;
}
ptr=0;
readed=fp1.Read(save,len);
}
fp1.Close();
file.Close();
}
void CC詞法分析器1Dlg::show(CString b, int st)
{
CString add;
switch(st)
{
case 1:
add="標(biāo)識(shí)符";
break;
case 2:
add="數(shù)字";
break;
case 3:
add="字符串";
break;
case 4:
add="特殊字符";
break;
case 5:
add="保留字";
break;
case 6:
add="注釋";
break;
case 7:
add="注釋";
break;
default:
MessageBox("error statu2");
}
int count=listcontrolll.GetItemCount();
if(st!=4)
{
listcontrolll.InsertItem(count,b);
listcontrolll.SetItemText(count,1,add);
}
else
{
int len=b.GetLength();
for(int i=0;i<len;i++)
{
char *u=b.GetBuffer();
CString k;
k.AppendChar(u[i]);
listcontrolll.InsertItem(count,k);
listcontrolll.SetItemText(count,1,add);
count++;
}
}
}
bool CC詞法分析器1Dlg::isreserved(CString buf)
{
CString reserved[37] = {
"array", "case", "const","do", "else"," for", "goto","if","goto",
"while","public","int","double","long","class","char","float","short","void",
"default","true","false","break","sizeof","return","protected","private",
"enum","bool","virtual","extern","switch","new","include","ifdefine","endif","define"};
for(int i=0;i<37;i++)
{
if(buf==reserved[i])
return true;
}
return false;
}
void CC詞法分析器1Dlg::OnBnClickedOk()
{
/*CString cc=_T("All Files(*.*)|*.*|cpp(*.cpp)|*.cpp|h(*.h)|*.h|txt(*.txt)|*.txt||");
CFileDialog dialog(true,NULL,NULL,OFN_OVERWRITEPROMPT,stredit2,this);
if(dialog.DoModal()==IDOK)
{
stredit2=dialog.GetPathName();
::CreateDirectoryA(stredit2,NULL);
}*/
CString bb=_T("All Files(*.*)|*.*|cpp(*.cpp)|*.cpp|h(*.h)|*.h|txt(*.txt)|*.txt||");
CFileDialog dialog1(true,NULL,NULL,OFN_OVERWRITEPROMPT,bb,this);
if(dialog1.DoModal()==IDOK)
{
stredit3=dialog1.GetFileName();
stredit2=dialog1.GetPathName();
//file.Open(stredit2,CFile::modeCreate|CFile::modeReadWrite);
CC詞法分析器1Dlg::conserve();
}
// TODO: 在此添加控件通知處理程序代碼
//OnOK();
}
void CC詞法分析器1Dlg::conserve(void)
{
CString b;
CString add;
CFile fp2;
if(!fp2.Open(stredit3,CFile::modeCreate|CFile::modeReadWrite))
{
MessageBox("Can not open");
return;
}
int i;
for(i=0;i<listcontrolll.GetItemCount();i++)
{
CString aa=listcontrolll.GetItemText(i,0);
CString bb=listcontrolll.GetItemText(i,1);
aa.Append("-----------------");
fp2.Write(aa.GetBuffer(),aa.GetLength());
fp2.Write(bb.GetBuffer(),bb.GetLength());
fp2.Write("\r\n",2);
}
fp2.Close();
file.Close();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -