📄 scannerdlg.cpp
字号:
// ScannerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Scanner.h"
#include "ScannerDlg.h"
#include "Vocabulary.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()
/////////////////////////////////////////////////////////////////////////////
// CScannerDlg dialog
CScannerDlg::CScannerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CScannerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CScannerDlg)
m_result = _T("");
m_source = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CScannerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CScannerDlg)
DDX_Control(pDX, IDC_EDIT_SOURCE, m_edit_source);
DDX_Control(pDX, IDC_EDIT_RESULT, m_edit_result);
DDX_Text(pDX, IDC_EDIT_RESULT, m_result);
DDX_Text(pDX, IDC_EDIT_SOURCE, m_source);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CScannerDlg, CDialog)
//{{AFX_MSG_MAP(CScannerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SCAN, OnScan)
ON_BN_CLICKED(IDC_BUTTON1, Onclear)
ON_BN_CLICKED(IDC_BUTTON2, OnCancel)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CScannerDlg message handlers
BOOL CScannerDlg::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 CScannerDlg::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 CScannerDlg::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 CScannerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CScannerDlg::OnScan()
{
// TODO: Add your control notification handler code here
if(ends=true)
{
ch=' ';
code=0;
ends=false;
for(int i=0;i<50;i++)
{
Identifier[i]="";
}
for(i=0;i<50;i++)
{
Const[i]="";
}
for(i=0;i<50;i++)
{
PrimaryKey[i]=0;
}
strToken="";
point=0;
m_result="";
m_source="";
strToken="";
for(i=0;i<50;i++)
{
Symbol[i]="";
}
}
CString cc;
UpdateData(true);
int length=m_source.GetLength();
if(m_source=="")
{
return;
}
while(point<length)
{
strToken="";
str=false;
ch=GetChar();
GetBC();
if(IsLetter())
{
while( IsLetter() || IsDigit() )
{
Concat();
ch=GetChar();
}
Retract();
code=Reserve();
if(strToken=="true"||strToken=="false")
value=InsertConst();
else if(code==0&&strToken!="true"&&strToken!="false")
{
value=InsertId();
strToken="";
//return ($ID ,value);
}
else
{
PrimaryKey[code]=1;
strToken="";
//return (code,-);
}
}
else if(IsDigit())
{
int x=0;
while(IsDigit())
{
Concat();
ch=GetChar();
if(ch=='.')
{
x++;
strToken+='.';
ch=GetChar();
}
}
if(x<2)
{
Retract();
value=InsertConst();
}
else
{
Retract();
value=InsertId();
}
//return ( $ int ,value);
}
else
{
Concat();
//else
//{
if(strToken=="+")
{
ch=GetChar();
if(ch=='+'||ch=='=')
Concat();
else
Retract();
}
if(strToken=="-")
{
ch=GetChar();
if(ch=='-'||ch=='=')
Concat();
else
Retract();
}
if(strToken=="*")
{
ch=GetChar();
if(ch=='=')
Concat();
else
Retract();
}
if(strToken=="/")
{
ch=GetChar();
if(ch=='=')
Concat();
else
Retract();
}
if(strToken=="=")
{
ch=GetChar();
if(ch=='=')
Concat();
else
Retract();
}
value=InsertSymbol();
//}
}
}
m_result="";
ShowOut();
ends=true;
}
char CScannerDlg::GetChar()
{
char ch=m_source.GetAt(point);
point++;
return ch;
}
void CScannerDlg::GetBC()
{
while(ch==' ')
{
ch=GetChar();
}
}
BOOL CScannerDlg::IsLetter()
{
if((ch>='A'&& ch<='Z') || (ch>='a' && ch<='z'))
{
return TRUE;
}
return FALSE;
}
BOOL CScannerDlg::IsDigit()
{
if(ch>='0' && ch<='9')
{
return TRUE;
}
return FALSE;
}
void CScannerDlg::Concat()
{
strToken+=ch;
}
void CScannerDlg::Retract()
{
point--;
ch=' ';
}
int CScannerDlg::Reserve() /// 是否是关键字
{
CVocabulary bu;
int p=bu.GetCode(strToken); ///// 是否是关键字
return p;
}
int CScannerDlg::InsertId() //插入标志符
{
for(int i=0;i<50;i++)
{
if(strToken==Identifier[i])
{
strToken="";
break ;
}
if(Identifier[i]=="")
{
Identifier[i]=strToken;
strToken="";
break ;
}
}
return i;
}
int CScannerDlg::InsertConst() //插入常数
{
for(int i=0;i<50;i++)
{
if(Const[i]==strToken)
{
strToken="";
return i;
}
if(Const[i]=="")
{
Const[i]=strToken;
strToken="";
return i;
}
}
return i;
}
int CScannerDlg::InsertSymbol()//插入符号
{
CVocabulary bu;
for(int i=51;i<94;i++)
{
if(strToken==bu.Vocabulary[i])
{
Symbol[i-50]=1;
break;
}
}
return i;
}
void CScannerDlg::ShowOut()
{
m_result+="-------关键字-------";
m_result+="\r\n";
CVocabulary bu;
for(int i=1;i<39;i++)
{
if(PrimaryKey[i]==1)
{
m_result+=bu.Vocabulary[i];
m_result+="\r\n";
}
}
m_result+="-------符号--------";
m_result+="\r\n";
for(i=51;i<94;i++)
{
if(Symbol[i-50]==1)
{
m_result+=bu.Vocabulary[i];
m_result+="\r\n";
}
}
m_result+="-------标识符--------";
m_result+="\r\n";
for(i=0;i<50;i++)
{
if(Identifier[i]!="")
{
m_result+=Identifier[i];
m_result+="\r\n";
}
}
m_result+="--------常数-------";
m_result+="\r\n";
for(i=0;i<50;i++)
{
if(Const[i]!="")
{
m_result+=Const[i];
m_result+="\r\n";
}
}
UpdateData(false);
}
void CScannerDlg::Onclear()
{
// TODO: Add your control notification handler code here
char strfile[]="文本文件 (*.txt)|*.txt|C++源文件 (*.cpp)|*.cpp|C++头文件 (*.h)|*.h|所有文件 (*.*)|*.*|";
CString cs;
char buf[10000];
int nByteRead;
int len;
CFileDialog *dlg=new CFileDialog(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,strfile);
if(dlg->DoModal()==IDOK)
{
CFile file;
if(!file.Open(dlg->GetFileName(),CFile::modeRead))
{
MessageBox("指定文件不存在","提示",MB_ICONEXCLAMATION);
return;
}
len=file.GetLength();
if(len>9999)
{
MessageBox("文件过长","提示",MB_ICONINFORMATION);
return;
}
nByteRead=file.Read(buf,len);
for(int i=0;i<nByteRead;i++)
{
cs+=buf[i];
}
m_source="";
m_source=cs;
UpdateData(false);
}
delete dlg;
}
void CScannerDlg::OnCancel()
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();
}
void CScannerDlg::OnButton3()
{
// TODO: Add your control notification handler code here
m_source.Empty();
m_result.Empty();
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -