⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fp_treeview.cpp

📁  关联规则fp算法
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// fp_treeView.cpp : implementation of the CFp_treeView class
//

#include "StdAfx.h"
#include "fp_tree.h"

#include "fp_treeDoc.h"
#include "fp_treeView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFp_treeView

IMPLEMENT_DYNCREATE(CFp_treeView, CFormView)

BEGIN_MESSAGE_MAP(CFp_treeView, CFormView)
	//{{AFX_MSG_MAP(CFp_treeView)
	ON_BN_CLICKED(IDC_SOURCE, OnSource)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_EXECUTE, OnExecute)
	ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect)
	ON_BN_CLICKED(IDC_QUIT, OnQuit)
	ON_LBN_SELCHANGE(IDC_LIST, OnSelchangeList)
	ON_BN_CLICKED(IDC_FPTREE, OnFptree)
	ON_BN_CLICKED(IDC_TEST1, OnTest1)
	ON_LBN_DBLCLK(IDC_LIST1, OnDblclkList1)
	ON_LBN_DBLCLK(IDC_LIST5, OnDblclkList5)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_COMMAND(IDM_SOURCE, OnSource)
	ON_COMMAND(IDM_CONNECT, OnConnect)
	ON_COMMAND(IDM_EXECUTE, OnExecute)
	ON_COMMAND(IDM_DISCONNECT, OnDisconnect)
	ON_COMMAND(IDM_QUIT, OnQuit)
	ON_BN_CLICKED(IDC_DEL, OnDel)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFp_treeView construction/destruction

CFp_treeView::CFp_treeView()
	: CFormView(CFp_treeView::IDD)
{
	//{{AFX_DATA_INIT(CFp_treeView)
	m_strConnection = _T("");
	m_strSQL = _T("");
	m_Edit1 = _T("");
	m_Edit2 = 3;
	m_dmTime = _T("挖掘时间:");
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_ButtonFlag=0;
	m_MenuFlag=0;
	m_minCount=3;
	m_strTableName= _T("");
	m_strColName= _T("");
	isDesc=FALSE;
	headTableArray.RemoveAll();
	tempArray.RemoveAll();
	fptree.setNull();
	sortVector.clear();
}

CFp_treeView::~CFp_treeView()
{
}

void CFp_treeView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFp_treeView)
	DDX_Control(pDX, IDC_LIST6, m_List6);
	DDX_Control(pDX, IDC_LIST5, m_List5);
	DDX_Control(pDX, IDC_LIST4, m_List4);
	DDX_Control(pDX, IDC_LIST3, m_List3);
	DDX_Control(pDX, IDC_LIST2, m_List2);
	DDX_Control(pDX, IDC_LIST1, m_List1);
	DDX_Control(pDX, IDC_LIST, m_ListBox);
	DDX_Text(pDX, IDC_CONNECTION_STRING, m_strConnection);
	DDX_Text(pDX, IDC_EXECUTE_STRING, m_strSQL);
	DDX_Control(pDX, IDC_DATAGRID, m_DataGrid);
	DDX_Text(pDX, IDC_EDIT1, m_Edit1);
	DDX_Text(pDX, IDC_EDIT2, m_Edit2);
	DDV_MinMaxUInt(pDX, m_Edit2, 1, 1000);
	DDX_Text(pDX, IDC_TIME, m_dmTime);
	//}}AFX_DATA_MAP
}

BOOL CFp_treeView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CFp_treeView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CFp_treeView printing

BOOL CFp_treeView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CFp_treeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CFp_treeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CFp_treeView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CFp_treeView diagnostics

#ifdef _DEBUG
void CFp_treeView::AssertValid() const
{
	CFormView::AssertValid();
}

