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

📄 globals.cpp

📁 LCL分群法 改善cure kmean.......等
💻 CPP
字号:
#include "stdafx.h"
#include "Globals.h"
#include "CCluster.h"
#include "CRecord.h"

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

/*
token = ","                ","
this  = "0,1,2,3"          " 0,,,,,,,1,2"
array = "0","1","2","3"    " 0","1","2"   
*/
short split(CString& str, CStringArray &array, LPCTSTR token, short trim)
{
	char *ptr;
   short len= str.GetLength();
	char *p0 = new char[len+1];
	p0[0] = 0;
   char *p  = p0;
   CString s;
   strcpy(p,str);
   BOOL bFirst = TRUE;
   
   array.RemoveAll();
   ptr = strtok(p,token);
   while(ptr != NULL) {
      s = ptr;
      if(trim == Split_$TrimRight) {
         s.TrimRight(" ");
      } else if(trim == Split_$TrimLeft) {
         s.TrimLeft(" ");
      } else if(trim == Split_$TrimBoth) {
         s.TrimRight(" ");
         s.TrimLeft(" ");
      }
      array.Add(s);
      ptr = strtok(NULL,token);
   }
   delete []p0;
   return (short)array.GetSize();
}

BOOL read_data(const char *filename,CPtrArray &data)
{
	CStdioFile hFile;
	if (hFile.Open(filename,CFile::modeRead) == 0){
		return FALSE;
	}
	CString sBuf;
	CStringArray array;
	long size;
	while(hFile.ReadString(sBuf)){
		split(sBuf,array," ,\t:",Split_$TrimNot);
		size = (long)array.GetSize();
		if(size == 0) {continue;}
		long dim = size - 1;
		CRecord *record = new CRecord(array[0],NULL,dim);
		float *vector = record->get_vector();
		for(long i=1;i<size;i++,vector++){
			*vector = (float)atof(array[i]);
		}
		array.RemoveAll();
		data.Add(record);
	}
	hFile.Close();
	return TRUE;
}

BOOL write_data(const char *filename,CPtrArray &array)
{
	CStdioFile hFile;
	if(!hFile.Open(filename,CFile::modeCreate | CFile::modeWrite | CFile::typeText)){
		return FALSE;
	}
	CString str,s;
	for(long i=0;i<array.GetSize();i++){
		CCluster *cluster = (CCluster *)array[i];
		CPtrArray &a = cluster->get_record_array();
		for(long j=0;j<a.GetSize();j++){
			str.Format("%s\t",cluster->get_label());
			CRecord *record = (CRecord *)a[j];
			s.Format("%s\t",record->get_label());
			str += s;
			float *vector = record->get_vector();
			for(long k=0;k<record->get_dim();k++){
				s.Format("%f\t",vector[k]);
				str += s;
			}
			str += "\n";
			hFile.WriteString(str);
		}
	}
	hFile.Close();
	return TRUE;
}

⌨️ 快捷键说明

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