📄 pagerankdlg.cpp
字号:
// pagerankDlg.cpp : implementation file
//
#include "stdafx.h"
#include "pagerank.h"
#include "pagerankDlg.h"
#include "AdjacencyWDigraph.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()
/////////////////////////////////////////////////////////////////////////////
// CPagerankDlg dialog
int CALLBACK ListCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CListCtrl * pV=(CListCtrl*)lParamSort;
CString szComp1,szComp2;
szComp1 = pV->GetItemText(lParam1,3);
szComp2 = pV->GetItemText(lParam2,3);
// int iCompRes;
double dComp1,dComp2;
dComp1 = atof(szComp1);
dComp2 = atof(szComp2);
double result = dComp1 - dComp2;
double temp = 0.0000001f;
return dComp1>dComp2? 1 : -1;
}
CPagerankDlg::CPagerankDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPagerankDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPagerankDlg)
m_Key = _T("");
m_prNum = 10;
m_prInit = 1.0f;
m_xs = 0.5f;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPagerankDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPagerankDlg)
DDX_Control(pDX, IDC_RECORD_LIST, m_Record_List);
DDX_Text(pDX, IDC_KEY_EDIT, m_Key);
DDX_Text(pDX, IDC_EDIT_NUM, m_prNum);
DDV_MinMaxInt(pDX, m_prNum, 5, 100);
DDX_Text(pDX, IDC_EDIT_INIT, m_prInit);
DDV_MinMaxFloat(pDX, m_prInit, 0.f, 1.f);
DDX_Text(pDX, IDC_EDIT_XS, m_xs);
DDV_MinMaxFloat(pDX, m_xs, 0.f, 1.f);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPagerankDlg, CDialog)
//{{AFX_MSG_MAP(CPagerankDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEARCH_BUTTON, OnSearchButton)
ON_BN_CLICKED(IDC_PAGERANK_BUTTON, OnPagerankButton)
ON_BN_CLICKED(IDC_SORT_BUTTON, OnSortButton)
ON_BN_CLICKED(IDC_CLEAR_BUTTON, OnClearButton)
ON_NOTIFY(LVN_COLUMNCLICK, IDC_RECORD_LIST, OnColumnclickRecordList)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPagerankDlg message handlers
BOOL CPagerankDlg::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
m_Record_List.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
LV_COLUMN h;
h.mask=LVCF_FMT|LVCF_TEXT|LVCF_WIDTH;
h.fmt=LVCFMT_LEFT;
h.cx=50;
h.pszText="ID";
m_Record_List.InsertColumn(0,&h);
h.cx=195;
h.pszText="WebPage";
m_Record_List.InsertColumn(1,&h);
h.cx=80;
h.pszText="Links";
m_Record_List.InsertColumn(2,&h);
h.cx=80;
h.pszText="pr value";
m_Record_List.InsertColumn(3,&h);
::CoInitialize(NULL);
m_pCon.CreateInstance(__uuidof(Connection));
try{
m_pCon->Open("Provider=Microsoft.Jet.OLEDB.4.0;\
Data Source=search.mdb","","",NULL);
}catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库search.mdb是否在当前路径下!");
exit(0);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CPagerankDlg::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 CPagerankDlg::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 CPagerankDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPagerankDlg::OnSearchButton()
{
// TODO: Add your control notification handler code here
variant_t vFieldValue;
CString strFieldValue;
_RecordsetPtr m_pRs;
m_pRs.CreateInstance(__uuidof(Recordset));
while(m_Record_List.GetItemCount()>0)
{
m_Record_List.DeleteItem(0);
}
UpdateData(true);
bool search=false;
CString strSql;
strSql.Format("%s",m_Key);
strSql="select * from pagerank where WebPage like '%"+strSql+"%'";
BSTR bsSql=strSql.AllocSysString();
try{
m_pRs->Open((CComVariant)bsSql,m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
if(!m_pRs->FirstOfFiles)
m_pRs->MoveFirst();
else
{
AfxMessageBox("没有找到满足条件的网页");
return;
}
int i=0;
while(VARIANT_FALSE==m_pRs->EndOfFile)
{
_variant_t vFieldValue;
CString strID,strWebPage,strPR,strLinks;
vFieldValue=m_pRs->GetCollect("id");
strID=(char *)_bstr_t(vFieldValue);
m_Record_List.InsertItem(i,strID);
m_Record_List.SetItemData(i,i);
vFieldValue=m_pRs->GetCollect("webpage");
strWebPage=(char *)_bstr_t(vFieldValue);
m_Record_List.SetItemText(i,1,strWebPage);
vFieldValue=m_pRs->GetCollect("links");
if(vFieldValue.vt != VT_NULL){
strLinks=(char *)_bstr_t(vFieldValue);
m_Record_List.SetItemText(i,2,strLinks);
}
i++;
m_pRs->MoveNext();
}
GetDlgItem(IDC_CLEAR_BUTTON)->EnableWindow();
GetDlgItem(IDC_PAGERANK_BUTTON)->EnableWindow();
GetDlgItem(IDC_SEARCH_BUTTON)->EnableWindow();
GetDlgItem(IDC_SORT_BUTTON)->EnableWindow(FALSE);
m_pRs->Close();
m_pRs = NULL;
//添加
this->pagerank_value();
this->sort_value();
}catch(_com_error * e){
AfxMessageBox(e->ErrorMessage());
if(m_pRs != NULL && m_pRs->State){
m_pRs->Close();
m_pRs = NULL;
}
return;
}
}
void CPagerankDlg::OnPagerankButton()
{
// TODO: Add your control notification handler code here
int nCount = m_Record_List.GetItemCount();
int ** relationArray;
relationArray = new int * [nCount];
for(int i = 0; i < nCount; i++){
relationArray[i] = new int [nCount];
}
for(i=0; i < nCount; i++){
CString links;
links = this->m_Record_List.GetItemText(i,2);
for(int j = 0; j < nCount; j++){
CString id;
id = this->m_Record_List.GetItemText(j,0);
int index = links.FindOneOf(id);
if(index != -1) relationArray[i][j] = 1;
else relationArray[i][j] = 0;
}
}
CString strout;
for(i = 0;i < nCount; i++){
for(int j=0; j<nCount;j++){
if(relationArray[i][j] == 0){
strout += "0 ";
}else{
strout += "1 ";
}
}
strout+="\n";
}
MessageBox(strout,"网页间的关系矩阵");
UpdateData();
AdjacencyWDigraph<int> graph(nCount, 0, relationArray, m_prNum, m_prInit ,m_xs);
float *pr = graph.calculate();
CString prOut;
for( i = 0;i < nCount; i++){
CString temp;
CString p;
p.Format("P%s=",m_Record_List.GetItemText(i,0));
temp.Format("%f\n",pr[i]);
prOut += p + temp;
}
GetDlgItem(IDC_SORT_BUTTON)->EnableWindow();
MessageBox(prOut,"PageRank value");
for( i = 0;i < nCount; i++){
CString temp;
temp.Format("%f",pr[i]);
m_Record_List.SetItemText(i,3,temp);
}
SetDlgItemText(IDC_SORT_BUTTON,"网页排序");
delete [] pr;
//afxDump << links;
}
void CPagerankDlg::OnSortButton()
{
// TODO: Add your control notification handler code here
_variant_t vFieldValue;
CString strFieldValue;
_RecordsetPtr m_pRs;
m_Record_List.SortItems( ListCompare, (DWORD)&m_Record_List );
CString capture;
GetDlgItemText(IDC_SORT_BUTTON,capture);
if(capture == "网页排序"){
capture = "还原";
}else{
capture = "网页排序";
}
SetDlgItemText(IDC_SORT_BUTTON,capture);
}
void CPagerankDlg::OnClearButton()
{
// TODO: Add your control notification handler code here
while(m_Record_List.GetItemCount()>0)
{
m_Record_List.DeleteItem(0);
}
GetDlgItem(IDC_CLEAR_BUTTON)->EnableWindow(FALSE);
GetDlgItem(IDC_PAGERANK_BUTTON)->EnableWindow(FALSE);
GetDlgItem(IDC_SORT_BUTTON)->EnableWindow(FALSE);
UpdateData(TRUE);
}
/*
* 类似java字符串的split()方法。
* 使用一个完整的串delimit(而不是其中的某个字符)来分割src串,没有trim选项,
* 即严格分割。num用来确定最多分割为多少个串,如果是0(默认),则按照delimit
* 分割,若为1,则返回源串。
*/
void CPagerankDlg::Split(const CString& src, CString delimit,
CStringList* pOutList, int num, CString nullSubst){
ASSERT( !src.IsEmpty() && !delimit.IsEmpty() );
if(num==1)
{
pOutList->AddTail(src);
return;
}
int deliLen = delimit.GetLength();
long index = -1, lastSearchPosition = 0, cnt = 0;
while( (index=src.Find(delimit, lastSearchPosition))!=-1 )
{
if(index==lastSearchPosition)
pOutList->AddTail(nullSubst);
else
pOutList->AddTail(src.Mid(lastSearchPosition, index-lastSearchPosition));
lastSearchPosition = index + deliLen;
if(num)
{
++cnt;
if(cnt+1==num) break;
}
}
CString lastOne = src.Mid(lastSearchPosition);
pOutList->AddTail( lastOne.IsEmpty()? nullSubst:lastOne);
}
void CPagerankDlg::OnColumnclickRecordList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
m_Record_List.SortItems( ListCompare, (DWORD)&m_Record_List );
*pResult = 0;
}
void CPagerankDlg::pagerank_value()
{
int nCount = m_Record_List.GetItemCount();
int ** relationArray;
relationArray = new int * [nCount];
for(int i = 0; i < nCount; i++){
relationArray[i] = new int [nCount];
}
//计算链接begin
for(i=0; i < nCount; i++){
CString links;
links = this->m_Record_List.GetItemText(i,2);
for(int j = 0; j < nCount; j++){
CString id;
id = this->m_Record_List.GetItemText(j,0);
int index = links.FindOneOf(id);
if(index != -1) relationArray[i][j] = 1;
else relationArray[i][j] = 0;
}
}
CString strout;
for(i = 0;i < nCount; i++){
for(int j=0; j<nCount;j++){
if(relationArray[i][j] == 0){
strout += "0 ";
}else{
strout += "1 ";
}
}
strout+="\n";
}
//计算链接end
UpdateData();
AdjacencyWDigraph<int> graph(nCount, 0, relationArray, m_prNum, m_prInit,m_xs);
float *pr = graph.calculate();
CString prOut;
for( i = 0;i < nCount; i++){
CString temp;
CString p;
p.Format("P%s=",m_Record_List.GetItemText(i,0));
temp.Format("%f\n",pr[i]);
prOut += p + temp;
}
GetDlgItem(IDC_SORT_BUTTON)->EnableWindow();
for( i = 0;i < nCount; i++){
CString temp;
temp.Format("%f",pr[i]);
m_Record_List.SetItemText(i,3,temp);
}
delete [] pr;
}
void CPagerankDlg::sort_value()
{
_variant_t vFieldValue;
CString strFieldValue;
_RecordsetPtr m_pRs;
m_Record_List.SortItems( ListCompare, (DWORD)&m_Record_List );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -