📄 hist.c
字号:
#include<stdio.h>#include<math.h>/************************************************Histogram - Takes floating point numbers from stdin and delivers a histogram of integers to stdout. This program is Copyright (C) 2003 byAndrew W. Eckford, and is released into thepublic domain so long as this entire commentis preserved in the source.************************************************//************************************************To compile:cc -lm -o hist hist.cTo use:hist FIRST_BIN INC NUM_BINSwhere FIRST_BIN is the center of the first bin,INC is the difference between bin centers, andNUM_BINS is the number of bins. The program'soutput consists of NUM_BINS integers.Example:cat data.txt | hist 0 1 5produces a histogram of the floating pointnumbers in data.txt with five bins, with bincenters at 0, 1, 2, 3, and 4.************************************************/int main(int argc, char *argv[]) { float start,inc,data_in; int index,bins; int c; long int *data; sscanf(argv[1], "%f", &start); sscanf(argv[2], "%f", &inc); sscanf(argv[3], "%d", &bins); data = (long int *)calloc(bins,sizeof(long int)); for (c = 0; c < bins; c++) { data[c] = 0; } while( fscanf(stdin,"%f",&data_in) != EOF ) { index = (int)rintf((data_in - start)/inc); if (index < 0) { index = 0; } if (index >= bins) { index = bins - 1; } data[index]++; } for (c = 0; c < bins; c++) { fprintf(stdout,"%ld ",data[c]); } free(data);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -