📄 filedlg.cpp
字号:
// FileDlg.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "FileDlg.h"
#include "MainFrm.h"
#include "FileDlgDoc.h"
#include "FileDlgView.h"
#include "MyFileDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
struct id_node
{
char *type;
CString id_value;
}node[100];
/////////////////////////////////////////////////////////////////////////////
// CFileDlgApp
BEGIN_MESSAGE_MAP(CFileDlgApp, CWinApp)
//{{AFX_MSG_MAP(CFileDlgApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_WORD_ANALYSIS, OnWordAnalysis)
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileDlgApp construction
CFileDlgApp::CFileDlgApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
id_num=0;
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CFileDlgApp object
CFileDlgApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CFileDlgApp initialization
BOOL CFileDlgApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CFileDlgDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CFileDlgView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// 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)
// No message handlers
//}}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()
// App command to run the dialog
void CFileDlgApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CFileDlgApp message handlers
void CFileDlgApp::OnFileOpen()
{
// TODO: Add your command handler code here
CMyFileDlg dlg(TRUE,"", NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"TextFiles (*.txt)|*.txt|All Files(*.*)|*.*||",
NULL );
if (dlg.DoModal() == IDOK)
{
OpenDocumentFile(dlg.GetPathName());
}
filename=dlg.GetPathName();
}
void CFileDlgApp::OnWordAnalysis()
{
// TODO: Add your command handler code here
FILE *fp;
fp=fopen(filename,"r+");
int type;
char cur;
// char forward;
CString display_last;
CString display_cur;
CString mid_value;
// forward=cur;
// display_last=NULL;
// display_cur=NULL;
// mid_value=NULL;
while(cur!=EOF)
{
id_num=0;
cur=getc(fp);
while(cur!='\n'&&cur!=EOF)
{
// node[id_num].type=Identify(&cur,fp,&forward);
// cur=forward;
buf[id_num]=cur;
cur=getc(fp);
id_num++;
if(cur=='\n'||cur==EOF)
buf[id_num]=cur;
}
type=Identify(buf);
for(int i=0;i<line_id_num;i++)
{
display_last.Format("%s=%s \n",node[i].type,node[i].id_value);
mid_value+=display_last;
}
// display_cur=mid_value;
display_cur+=mid_value;
mid_value.Empty();
// display_last=display_cur;
DisplayDlg display(NULL,display_cur);
if(display.DoModal()==IDOK)
{
;
}
// cur=getc(fp);
// node[store_pos++].id_value=GE;
// node[store_pos].type=RELOP;
}
}
char CFileDlgApp::nextchar(char *buf,int i)
{
char c;
c=buf[i];
return c;
}
int CFileDlgApp::isalnum(int c) //判断是不是在0-9,字母
{
if((48<=c&&c<=57)||(65<=c&&c<=90)||(97<=c&&c<=122))
return 1;
else
return 0;
}
int CFileDlgApp::isalpha(int c) //判断是不在A-Z或a-z
{
if((65<=c&&c<=90)||(97<=c&&c<=122))
return 1;
else
return 0;
}
int CFileDlgApp::isdigit(int c)
{
if(48<=c&&c<=57)
return 1;
else
return 0;
}
int CFileDlgApp::ispecial(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')')
return 1;
else
return 0;
}
int CFileDlgApp::findstate() //识别不同的DFC
{
switch(start)
{
case 0:
start=9;
break;
case 9:
start=12;
break;
case 12:
start=20;
break;
case 20:
start=25;
break;
case 25:
start=28;
break;
case 28:
start=31;
break;
case 31: //識別特殊字符+ - * \ ( ) ; , .
start=0;
break;
default:
start=0;
break;
}
return start;
}
int CFileDlgApp::Identify(char *buf)
{
char c;
// char forward;
int cur;
int forward;
int i;
char value[10];
static int pos;
static int store_pos;
start=state=0;
c=' ';
pos=0;
store_pos=0;
forward=cur=0;
for(i=0;i<=9;i++)
value[i]=NULL;
// c=buf[0];
while(c!=EOF&&c!='\n')
{
switch(state)
{
case 0:
// *forward=c;
c=nextchar(buf,pos++);
if(c==' '||c=='\t')
{
c=nextchar(buf,pos++);
break;
}
else if(c=='\n')
break;
else if(c==EOF)
break;
else if(c=='<')
state=1;
else if(c=='=')
state=5;
else if(c=='>')
state=6;
else
{
state=findstate();
pos--;
}
break;
case 1:
c=nextchar(buf,pos++);
if(c=='=')
state=2;
else if(c=='>')
state=3;
else
state=4;
break;
case 2:
node[store_pos].type="RELOP";
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos].id_value=value;
store_pos++;
state=findstate();
break;
case 3:
node[store_pos].type="RELOP";
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos++].id_value=value;
state=findstate();
break;
case 4:
node[store_pos].type="RELOP";
pos--;
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos++].id_value=value;
state=findstate();
break;
case 5:
node[store_pos].type="RELOP";
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos++].id_value=value;
state=findstate();
break;
case 6:
c=nextchar(buf,pos++);
if(c=='=')
state=7;
else
state=8;
break;
case 7:
node[store_pos].type="RELOP";
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos++].id_value=value;
state=findstate();
break;
case 8:
node[store_pos].type="RELOP";
pos--;
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos++].id_value=value;
state=findstate();
break;
case 9:
c=nextchar(buf,pos++);
if(isalpha((int)c))
state=10;
else
{
state=findstate();
pos--;
}
// c=nextchar(buf,pos++);
break;
case 10:
c=nextchar(buf,pos++);
if(!isalnum((int)c))
state=11;
if(state==11)
break;
break;
case 11:
node[store_pos].type="ID";
pos--;
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos++].id_value=value;//modify
state=findstate();
break;
case 12:
c=nextchar(buf,pos++);
if(isdigit((int)c))
state=13;
else
{
state=findstate();
pos--;
}
break;
case 13:
c=nextchar(buf,pos++);
if(c=='.')
state=14;
else if(c=='E')
state=16;
else if(!isdigit((int)c))
state=19;
// state=findstate();
break;
case 14:
c=nextchar(buf,pos++);
if(isdigit((int)c))
state=15;
else
state=findstate();
break;
case 15:
c=nextchar(buf,pos++);
if(c=='E')
state=16;
else if(!isdigit((int)c))
state=findstate();
break;
case 16:
c=nextchar(buf,pos++);
if(c=='+'||c=='-')
state=17;
else if(isdigit((int)c))
state=18;
else
state=findstate();
break;
case 17:
c=nextchar(buf,pos++);
if(isdigit((int)c))
state=18;
else
state=findstate();
break;
case 18:
c=nextchar(buf,pos++);
if(!isdigit((int)c))
state=19;
else
state=findstate();
break;
case 19:
node[store_pos].type="NUM";
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos++].id_value=value;
pos--;
state=findstate();
break;
case 20:
c=nextchar(buf,pos++);
if(isdigit((int)c))
state=21;
else
{
state=findstate();
pos--;
}
break;
case 21:
c=nextchar(buf,pos++);
if(c=='.')
state=22;
else if(!isdigit((int)c))
{
state=findstate();
pos--;
}
break;
case 22:
c=nextchar(buf,pos++);
if(isdigit((int)c))
state=23;
else
{
state=findstate();
pos--;
}
break;
case 23:
c=nextchar(buf,pos++);
if(!isdigit((int)c))
state=24;
break;
case 24:
node[store_pos].type="NUM";
pos--;
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos++].id_value=value;
state=findstate();
break;
case 25:
c=nextchar(buf,pos++);
if(isdigit((int)c))
state=26;
else
{
state=findstate();
pos--;
}
break;
case 26:
c=nextchar(buf,pos++);
if(!isdigit((int)c))
state=27;
else
{
state=findstate();
pos--;
}
break;
case 27:
node[store_pos].type="NUM";
pos--;
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos++].id_value=value;
state=findstate();
break;
case 28:
c=nextchar(buf,pos++);
if(c==':')
state=29;
else
{
state=findstate();
pos--;
}
break;
case 29:
c=nextchar(buf,pos++);
if(c=='=')
state=30;
else
{
state=findstate();
pos--;
}
break;
case 30:
node[store_pos].type="SPECIAL_WORD";
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos++].id_value=value;
state=findstate();
break;
case 31:
c=nextchar(buf,pos++);
if(ispecial(c))
state=32;
else
{
state=findstate();
pos--;
}
break;
case 32:
node[store_pos].type="SPECIAL_WORD";
cur=pos;
for( i=0;forward<cur;forward++,i++)
value[i]=buf[forward];
forward=cur;
for(;i<=10;i++)
value[i]=' ';
node[store_pos].id_value=value;
store_pos++;
state=findstate();
break;
default:
break;
}
}
line_id_num=store_pos;
return END;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -