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

📄 lib.c

📁 C语言的朴素贝叶斯分类器代码
💻 C
字号:
/* THIS FILE MAINLY CONTAINS THE CODE FOR READING DATA FROM AN EXTERNAL DATA                       FILE AND GENERATING RANDOM NUMBERS                     */#include "header.h"/*----------------------------- Global variables ----------------------------*/char **DataSet;                                            /* dataset buffer */long ROW, COL;                      /* Number of row and column in a dataset *//*-------------------------Function references-------------------------------*/long GetNumberofAttributes(char *);long GetNumberofTuples(char *);void AllocateDataSpace( long, long );void GetDataIntoBuffer(char *);void DisplayDataBuffer();/*---------------------------------------------------------------------------*//*--------------------Reads data from the file-------------------------------*//*---------------------------------------------------------------------------*/ReadData(char *filename){FILE *fp;                                                  /* checking file existance */  if ((fp = fopen(filename, "r")) == NULL){  	printf ("\n %s not found in this path", filename);	exit(0);  }  fclose( fp );                       /* get number of tuples and attrib  from the data file*/  COL = GetNumberofAttributes( filename );  ROW = GetNumberofTuples( filename );                   /* allocate memory for the data set in the DataSet buffer */  AllocateDataSpace( ROW, COL );                           /* Collect data into DataSet buffer from the file */  GetDataIntoBuffer( filename );}/*---------------------------------------------------------------------------*/long GetNumberofTuples(char *filename){FILE *pfp;char command[STRBUF];  sprintf(command, "cat %s | wc -l", filename);    /* use wc system command  */  pfp = popen(command, "r");	       	           /* open a pipe to read    */  fgets(command, STRBUF, pfp);		           /* get the number of line */  pclose (pfp);  return atol(command);}/*---------------------------------------------------------------------------*/long GetNumberofAttributes(char *filename){long i, field;FILE *fp;char buffer[STRBUF];  fp = fopen(filename, "r");  fgets( buffer, STRBUF, fp);  fclose (fp);  field = 0;  for ( i = strlen(buffer); i; i--){	if ( SEPARATOR(buffer[i]) )  		field ++;  }  return field + 1;                    /* number of field is = separator + 1 */}/*---------------------------------------------------------------------------*/void AllocateDataSpace(long row, long col){ DataSet = (char**)malloc(sizeof(char*) * row); SUCCESS( DataSet ); while(row){        DataSet[ row - 1] = (char *)malloc(sizeof(char) * col);	SUCCESS( DataSet[ row - 1 ] );        row --;  }}/*---------------------------------------------------------------------------*/void GetDataIntoBuffer(char *database){FILE *fp;long tuples, field, i, j;char buffer[STRBUF], tempBuffer[STRBUF];  fp = fopen (database, "r");  for (tuples = 0; tuples < ROW; tuples ++){	fgets( buffer, STRBUF, fp);                     /* collect one tuple */	i = 0;	for ( field = 0; field < COL; field ++ ){    /* collect attrib value */	        strcpy(tempBuffer, buffer);		for ( ; i < strlen (buffer) && ! SEPARATOR(buffer[i]); i++);		tempBuffer[i++] = '\0';		for (j = strlen(tempBuffer)-1; j >= 0 &&			                     ! SEPARATOR(tempBuffer[j]); j--);		DataSet[tuples][field] = (char)atoi(tempBuffer + j + 1); 	}  }  fclose (fp);}/*---------------------------------------------------------------------------*/void FreeDataBuffer(){  free( DataSet );}/*---------------------------------------------------------------------------*/void DisplayDataBuffer(){int i = ROW;int j = COL;  for(i = 0 ; i < ROW; i ++){	printf("%d", DataSet[i][0]);	for(j = 1; j < COL; j ++)		printf(",%d", DataSet[i][j]);	printf("\n");  }}/*---------------------------------------------------------------------------*/

⌨️ 快捷键说明

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