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

📄 source.c

📁 朴素贝叶斯c代码
💻 C
字号:
#include "source.h"Source::Source( char delim[], char *fn){	if(fn== NULL)	{		fname = new char[1024];//		printf("Please enter training file name:");		scanf("%s", fname);#ifdef BDG		cout << fname <<endl;#endif	}	else {		fname = new char[strlen(fn)+1];		strcpy(fname, fn);	}	handle = fopen(fname, "r");	ERROR2(handle, "File %s cannot be opened!", fname);	strcpy(delimiter, delim);	n_Instances    = countInstances();	n_Descriptions = countDescriptions();	buffer = NULL;}void Source::init(){	fseek (handle, 0L, SEEK_SET);}Description Source::readDescription(){	if(buffer==NULL) {		where = buffer = new char[MAXLEN];		fgets(buffer, 2048, handle);	}	if(where == NULL) {		fgets(buffer, 2048, handle);		where = buffer;	}	if(where) {		char *curr = where;		where = strstr(where,delimiter);		if(where) *where = 0;//		cout << curr<<(where!=NULL)?(*where):"";		Description val = atof(curr);//		cout << val <<"\n";		if(where) where++; // to next value		return val;	}}int Source::countInstances(){	n_Instances = 0;	char tmp[MAXLEN];	long int pos = ftell(handle);	init();	while(!feof(handle)) {		if(fgets(tmp, MAXLEN, handle) != NULL)			n_Instances++;	}	fseek (handle, pos, SEEK_SET);//	n_Instances --;//	cout << "Instance :"<<n_Instances;	return n_Instances;}int Source::countDescriptions(){	n_Descriptions = 0;	char tmp[MAXLEN];	long int pos = ftell(handle);	init();	if(fgets(tmp, MAXLEN, handle) != NULL) { 		n_Descriptions++;		char *pstr = tmp;    while((pstr = strstr(pstr, delimiter)) != NULL)		{			pstr++;			n_Descriptions++;		}	}	fseek (handle, pos, SEEK_SET);#ifdef DBG2	cout << "Source::countDescriptions: "<<n_Descriptions<<endl;#endif	return n_Descriptions;}Description *Source::readOneInstance(){	char tmp[MAXLEN];	Description *oneInst;	oneInst = new Description [n_Descriptions+1];	for(int i=0; i<n_Descriptions; i++)	{		oneInst[i] = readDescription();#ifdef DBG2		cout << oneInst[i] <<",";#endif	}#ifdef DBG2	cout <<endl;#endif	return oneInst;}

⌨️ 快捷键说明

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