void CFp_treeView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CFp_treeDoc* CFp_treeView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFp_treeDoc)));
	return (CFp_treeDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFp_treeView message handlers

void CFp_treeView::GenerateError(HRESULT hr, PWSTR pwszDescription)
{
	CString m_strError;
	m_strError.Format("Run-time error '%d(%x)'",hr,hr);
	m_strError+="\n\r";
	m_strError+=pwszDescription;
	AfxMessageBox(m_strError);
}

void CFp_treeView::OnSource() 
{
	// TODO: Add your control notification handler code here
	HRESULT hr;
	IDataSourceLocatorPtr m_dlPrompt=NULL;
	_ConnectionPtr m_Conn=NULL;

	//初始化COM
	::CoInitialize(NULL);
	//创建IDataSourceLocatorPtr的实例
	hr=m_dlPrompt.CreateInstance(_uuidof(DataLinks));
	//弹出数据连接的对话框
	m_Conn=m_dlPrompt->PromptNew();
	if(m_Conn!=NULL)
	{
		//将连接字符串复制到m_strConnection中
		m_strConnection.Format("%s",(char*)m_Conn->ConnectionString);
		//将变量中的值保存到控件EDIT2中
		UpdateData(FALSE);

		m_MenuFlag=1;
	}
	
}

void CFp_treeView::getTables()
{
	_bstr_t tablesNames;
	CString kooky;
	//先将ListBox 清空
	m_ListBox.ResetContent();
	//将控件中的值保存到变量中
	UpdateData(TRUE);
	try
	{
		//创建实例
		m_Recordset.CreateInstance(_uuidof(Recordset));
		//以只读方式打开结果集,得到表名信息
		m_Recordset=m_Connection->OpenSchema(adSchemaTables,vtMissing,vtMissing);
		//如果结果集没有结束
		while(!m_Recordset->adoEOF)
		{
			//得到表项名字
			tablesNames=m_Recordset->GetCollect("TABLE_NAME");
			kooky=(char*)tablesNames;
			//如果是表项,则加到ListBox中
			if(kooky.Left(4)!="MSys"&&kooky.Left(3)!="sys"&&kooky.Left(12)!="dtproperties")
				m_ListBox.AddString(kooky);
			//移到下一个表项
			m_Recordset->MoveNext();
		}
	}
	//捕获异常_com_error
	catch(_com_error &e)
	{
		GenerateError(e.Error(),e.Description());
	}
	//捕获其他异常
	catch(...){}

	//将变量中的值保存到控件ListBox中
	UpdateData(FALSE);
	//最后将结果集置空
	m_Recordset=NULL;
}

void CFp_treeView::OnConnect() 
{
	// TODO: Add your control notification handler code here
	if(m_MenuFlag==0)
		return;

	//更新变量值
	UpdateData(TRUE);
	try
	{
		//创建实例
		m_Connection.CreateInstance(_uuidof(Connection));
		//根据连接字符串开启数据熔接
		m_Connection->Open(_bstr_t(m_strConnection.GetBuffer(0)),"","",-1);
	}
	//捕获异常_com_error
	catch(_com_error&e)
	{
		GenerateError(e.Error(),e.Description());
	}
	//捕获其他异常
	catch(...){}

	//将变量保存到ListBox中
	UpdateData(FALSE);
	//显示表项名
	getTables();

	m_MenuFlag=2;
	
}


void CFp_treeView::OnExecute() 
{
	// TODO: Add your control notification handler code here
	if(m_strSQL.GetLength()==0)
		return;
	try
	{
		//创建实例
		m_Recordset.CreateInstance(_uuidof(Recordset));
		//将控件中的值保存到变量中,主要是保存SQL语句
		UpdateData(TRUE);
		//设定光标服务
		m_Connection->CursorLocation=adUseClient;
		//根据连接字符串开启数据连接,得到结果集
		m_Recordset->Open(m_strSQL.GetBuffer(0),m_Connection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
	}
	//捕获异常_com_error
	catch (_com_error &e)
	{
		GenerateError(e.Error(),e.Description());
	}
	//捕获其他异常
	catch(...){}
	//将结果集中的内存在datagrid中显示出来
	m_DataGrid.SetRefDataSource((LPUNKNOWN)m_Recordset);
	//刷新DataGrid
	m_DataGrid.Refresh();
	//将变量保存到DataGrid中
	UpdateData(FALSE);
	//将结果集置空
	m_Recordset=NULL;

}


void CFp_treeView::OnDisconnect() 
{
	// TODO: Add your control notification handler code here
	if(m_MenuFlag!=2)
		return;
	try
	{
		//关闭连接
		m_Connection->Close();
	}
	//捕获异常_com_error
	catch (_com_error &e)
	{
		GenerateError(e.Error(),e.Description());
	}
	//捕获其他异常
	catch(...){}
	//清空ListBox控件
	m_ListBox.ResetContent();
	//将DataGrid控件置空
	m_DataGrid.SetRefDataSource(NULL);
	//将DataGrid控件设置成默认状态
	m_DataGrid.ClearFields();
	//刷新DataGrid
	m_DataGrid.Refresh();
	//将SQL语句清空
	//这里不清空连接语句,是为了断开连接以后如果重新连接不需要重新设定数据源
	m_strSQL="";
	//将变量值保存到控件中
	UpdateData(FALSE);

	m_MenuFlag=1;
	m_ButtonFlag=0;
	resetList();
}

void CFp_treeView::OnQuit() 
{
	// TODO: Add your control notification handler code here
	try
	{
		//关闭记录集
		if(m_Recordset!=NULL)
			m_Recordset->Close();
		//关闭连接
		if(m_Connection!=NULL)
			m_Connection->Close();
	}
	catch(...){}
	//退出
	
	
	resetList();
	fptree.~CTree();
	headTableArray.~CArray();
	tempArray.~CArray();

	WinExec("regsvr32 MSDATGRD.OCX /u /s",1);
	PostMessage(WM_QUIT);
}


void CFp_treeView::OnSelchangeList() 
{
	// TODO: Add your control notification handler codhere
	//得到鼠标单击项在ListBox中的索引值
	int index=m_ListBox.GetCurSel();
	//根据索引值得到表项的名字
	m_ListBox.GetText(index,m_strTableName);
	//构造SQL查询语句
	m_strSQL="select * from ["+m_strTableName+"]";
	//将变量值保存在控件中
	UpdateData(FALSE);
	//进行查询
	OnExecute();
	
	//列名未定
	m_strColName="";
	m_ButtonFlag=0;
	resetList();
}

BEGIN_EVENTSINK_MAP(CFp_treeView, CFormView)
    //{{AFX_EVENTSINK_MAP(CFp_treeView)
	ON_EVENT(CFp_treeView, IDC_DATAGRID, 216 /* HeadClick */, OnHeadClickDatagrid, VTS_I2)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CFp_treeView::OnHeadClickDatagrid(short ColIndex) 
{
	// TODO: Add your control notification handler code here
	//得到DataGrid中表示各列信息的类
	CColumns cols=m_DataGrid.GetColumns();
	//此参数有用来得具体某一列
	VARIANT index;
	//值为index
	index.intVal=ColIndex;
	//类型为整型
	index.vt=VT_I4;
	//得到单击列
	CColumn col=cols.GetItem(index);
	//得到此列的名字
	m_strColName=col.GetCaption();

	//排序必须在已建立连接的情况下
	/*
	if(m_Connection!=NULL)
	{
		//排序还必须在表名存在的情况下
		if(m_strTableName.GetLength()!=0)
		{
			//先得到查询语句的长度
			int nOriginalLength=m_strSQL.GetLength();
			//构造新的查询语句
			CString strTmp="order by ["+m_strColName+"]";
			if(isDesc)
			{
				strTmp=strTmp+"desc";
				isDesc=FALSE;
			}
			else
				isDesc=TRUE;
			m_strSQL+=strTmp;
			//将变量值保存到控件中
			UpdateData(FALSE);
			//对新的查询语句进行查询
			OnExecute();
			//恢复原查询语句,这样做是为了再单击别的列,可以对别的列进行排序
			m_strSQL.Delete(nOriginalLength,strTmp.GetLength());
		}
	}*/
	m_ButtonFlag=1;
}

void CFp_treeView::OnFptree() 
{
	if(m_ButtonFlag==0)//没有选中列
		return;
	// TODO: Add your control notification handler code here
	//头表数组清空; vector清空; FP树清空
	headTableArray.RemoveAll();
	sortVector.clear();
	fptree.setNull();
////////////////////必须在连接和表名存在的情况下////////////////
	if(m_Connection!=NULL&&m_strTableName.GetLength()!=0)
	{
		try
		{
			//创建实例
			m_Recordset.CreateInstance(_uuidof(Recordset));
			//将控件中的值保存到变量中,主要是保存SQL语句
			UpdateData(TRUE);
			//设定光标服务
			m_Connection->CursorLocation=adUseClient;
			//根据连接字符串开启数据连接,得到结果集
			m_Recordset->Open(m_strSQL.GetBuffer(0),m_Connection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
			
			m_Recordset->MoveFirst();
			CString str,templeft,tempright;
			
			while(!m_Recordset->adoEOF)
			{
				_variant_t   varcheck;   
				varcheck=m_Recordset->GetCollect(_variant_t(m_strColName));
				if(varcheck.vt!=VT_NULL)
				{
					str=(char*)(_bstr_t)varcheck;	
				
					str.TrimLeft();
					str.TrimRight();
					int temp=0;

					char sign;
					if(str.Find(',')!=-1) sign=',';
					else if(str.Find('|')!=-1) sign='|';
					else if(str.Find(' ')!=-1) sign=' ';
						
					while(temp!=-1)
					{
						temp=str.Find(sign);
						
						if(temp==-1)
							templeft=str;
						else
							templeft=str.Left(temp);
					
						tempright=str.Right(str.GetLength()-temp-1);
						
						int index=checkItem(headTableArray,templeft);
						
						if(index==-1)//no this item in Array,add it into Array
						{
							CHeadTable headTable;
							headTable.setCount(1);
							headTable.setItemName(templeft);
							
							headTableArray.Add(headTable);
						}
						else 
						{	
							CHeadTable headTable;
							headTable=headTableArray.GetAt(index);
							headTable.setCount(headTable.getCount()+1);
							
							headTableArray.SetAt(index,headTable);
							//排序头表 每变化一次排序一下,从大到小
							sortItem(index,headTable.getCount());
						}
						
						str=tempright;
					}//while end of temp
				}
				m_Recordset->MoveNext();
			} //the end of while of (adoEOF)

			m_Recordset->Close();
		}
		//捕获异常_com_error
		catch (_com_error &e)
		{
			GenerateError(e.Error(),e.Description());
		}
		//捕获其他异常
		catch(...){}
		

	/////////////////////////////头表剪枝 去掉支持度较小的项///////////
		UpdateData();
		for(int i=headTableArray.GetSize()-1;i>=0;i--)
		{
			if(headTableArray.GetAt(i).getCount()>=m_Edit2) 
				break;
			else
				headTableArray.RemoveAt(i);
		}
		m_minCount=m_Edit2;//更新最小支持度
/////////////////////////////头表好了测试一下///////////////////
		m_List1.ResetContent();
		char buffer[20];
		for(int j=0;j<headTableArray.GetSize();j++)
		{
			m_List1.AddString(headTableArray.GetAt(j).getItemName()+"-"+(_itoa)(headTableArray.GetAt(j).getCount(),buffer,10));
		}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -