www.pudn.com > eval-1.2.zip > hist.c



#include 
#include 
#include 
#include 

#include "read.h"
#include "stat.h"
#include "def.h"

int main(int cn, char **cl)
{
  FILE *f;
  void *tmp;
  double min = 0, max = 0, d, *D = 0;
  unsigned i, iv = 100, line = 0, l = 0, num = 0;
  char buf[0xFF], *e;

  if (cn > 5 || cn < 2) {
    puts("usage: hist  [min] [max] [iv]");
    puts(" [in]     input file (- for stdin)");
    puts(" [min]    lower interval bound");
    puts(" [max]    higher interval bound");
    puts(" [iv]     number of intervals");
    return 0;
  }

  if (cn > 2) min = strtod(cl[2], 0);
  if (cn > 3) max = strtod(cl[3], 0);
  if (cn > 4) if (!(iv = strtoul(cl[4], 0, 10))) iv = 100;

  if (!strcmp(cl[1], "-")) f = stdin;
  else
    if ((f = fopen(cl[1], "r")) == 0) {
      fprintf(stderr, "Could not open %s\n", cl[1]);
      return EXIT_FAILURE;
    }

  while (line++, fgets(buf, sizeof buf, f) && !feof(f) && !ferror(f)) {
    if (!(d = strtod(buf, &e)) && e == buf) {
      fprintf(stderr, "malformed input (%s, %u)\n", cl[1], line);
      continue;
    }

    if (l >= num) {
      num += 16384;
      if ((tmp = realloc(D, num * sizeof *D)) == 0) {
        fprintf(stderr, "realloc error\n");
        return 0;
      }
      D = tmp;
    }

    D[l++] = d;
  }

  if (!min && l) {
    min = D[0];
    for (i=1; i max) max = D[i];
  }

  Hist(D, l, min, max * 1.01, iv);

  return 0;
}