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

📄 dialogreaddata.cpp

📁 实现数据挖掘的一个重要算法sliq。能够从文本中读出数据
💻 CPP
字号:
// DialogReadData.cpp : implementation file
//

#include "stdafx.h"
#include "Sliq.h"
#include "DialogReadData.h"
#include "WzdDirDlg.h"
#include <stdlib.h>

#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialogReadData dialog


CDialogReadData::CDialogReadData(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogReadData::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogReadData)
	m_Edit_SaveAddress = _T("");
	m_Edit_ReadAddress = _T("");
	//}}AFX_DATA_INIT
}


void CDialogReadData::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogReadData)
	DDX_Control(pDX, IDC_COMBO_DATATYPE, m_ComboBox_ctrTypeList);
	DDX_Control(pDX, IDC_COMBO_ATTRIBUTELIST, m_ComboBox_ctrList);
	DDX_Text(pDX, IDC_EDIT_SAVEADDRESS, m_Edit_SaveAddress);
	DDX_Text(pDX, IDC_EDIT_ADDRESS, m_Edit_ReadAddress);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogReadData, CDialog)
	//{{AFX_MSG_MAP(CDialogReadData)
	ON_BN_CLICKED(IDC_BUTTON_READDATA, OnButtonReaddata)
	ON_BN_CLICKED(IDC_BUTTON_SAVEDATA, OnButtonSavedata)
	ON_CBN_EDITCHANGE(IDC_COMBO_DATATYPE, OnEditchangeComboDatatype)
	ON_BN_CLICKED(IDC_BUTTON_AC, OnButtonAc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogReadData message handlers

map<CString,CString>attributelist;

void CDialogReadData::AddAttribute()
{
	CString SaveAttribute = m_Edit_SaveAddress+"\\AttributeList.txt";
	ifstream fin(m_Edit_ReadAddress.GetBuffer(m_Edit_ReadAddress.GetLength()),ios::in);
	ofstream fout(SaveAttribute.GetBuffer(SaveAttribute.GetLength()),ios::out);
	string str;
	while(fin.peek()!='\n')
	{
		fin>>str;
		m_ComboBox_ctrList.AddString(str.c_str());
		attributelist[str.c_str()] = "";
		fout<<str<<endl;
	}
	fin.close();
	fout.close();
}


void CDialogReadData::DataPretreat()
{
	map<CString,CString>::iterator i;

	for(i = attributelist.begin();i != attributelist.end();i++)
	{
		int n,m,k=0,NumOfList;
		ifstream fin(m_Edit_ReadAddress.GetBuffer(m_Edit_ReadAddress.GetLength()),ios::in);
		string str;
		while(fin.peek()!='\n')
		{
			fin>>str;
			if(str.c_str() == i->first)
				m=k;
			k++;
		}
		NumOfList = k;
		if(i->second == "CString")
		{
			k=0;
			multimap<CString,int>SortMap;
			int nIndex;
			while(fin>>str)
			{
				if(k==0)
					nIndex = atoi(str.c_str());
				if(k==m)
					SortMap.insert(make_pair(str.c_str(),nIndex));
				//	SortMap[str.c_str()] = nIndex;
				k++;
				if(k==NumOfList)
					k=0;
			}
			
			CString SavePath = m_Edit_SaveAddress + "\\" + i->first + ".txt";
			ofstream fout(SavePath,ios::out);
			multimap<CString,int>::iterator s;
			for(s = SortMap.begin();s != SortMap.end();s++)
			{
				fout<<s->second<<" "<<(LPSTR)(LPCTSTR)s->first<<endl;
			}
			fout.close();
		}
		else if(i->second == "double")
		{
			k=0;
			multimap<double,int>SortMap;
			int nIndex;
			while(fin>>str)
			{
				if(k==0)
					nIndex = atoi(str.c_str());
				if(k==m)
					SortMap.insert(make_pair(atof(str.c_str()),nIndex));
				//	SortMap[atof(str.c_str())] = nIndex;
				k++;
				if(k==NumOfList)
					k=0;
			}

			CString SavePath = m_Edit_SaveAddress + "\\" + i->first + ".txt";
			ofstream fout(SavePath,ios::out);
			multimap<double,int>::iterator s;
			for(s = SortMap.begin();s != SortMap.end();s++)
			{
				fout<<s->second<<" "<<s->first<<endl;
			}
			fout.close();
		}
		else if(i->second == "int")
		{
			k=0;
			multimap<int,int>SortMap;
			int nIndex;
			while(fin>>str)
			{
				if(k==0)
					nIndex = atoi(str.c_str());
				if(k==m)
					SortMap.insert(make_pair(atoi(str.c_str()), nIndex));
			//		SortMap[atoi(str.c_str())] = nIndex;
				k++;
				if(k==NumOfList)
					k=0;
			}

			CString SavePath = m_Edit_SaveAddress + "\\" + i->first + ".txt";
			ofstream fout(SavePath,ios::out);
			multimap<int,int>::iterator s;
			for(s = SortMap.begin();s != SortMap.end();s++)
			{
				fout<<s->second<<" "<<s->first<<endl;
			}
			fout.close();
		}
		m=0;
	}
}

void CDialogReadData::OnButtonReaddata() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CFileDialog dlg(true);
	if(IDOK == dlg.DoModal())
	{
		m_Edit_ReadAddress = dlg.GetPathName();
		UpdateData(false);
	}
}


void CDialogReadData::OnButtonSavedata() 
{
	// TODO: Add your control notification handler code here
	if(m_Edit_ReadAddress=="")
	{
		AfxMessageBox("请先选择数据文件");
		return;
	}
	UpdateData(true);
	CWzdDirDlg dlg;
	CString dir=dlg.GetDirectory(this);
	if(!dir.IsEmpty())
	{
		m_Edit_SaveAddress = dir;
		UpdateData(FALSE);	
	}
	AddAttribute();
}


void CDialogReadData::OnButtonDistillattribute() 
{
	// TODO: Add your control notification handler code here
	AddAttribute();
	CString SaveAttribute = m_Edit_SaveAddress+"\\AttributeList.txt";
	ifstream fin(SaveAttribute.GetBuffer(SaveAttribute.GetLength()),ios::in);
	string str;
	while(fin>>str)
	{
		m_ComboBox_ctrList.AddString(str.c_str());
	}
}

void CDialogReadData::OnEditchangeComboDatatype() 
{
	// TODO: Add your control notification handler code here
}

void CDialogReadData::OnButtonAc() 
{
	// TODO: Add your control notification handler code here
		
	int nIndex1 = m_ComboBox_ctrList.GetCurSel();
	int nIndex2 = m_ComboBox_ctrTypeList.GetCurSel();

	CString str1,str2;
	m_ComboBox_ctrList.GetLBText(nIndex1,str1);
	m_ComboBox_ctrTypeList.GetLBText(nIndex2,str2);
	attributelist[str1] = str2;
}



void CDialogReadData::OnOK() 
{
	// TODO: Add extra validation here
	CString str="您有如下属性列未定义变量类型:\n";
	int n=0;
	map<CString,CString>::iterator i;
	for(i = attributelist.begin();i != attributelist.end();i++)
	{
		if(i->second == "")
		{
			n++;
			str+=i->first+"\n";
		}
	}

	if(n)
	{
		AfxMessageBox(str,MB_OKCANCEL);
		return ;
	}
	CString SaveAttribute = m_Edit_SaveAddress+"\\AttributeList.txt",strfirst,strsecond;
	ofstream fout(SaveAttribute.GetBuffer(SaveAttribute.GetLength()),ios::out);
	for(i = attributelist.begin();i != attributelist.end();i++)
	{
		strfirst = i->first;
		strsecond = i->second;
		fout<<strfirst.GetBuffer(strfirst.GetLength())<<" "<<strsecond.GetBuffer(strsecond.GetLength())<<endl;
	}
	
	DataPretreat();
	AfxMessageBox("数据预处理结束!");
	CDialog::OnOK();
}

⌨️ 快捷键说明

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