//===================================================== file = count1.c ===== //= Program to count the number of events in an interval of time = //=========================================================================== //= Notes: = //= 1) Input from input file "in.dat" to stdin (see example below) = //= * Input is two columns of reals with no commas = //= * No comments are allowed in the input file = //= 2) The time interval INTERVAL_TIME is in the #define section = //= 3) Output is to stdout = //=-------------------------------------------------------------------------= //= Example "in.dat" file: = //= = //= 0.01 6 = //= 0.05 3 = //= 0.05 4 = //= 0.03 2 = //= 0.02 1 = //= 0.04 4 = //= 0.06 5 = //= 0.03 4 = //= 0.12 3 = //= 0.04 2 = //=-------------------------------------------------------------------------= //= Example output (for above "in.dat" and INTERVAL_TIME = 0.1) = //= = //= & ------------------------------------------- count1.c ----- & = //= 9 = //= 11 = //= 9 = //= 0 = //= 5 = //= & ---------------------------------------------------------- & = //=-------------------------------------------------------------------------= //= Build: bcc32 count1.c = //=-------------------------------------------------------------------------= //= Execute: count1 < in.dat = //=-------------------------------------------------------------------------= //= Author: Kenneth J. Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (06/07/02) - Genesis = //= KJC (05/30/13) - Modified for integer output = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() and feof() #include // Needed for exit() and atof() #include // Needed for strcmp() //----- Defines ------------------------------------------------------------- #define INTERVAL_TIME 0.10 // Time interval for counting //=========================================================================== //= Main program = //=========================================================================== void main(void) { double time_stamp; // Delta timestamp value of a tuple double sum_time; // Time sum for this interval int num_bytes; // Number of bytes of a tuple int sum_bytes; // Bytes sum for this interval char instring1[1024]; // Input string #1 char instring2[1024]; // Input string #2 // Output a banner printf("& ------------------------------------------- count1.c ----- & \n"); // Compute and output bytes counts sum_bytes = sum_time = 0.0; while(1) { // Get time_stamp and num_butes scanf("%s %s \n", instring1, instring2); time_stamp = atof(instring1); num_bytes = atof(instring2); // Compute bytes sums for time interval sum_time = sum_time + time_stamp; if (sum_time <= INTERVAL_TIME) { sum_bytes = sum_bytes + num_bytes; if (feof(stdin)) { printf("%d \n", sum_bytes); break; } } else { printf("%d \n", sum_bytes); sum_bytes = num_bytes; sum_time = sum_time - INTERVAL_TIME; while (sum_time > INTERVAL_TIME) { sum_time = sum_time - INTERVAL_TIME; printf("0 \n"); } if (feof(stdin)) break; } } // Output closing message printf("& ---------------------------------------------------------- & \n"); }