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

📄 tools.h

📁 一个马尔可夫模型的源码
💻 H
字号:
#ifndef TOOLS1
#define TOOLS1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define columnInconsistant -4
#define numberFormError -3
#define strError -2
#define openFileError -1
#define correctAction 0

#define extraSpace 3

/* ------------------- Define of Global Variables ------------------ */
char seps[] = " ,:;\t\r\n";

/* ------------------- Define of Macros ------------------ */
#define SUCCESS(a) if(a == NULL) { printf("\nUnsuccessful Allocation"); exit(1);}

/* ------------------- Define of Functions ------------------ */
int cal_lines(char *inFile);
int getLengthOfLongestLine(char *inFile);
int AllocateDataSpace(double ***pData, int row, int col);

/* ------------------- Implementation of Functions ------------------ */

/* count lines in the input file, according to the number of new line characters in the file */
int cal_lines(char * inFile) {
	FILE *in_fp;
	int lines, ch=-10000;

	if( (in_fp  = fopen( inFile, "r" )) == NULL ) 	{
		printf( "The file '%s' was not opened\n", inFile );
		exit(1);
	}

	lines = 0;
	while( feof( in_fp ) == 0) {
		while(ch != 10 && feof( in_fp ) == 0)
			ch = fgetc( in_fp );
		
		lines++;
		if(ch==10)
			ch = fgetc( in_fp );
	}

	fclose(in_fp);
	return lines;
}

/* go through the file, to find the length of the longest line in the file */
int getLengthOfLongestLine(char *inFile) {
	FILE *in_fp;
	int longestLength, length, ch;

	if( (in_fp  = fopen( inFile, "r" )) == NULL ) 	{
		printf( "The file '%s' was not opened\n", inFile );
		exit(1);
	}

	longestLength = 0;
	while( feof( in_fp ) == 0) {
		/* get the length of the current line */
		length = 0;
		ch = fgetc( in_fp );
		while(ch != 10 && feof( in_fp ) == 0) {
			length++;
			ch = fgetc( in_fp );
		}
		if(longestLength < length) {
			longestLength = length;
		}
	}

	fclose(in_fp);
	return longestLength;
}

/*---------------------------------------------------------------------------*/
int AllocateDataSpace(double ***pData, int row, int col) {
	int i;
	double** Data;
	Data = (double**)malloc(sizeof(double*) * row);
	SUCCESS( Data );
	for(i=0; i < row; i++) {
		Data[ i ] = (double *)malloc(sizeof(double) * col);
		SUCCESS( Data[ i ] );
	}

	(*pData) = Data;
	return correctAction;
}

#endif

⌨️ 快捷键说明

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