📄 accidenceanalyzerdlg.cpp
字号:
// AccidenceAnalyzerDlg.cpp : implementation file
//
#include "stdafx.h"
#include <afxdlgs.h>
#include "AccidenceAnalyzer.h"
#include "AccidenceAnalyzerDlg.h"
/////////////////////////////////////
#include<stdio.h>
#define BUFFERNUM 300
#define WORDLEN 20
#define WORDLISTNUM 1000
#define KEYNUM 39
#define FALSE 0
#define TRUE 1
int space=FALSE;
int note=FALSE;
int fullnote1=FALSE;
int fullnote2=FALSE;
int endoffile=FALSE;
int wordcount=1;
char *key[KEYNUM];
////////////////////////////////////////////////////////////////////////////
#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()
/////////////////////////////////////////////////////////////////////////////
// CAccidenceAnalyzerDlg dialog
CAccidenceAnalyzerDlg::CAccidenceAnalyzerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAccidenceAnalyzerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAccidenceAnalyzerDlg)
m_list = _T("");
m_result = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAccidenceAnalyzerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAccidenceAnalyzerDlg)
DDX_Control(pDX, IDC_EDIT2, m_prework);
DDX_Control(pDX, IDC_EDIT1, m_show);
DDX_Text(pDX, IDC_EDIT3, m_list);
DDX_Text(pDX, IDC_EDIT4, m_result);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAccidenceAnalyzerDlg, CDialog)
//{{AFX_MSG_MAP(CAccidenceAnalyzerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAccidenceAnalyzerDlg message handlers
BOOL CAccidenceAnalyzerDlg::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 CAccidenceAnalyzerDlg::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 CAccidenceAnalyzerDlg::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 CAccidenceAnalyzerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
////////////////////////////////////////////////////////////////////////////
void prework(int *x,char *p,char buffer[]) //调用此函数时取一行代码送入缓冲区,并作预处理
{
int i=0;char ch;
while((ch=p[(*x)++])!=0)
{
if(fullnote2==TRUE)
{
if(ch!='/') {fullnote2=FALSE;goto NEXT;}
else {fullnote1=FALSE;fullnote2=FALSE;note=FALSE;goto NEXT;}
}
if(fullnote1==TRUE)
{
if(ch!='*') goto NEXT;
else {fullnote2=TRUE;goto NEXT;}
}
switch(ch)
{
case 32://空格
{
if(space==FALSE) {buffer[i]=ch;space=TRUE;i++;goto NEXT;}
else goto NEXT;
}
case 9://TAB
{
if(space==FALSE) {buffer[i]=32;space=TRUE;i++;goto NEXT;}
else goto NEXT;
}
case 47://'/'
{
if(note==FALSE) {note=TRUE;goto NEXT;}
else
{
buffer[i]='\0';
do{ch=p[(*x)++];} while(ch!=EOF&&ch!=13&&ch!=10) ;
note=FALSE;
if(ch==EOF) {endoffile=TRUE;return;}
if(ch==13||ch==10) return;
}
}
case 42://'*'
{
if(note==FALSE) {buffer[i]=ch;i++;goto NEXT;}
else {fullnote1=TRUE;goto NEXT;}
}
case 13://回车
{
buffer[i]='\0';
return;
}
/* case 10://换行
{
buffer[i]='\0';
return;
}*/
default://其他
{
if(note==TRUE) {note=FALSE;buffer[i]='/';i++;}
buffer[i]=ch;i++;
space=FALSE;goto NEXT;
}
}
NEXT:;
}
if(ch==0) {endoffile=TRUE;buffer[i]='\0';}
return;
}
///////////////////////////////////////////////////////////
struct wordstruct
{
char worddetail[WORDLEN];
int wordtype; //1表示独立单词,亦及一词一组的单词,包括关键字、算符、界符
//2表示用户自定的标示符 3表示常数(整型、实型、字符型,暂不包括布尔型)
}wordstructlist[WORDLISTNUM];
//////////////////////////////////////////////////////////////
void initialkey()
{
key[1]="void";
key[2]="short";
key[3]="int";
key[4]="float";
key[5]="double";
key[6]="long";
key[7]="unsigned";
key[8]="signed";
key[9]="char";
key[10]="scanf";
key[11]="if";
key[12]="else";
key[13]="printf";
key[14]="TRUE";
key[15]="FALSE";
key[16]="while";
key[17]="do";
key[18]="for";
key[19]="break";
key[20]="continue";
key[21]="case";
key[22]="switch";
key[23]="const";
key[24]="default";
key[25]="enum";
key[26]="extern";
key[27]="goto";
key[28]="register";
key[29]="return";
key[30]="define";
key[31]="auto";
key[32]="sizeof";
key[33]="static";
key[34]="struct";
key[35]="typedef";
key[36]="include";
key[37]="union";
key[38]="volatile";
}
////////////////////////////////////////////////////////////////////////
BOOL getword(char *input,int *p,char word[])
{
char ch;
char chtemp;
int i=0;
for(int temp=0;temp<WORDLEN;temp++) word[i]='\0';
again:
if(input[*p]==0) return FALSE;
ch=input[(*p)++];
if(ch>=48&&ch<=57) //第一个字符是数字
{
word[i++]=ch;
ch=input[(*p)++];
while((ch>=48&&ch<=57)||ch==46)
{
word[i++]=ch;
ch=input[(*p)++];
}
word[i]='\0';
(*p)--;
return TRUE;
}
else if((ch>=65&&ch<=90)||(ch>=97&&ch<=122)||ch==95)//第一个字符是下划线或字母
{
word[i++]=ch;
ch=input[(*p)++];
while((ch>=65&&ch<=90)||(ch>=97&&ch<=122)||ch==95||(ch>=48&&ch<=57))
{
word[i++]=ch;
ch=input[(*p)++];
}
word[i]='\0';
(*p)--;
return TRUE;
}
else if(ch==44||ch==59||ch==40||ch==41||ch==91||ch==93||ch==123||ch==125)//第一个字符是界符,;()[]{}
{
word[i++]=ch;
word[i]='\0';
return TRUE;
}
else if(ch==34||ch==39) //第一个字符是"或'
{
word[i++]=ch;
chtemp=ch;
ch=input[(*p)++];
while(ch!=chtemp)
{
word[i++]=ch;
ch=input[(*p)++];
}
word[i++]=ch;
word[i]='\0';
return TRUE;
}
else if(ch==42||ch==47||ch==37||ch==33||ch==61||ch==60||ch==62) //第一个字符是=*/%!<>,这些字符后加一个=将是另一种算符
{
word[i++]=ch;
ch=input[*p];
if(ch==61)
{
word[i++]=ch;
(*p)++;
word[i]='\0';
}
else word[i]='\0';
return TRUE;
}
else if(ch==38||ch==124||ch==43||ch==45) //第一个字符是&|+-,若这些字符后加一个=,或者连续出现两次都将是另一个算符
{
word[i++]=ch;
chtemp=ch;
ch=input[*p];
if(ch==61)
{
word[i++]=ch;
(*p)++;
word[i]='\0';
}
else if(ch==chtemp)
{
word[i++]=ch;
(*p)++;
word[i]='\0';
}
else word[i]='\0';
return TRUE;
}
else if(ch==35||ch==46) //第一个字符是#或.
{
word[i++]=ch;
word[i]='\0';
return TRUE;
}
else if(ch==13) //换行
{
word[i++]=ch;
word[i]='\0';
return TRUE;
}
else goto again;//其他情况(一般指空格)
}
//////////////////////////////////////////////////////////////
BOOL keyword(char word[])
{
for(int i=1;i<KEYNUM;i++)
{
if(strcmp(key[i],word)==0) return TRUE;
}
return FALSE;
}
//////////////////////////////////////////////////////////////
int getwordtype(char word[])
{
if(word[0]==13) return(4);
else if(word[0]>=48&&word[0]<=57) return(3);
else if(word[0]==34||word[0]==39) return(3);
else if((word[0]>=65&&word[0]<=90)||(word[0]>=97&&word[0]<=122)||word[0]==95)
{
if(keyword(word)) return(1);
else return(2);
}
else return(1);
}
//////////////////////////////////////////////////////////////
void wordlist(struct wordstruct wordstructlist[],char *input,int *p,int results[])
{
char word[WORDLEN];
int type;
int i;
int resultp=0;
while(getword(input,p,word))
{
type=getwordtype(word);
if(type==4)
{
results[resultp++]=-1;
goto end;
}
for(i=1;i<wordcount;i++)
{
if(wordstructlist[i].wordtype==type)
{
if(strcmp(word,wordstructlist[i].worddetail)==0)
{
results[resultp++]=i;
goto end;
}
}
}
strcpy(wordstructlist[wordcount].worddetail,word);
wordstructlist[wordcount].wordtype=type;
results[resultp++]=wordcount;
wordcount++;
end:
;
}
results[resultp]=-2;
}
//////////////////////////////////////////////////////////////
void CAccidenceAnalyzerDlg::OnButton1()
{
CString filter="参数文件 (*.cpp)|*.cpp|All Files (*.*)|*.*||";
CFileDialog dlg(TRUE,"cpp","*.cpp",NULL,filter);
if(dlg.DoModal()==IDOK)
{
CFile thefile(dlg.GetPathName(), CFile::modeRead);
char *p;
DWORD dwfilelen;
dwfilelen=thefile.GetLength();
p=new char[dwfilelen+1];
p[dwfilelen]=0;
thefile.Read(p,dwfilelen);
m_show.SetWindowText(p);
thefile.Close();
}
}
//////////////////////////////////////////////////////////
void CAccidenceAnalyzerDlg::OnButton2()
{
char buffer[BUFFERNUM];
int i,j;
int *x;
char *input;
char *output;
DWORD dwfilelen;
dwfilelen=m_show.GetWindowTextLength();
input=new char[dwfilelen+1];
output=new char[dwfilelen+1];
m_show.GetWindowText(input,dwfilelen+1);
x=new int;
*x=0;j=0;
while(endoffile==FALSE)
{
prework(x,input,buffer);
for(i=0;buffer[i]!='\0';i++) {output[j++]=buffer[i];}
output[j++]='\n';
output[j++]='\r';
}
output[j]=0;
m_prework.SetWindowText(output);
}
//////////////////////////////////////////////////////////////
void CAccidenceAnalyzerDlg::OnButton3()
{
char *input;
int *p;
DWORD dwfilelen;
int i;
CString str;
int *results;
initialkey();
UpdateData(1);
m_list="";
m_result="";
dwfilelen=m_prework.GetWindowTextLength();
input=new char[dwfilelen+1];
m_prework.GetWindowText(input,dwfilelen+1);
results=new int[WORDLISTNUM];
p=new int;
*p=0;
wordlist(wordstructlist,input,p,results);
for(i=1;i<wordcount;i++)
{
if(wordstructlist[i].wordtype==1)
{
str.Format("%d:<%s,->\r\n",i,wordstructlist[i].worddetail);
}
if(wordstructlist[i].wordtype==2)
{
str.Format("%d:<标示符,%s>\r\n",i,wordstructlist[i].worddetail);
}
if(wordstructlist[i].wordtype==3)
{
str.Format("%d:<常数,%s>\r\n",i,wordstructlist[i].worddetail);
}
m_list+=str;
}
for(i=0;results[i]!=-2;i++)
{
if(results[i]==-1)
{
m_result+="\r\n";
}
else
{
if(results[i+1]!=-1)
{
str.Format("(%d)->",results[i]);
m_result+=str;
}
else
{
str.Format("(%d)",results[i]);
m_result+=str;
}
}
}
UpdateData(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -