home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_40.arc / C-REVIEW.ARC / GRIND.C < prev    next >
C/C++ Source or Header  |  1987-09-23  |  4KB  |  173 lines

  1. /*
  2.      Program: Grind
  3.  
  4.      Version: 1.00
  5.      Date:    September 19, 1987
  6.  
  7.      Language: Generic ANSI C
  8.  
  9.      Tests all aspects of a C compilers functions, including disk i/o,
  10.      screen i/o, floating point, recursion, prototyping, and memory
  11.      allocation. It should be a large enough program to test the advanced
  12.      optimizers in some compilers.
  13.  
  14.      Copyright 1987 Scott Robert Ladd. All Rights Reserved.
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <math.h>
  19.  
  20. #define MAXFLOATS 1000
  21.  
  22. struct tabent
  23.      {
  24.      double val, vsum, vsqr, vcalc;
  25.      };
  26.  
  27. struct tabent table[MAXFLOATS];
  28.  
  29. char *errmsg[] = {
  30.                 "GRIND.TBL cannot be created",
  31.                 "GRIND.TBL cannot be closed properly",
  32.                 "GRIND.IN cannot be found",
  33.                 "GRIND.IN has been truncated",
  34.                 "GRIND.IN floating point format wrong",
  35.                 "GRIND.IN cannot be closed properly"
  36.                 };
  37.  
  38. /* function prototypes */
  39. void readfloats(void);
  40. void sortfloats(void);
  41. void quicksort(struct tabent *,int,int);
  42. void maketable(void);
  43. void writetable(FILE *);
  44. void error(int);
  45. void exit(int);
  46. double atof(char *);
  47.  
  48. main()
  49.      {
  50.      FILE *tblout;
  51.  
  52.      puts("\nGrind  v1.00  -- A Benchmark Program");
  53.      puts("Written by Scott Robert Ladd for Micro Cornucopia C compiler review.");
  54.      puts("Released: 09/19/87\n");
  55.  
  56.      readfloats();
  57.  
  58.      sortfloats();
  59.  
  60.      maketable();
  61.  
  62.      if (NULL == (tblout = fopen("GRIND.TBL","w+")))
  63.           error(1);
  64.      writetable(tblout);
  65.      if (fclose(tblout))
  66.           error(2);
  67.  
  68.      puts("\7\7\7End run GRIND!!!");
  69.  
  70.      return(0);
  71.      }
  72.  
  73. void readfloats()
  74.      {
  75.      register int i;
  76.      char buf[12];
  77.      FILE *fltsin;
  78.  
  79.      printf("--> Reading in floats. At #     ");
  80.      if (NULL == (fltsin = fopen("GRIND.IN","r")))
  81.           error(3);
  82.  
  83.      for (i = 0; i < MAXFLOATS; ++i)
  84.           {
  85.           printf("\b\b\b\b\b%5d",i);
  86.           if (NULL == fgets(buf,12,fltsin))
  87.                error(4);
  88.           table[i].val = atof(buf);
  89.           }
  90.  
  91.      if (fclose(fltsin))
  92.           error(6);
  93.  
  94.      printf("\n");
  95.      }
  96.  
  97. void sortfloats()
  98.      {
  99.      puts("--> Sorting data");
  100.      quicksort(table,0,MAXFLOATS-1);
  101.      }
  102.  
  103. void quicksort(item,left,right)
  104. struct tabent *item;
  105. int left, right;
  106.      {
  107.      register int i, j;
  108.      struct tabent x, y;
  109.  
  110.      i = left;
  111.      j = right;
  112.      x = item[(i+j)/2];
  113.  
  114.      do
  115.           {
  116.           while (item[i].val < x.val && i < right) i++;
  117.           while (x.val < item[j].val && j > left)  j--;
  118.           if (i <= j)
  119.                {
  120.                y = item[i];
  121.                item[i] = item[j];
  122.                item[j] = y;
  123.                i++;
  124.                j--;
  125.                }
  126.           }
  127.      while (i <= j);
  128.  
  129.      if (left < j)  quicksort(item,left,j);
  130.      if (i < right) quicksort(item,i,right);
  131.      }
  132.  
  133. void maketable()
  134.      {
  135.      register int i;
  136.      double sum = 0;
  137.  
  138.      puts("--> Calculating table values");
  139.  
  140.      for (i = 0; i < MAXFLOATS; ++i)
  141.           {
  142.           sum = sum + table[i].val;
  143.           table[i].vsum  = sum;
  144.           table[i].vsqr  = table[i].val * table[i].val;
  145.           table[i].vcalc = sqrt(fabs(table[i].val)) * log10(fabs(table[i].val));
  146.           }
  147.      }
  148.  
  149. void writetable(fd)
  150. FILE *fd;
  151.      {
  152.      register int i;
  153.  
  154.      puts("--> Writing Table to File");
  155.  
  156.      for (i = 0; i < MAXFLOATS; i = i + 10)
  157.           {
  158.           fprintf(fd,
  159.                   "val = %5.2f, sum = %5.2f, sqr = %5.2f, calc = %5.2f\n",
  160.                   table[i].val,
  161.                   table[i].vsum,
  162.                   table[i].vsqr,
  163.                   table[i].vcalc);
  164.           }
  165.      }
  166.  
  167. void error(errno)
  168. int errno;
  169.      {
  170.      printf("\n\7ERROR: %s\7",errmsg[errno-1]);
  171.      exit(errno);
  172.      }
  173.