📄 cykdlg.cpp
字号:
// CYKDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CYK.h"
#include "CYKDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CCYKDlg dialog
CCYKDlg::CCYKDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCYKDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCYKDlg)
m_ruleback = _T("");
m_rulefront = _T("");
m_string = _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 CCYKDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCYKDlg)
DDX_Control(pDX, IDC_LISTRESULT, m_listresult);
DDX_Control(pDX, IDC_RULELIST, m_rulelist);
DDX_Text(pDX, IDC_RULEBACK, m_ruleback);
DDX_Text(pDX, IDC_RULEFRONT, m_rulefront);
DDX_Text(pDX, IDC_String, m_string);
DDX_Text(pDX, IDC_RESULT, m_result);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCYKDlg, CDialog)
//{{AFX_MSG_MAP(CCYKDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_Begin, OnBegin)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCYKDlg message handlers
BOOL CCYKDlg::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 CCYKDlg::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 CCYKDlg::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 CCYKDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCYKDlg::OnAdd()
{CString str;
int lengthfront,lengthback;
UpdateData(TRUE);
str=m_rulefront+"->"+m_ruleback;
lengthfront=strlen(m_rulefront);
lengthback=strlen(m_ruleback);
if(lengthfront!=1||m_rulefront<"A"||m_rulefront>"Z"
||(lengthback!=1&&lengthback!=2)||(lengthback==1&&(m_ruleback<"a"||m_ruleback>"z"))
||(lengthback==2&&(m_ruleback.Left(1)<"A"||m_ruleback.Left(1)>"Z"))
||(lengthback==2&&(m_ruleback.Right(1)<"A"||m_ruleback.Right(1)>"Z")))
{AfxMessageBox("错误规则(只允许X->XX or X->x),请重新输入"); return;}
m_rulelist.AddString(str);
UpdateData(FALSE);
}
void CCYKDlg::OnDelete()
{int index=m_rulelist.GetCurSel();
if(index<0) return;
m_rulelist.DeleteString(index);
UpdateData(FALSE);
}
void CCYKDlg::OnBegin()
{int i,j,k,h;
int StringLen,RuleNum;
CString str;
UpdateData(TRUE);
StringLen=strlen(m_string);
CString** T;
MArray_2D(T,StringLen,StringLen,CString,"T[i][j]");
for(i=0;i<StringLen;i++)
for(j=0;j<StringLen;j++)
{T[i][j]=_T("");
}
RuleNum=m_rulelist.GetCount();
Rule* RuleMat;
RuleMat=new Rule[RuleNum];
for(i=0;i<RuleNum;i++)
{m_rulelist.GetText(i,str);
RuleMat[i].RuleFront=str.Left(1);
RuleMat[i].RuleBack=str.Mid(3,strlen(str));
}
//******************************
// CYK algorithm
//******************************
//*****************************************************************
for(i=1;i<=StringLen;i++)
{for(j=StringLen;j>=i;j--)
{for(h=0;h<RuleNum;h++)
{if(i==1){if((m_string.Right(j)).Left(1)==RuleMat[h].RuleBack)
T[StringLen-j][0]+=RuleMat[h].RuleFront;
}
else{for(k=1;k<i;k++)
{if(((T[StringLen-j][k-1].Find((RuleMat[h].RuleBack).Left(1)))!=-1)&&((T[StringLen-j+k][i-k-1].Find((RuleMat[h].RuleBack).Right(1)))!=-1))
{if((T[StringLen-j][i-1].Find(RuleMat[h].RuleFront))==-1)
T[StringLen-j][i-1]+=RuleMat[h].RuleFront;
}
}
}
}
}
}
if(T[0][StringLen-1].Find("S")==-1) m_result=_T("此字串不能由S生成");
else m_result=_T("此字串可以由S生成");
//******************************************************************
//************************************
// display the resultlist
//************************************
//******************************************************************
ListView_SetExtendedListViewStyle(this->m_listresult,LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_ONECLICKACTIVATE | LVS_EX_UNDERLINEHOT);
int ColNum= m_listresult.GetItemCount();
for(i=0;i<ColNum+1;i++)
{m_listresult.DeleteColumn(0);
}
m_listresult.DeleteAllItems();
m_listresult.InsertColumn(0,"j\\i",LVCFMT_LEFT,30,-1);
for(i=0;i<StringLen;i++)
{str.Format("%d",i+1);
m_listresult.InsertColumn(i+1,str,LVCFMT_LEFT,40,-1);
}
for(i=0;i<StringLen;i++)
{str.Format("%d",i+1);
m_listresult.InsertItem(i+1,str,0);
}
for(i=0;i<StringLen;i++)
for(j=0;j<StringLen-i;j++)
{m_listresult.SetItemText(i,j+1,T[j][i]);
}
delete[] RuleMat;
MFree_2D(T);
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -