您的位置:首页 > 其它

统计数据分布小工具

2007-06-19 19:13 246 查看
这些天要分析很大的数据,需要统计其P[X>x]的概率分布情况,采用matlab和origin,发现慢的要死,自己写了一个分析工具

缺点:没有数据输入的检查,也就是说,假定从文件中输入的数据是数字串。不过满足一般的使用够了。

实例:resp.txt中有

3480
308
20474
729
375
94
324
1375
1322
94
858
326
335
837
366
811
319
143082
.......

结果:

C:/webme/end>distri_release resp_size.txt 100
The file resp_size.txt was opened
Entering CalcDistribution......
CalcDistribution......
100 0.715434 0.284566
200 0.710231 0.289769
300 0.620970 0.379030
400 0.485702 0.514298
500 0.440679 0.559321
600 0.416528 0.583472
700 0.398978 0.601022
800 0.384246 0.615754
900 0.356537 0.643463
1000 0.336602 0.663398
1100 0.330908 0.669092
1200 0.326110 0.673890
1300 0.306142 0.693858
1400 0.286609 0.713391
1500 0.218431 0.781569
1600 0.215034 0.784966
1700 0.211592 0.788408
1800 0.211457 0.788543
1900 0.211326 0.788674
2000 0.210898 0.789102
2100 0.210560 0.789440
...................




/**//*


统计数据分布情况


*/




/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// 头文件


#include <stdio.h>


#include <assert.h>


#include <malloc.h>






/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// 全局变量






/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// 参数表


int CalcDistribution(FILE *input_stream, const int interval)




...{


// 局部变量


int cur_input_num,nInput = 0,iPos = 0;


double distri = 0.0;




#define MAX_HOLD 2000000


#define INCREMENT 100000




int *hold;


void *alloc_result;


int maxpos = 0,holdpos,nIncre = 0;


int count =0; // 统计前面部分的数据个数总合


// 函数参数分析


assert(interval>0);




// 函数动作




printf( "Entering CalcDistribution...... ");




hold = (int *)malloc(sizeof(int)*MAX_HOLD);


if( hold == NULL )




...{


printf( "Memory alloc error! ");


return -1;


}


memset(hold,0,MAX_HOLD);




// 读取文件中的数字


while(fscanf(input_stream,"%u ",&cur_input_num) != EOF)




...{


nInput++; // 输入数字个数加一


holdpos = cur_input_num/interval;


hold[holdpos]++;


if(holdpos>maxpos) maxpos = holdpos;




// 重新分配内存


if(maxpos>(MAX_HOLD+nIncre*INCREMENT))




...{


nIncre++;




alloc_result = realloc(hold,sizeof(int)*(MAX_HOLD+nIncre*INCREMENT));


if( alloc_result == NULL )




...{


printf( "Memory alloc error! ");


free(hold);


return -1;


}


printf( "Memory realloc success! ");


}


}




printf( "CalcDistribution...... ");




for (iPos = 0;iPos < maxpos;iPos++)




...{


count += hold[iPos]; // 统计前面部分的数据个数总合


distri = (double)count/(double)nInput; // 前面部分的数据个数总合/数据总数 == 概率P[X<x]


printf("%12d %f %f ",(iPos+1)*interval,1-distri,distri);


}




free(hold);


printf( "Leaving CalcDistribution...... ");


// 返回值


return 1;


}




/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






//////////////////////////////////////////////////////////////////////////////


// 入口点


int main(int argc, char* argv[])




...{


// 局部变量


int numclosed;


int interval = 1000;


char *filename=NULL;


static FILE *input_stream;




// 函数参数分析


if(argc<2)




...{


printf("Usage: %s filename [interval] ",argv[0]);


return -1;


}


filename = (char *)strdup(argv[1]);


if(argc == 3) interval = atoi(argv[2]);




// 函数动作


// 打开文件


if( (input_stream = fopen(filename, "rb")) == NULL )




...{


printf( "The file %s was not opened ",filename);


return 0;


}


else


printf( "The file %s was opened ",filename);




// 计算数据分布


CalcDistribution(input_stream,interval);




free(filename);




/**//* All files are closed: */


numclosed = _fcloseall( );


printf( "Number of files closed by _fcloseall: %u ", numclosed );


// 返回值


return 0;


}




/**///////////////////////////////////////////////////////////////////////////////

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: