home *** CD-ROM | disk | FTP | other *** search
/ Explore the World of Soft…e: Engineering & Science / Explore_the_World_of_Software_Engineering_and_Science_HRS_Software_1998.iso / programs / statistc / ochy9309.exe / OCHYPG.CXX next >
Text File  |  1993-09-15  |  11KB  |  300 lines

  1. /*
  2.  *                 OC Curves for Hypergeometric Sampling
  3.  *
  4.  *                   A Quality Assurance Training Tool:
  5.  *            Statistics Committee of the QA Section of the PMA
  6.  *
  7.  *               Bob Obenchain, CompuServe User [72007,467]
  8.  *
  9.  *                     Usage: DOSprompt>  ochypg
  10.  *
  11.  *      Parameter settings are either...
  12.  *             specified interactively in response to prompts, or
  13.  *             redirected to come from a batch input file.
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <signal.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <process.h>
  21. #include <math.h>
  22.  
  23. #define abs(a)      (( (a) >= 0 ) ? (a) : (-a))  /* absolute value of a */
  24. #define sgn(a)      (( (a) >= 0 ) ?  1  :  -1 )  /* numerical sign of a */
  25. #define min(a,b)    (( (a) <= (b) ) ?  (a) : (b) )
  26.  
  27. /* externals */
  28.  
  29. FILE    *inpfile, *csvfile;
  30. char    buf[256], inpnam[13], outnam[13];
  31. double  alpha, fraction, term, prob, pmax;
  32. long    xn, xmin, xmax, xold, ymax;
  33. long    sn, nc, pn;
  34. int     yesno;
  35.  
  36. /* prototypes */
  37.  
  38. double  gammln( double xx );
  39. char *copyright="Copyright 1987, 1988 Numerical Recipes Software";
  40.  
  41. void   main( void );
  42. void   handler( int sig );
  43. double hypgprob( long from, long bad, long take, long find );
  44.  
  45. void handler( int sig ) {     /* Interrupt Handler */
  46.       char c;        /* key scan code,,char */
  47.       
  48.       signal( SIGINT, SIG_IGN );     /* ignore further signals */
  49.       printf("\n\n...Enter a Q to Quit, anything else to Continue...\n\n");
  50.       gets( buf );
  51.       sscanf( buf, "%c", &c );
  52.       if( c == 'Q' || c == 'q' ) {
  53.            exit( 1 );
  54.            }
  55.       /* else...normal return, to continue processing... */
  56.       signal( SIGINT, handler );
  57.       }
  58.  
  59. /*
  60.  *  gammln() function is the logarithm of gamma function of Press, et.al.
  61.  *  (1988). "Numerical Recipes in C," Cambridge University Press.
  62.  */
  63.  
  64. double hypgprob( from, bad, take, find )     /* Hypergeometric Probability */
  65. long from, bad, take, find; {
  66.  
  67.       double hpl;
  68.  
  69.       if( from < 0 || bad < 0 || take < 0 || find < 0 || from < bad )
  70.              return( -1.0 );       /* error */
  71.  
  72.       if( find > take || from < take || bad < find ||
  73.           from - bad < take - find )
  74.              return( 0.0 );       /* impossible */
  75.  
  76.       if( take == from ) {
  77.           if( bad == find )
  78.              return( 1.0 );       /* certain */
  79.           else
  80.              return( 0.0 );       /* impossible */
  81.           }
  82.  
  83.       if( from == bad ) {
  84.           if( take == find )
  85.              return( 1.0 );       /* certain */
  86.           else
  87.              return( 0.0 );       /* impossible */
  88.           }
  89.  
  90.       if( bad == 0 ) {
  91.           if( find = 0 )
  92.              return( 1.0 );       /* certain */
  93.           else
  94.              return( 0.0 );       /* impossible */
  95.           }
  96.  
  97.       hpl =  gammln( (double)( bad + 1 ) )
  98.            - gammln( (double)( find + 1 ) )
  99.            - gammln( (double)( bad - find + 1 ) )
  100.            + gammln( (double)( take + 1 ) )
  101.            - gammln( (double)( from + 1 ) )
  102.            + gammln( (double)( from - take + 1 ) )
  103.            - gammln( (double)( take - find + 1 ) )
  104.            + gammln( (double)( from - bad + 1 ) )
  105.            - gammln( (double)( from - bad - take + find + 1 ) );
  106.       return( exp( hpl ) );
  107.       }
  108.  
  109. void main() {
  110.  
  111.       char kb;
  112.       long x;
  113.  
  114.       if( signal( SIGINT, handler ) == SIG_ERR ) {
  115.              printf("\n\n\tOCHypg: Couldn't set SIGINT...Abort!\n\n");
  116.              exit( 1 );
  117.              }
  118.       
  119.       printf("\n\n     ***    Operating Characteristic Curves for     ***" );
  120.       printf(  "\n                   Hypergeometric Sampling" );
  121.       printf("\n\n                   OCHYPRG....Version 9308");
  122.       printf("\n\n                A Quality Assurance Training Tool:");
  123.       printf(  "\n         Statistics Committee of the QA Section of the PMA");
  124.       printf("\n\n            Bob Obenchain, CompuServe User [72007,467]\n\n" );
  125.       
  126.       printf(      "\tWill Parameter Settings be Input via K = Keyboard ? ...or" );
  127.       printf(    "\n\t                                     B = Batch File ?" );
  128.       printf(  "\n\n\tPress the   K   or   B   key now --> " );
  129.       while( 1 ) {
  130.               gets( buf );
  131.               sscanf( buf, "%c", &kb );
  132.               if( kb == 'k' )
  133.                   kb  = 'K';
  134.               if( kb == 'b' )
  135.                   kb  = 'B';
  136.               if( kb == 'K' || kb == 'B' )
  137.                   break;
  138.               printf( "\r\tPress either K  or  B  ...Try Again --> " );
  139.               }
  140.  
  141.       strcpy( outnam, "ochypg" );
  142.       strcpy( inpnam, "ochypg" );
  143.  
  144.       if( kb == 'B' )
  145.           printf( "\n\n\tBatch Input of Parameter Settings Selected..." );
  146.       else
  147.           printf( "\n\n\tKeyboard Input of Parameter Settings Selected..." );
  148.       
  149.       printf( "\n\n\tAt colon Prompts : ...simply press ENTER to get the [default]." );
  150.  
  151.       if( kb == 'B' ) {
  152.           printf(  "\n\n\tSpecify filename of Batch Input File [%s] : ", inpnam );
  153.           gets( buf );
  154.           if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
  155.               sscanf( buf, "%s", inpnam );
  156.           inpnam[ min( 8, (int)strcspn( inpnam, ". " ) ) ] = '\0';
  157.           if( inpnam[0] == '\0' )
  158.               strcpy( inpnam, "ochypg" );
  159.           strcpy( outnam, inpnam );
  160.           strcat( inpnam, ".inp" );
  161.           printf( "\n\tThe Batch Input file is to be: %s\n", inpnam );
  162.           if( ( inpfile = fopen( inpnam, "r" ) ) == NULL ) {
  163.                   printf(  "\tCannot read Batch Input file: %s\n", inpnam );
  164.                   printf(  "\t...using Keyboard Input from standard Infile, stdin.\n" );
  165.                   kb = 'K';
  166.                   }
  167.           }
  168.       
  169.       if( kb == 'K' )
  170.           inpfile = stdin;
  171.  
  172.       printf(  "\n\n\tSpecify filename to save primary OCHypg Output [%s] : ",
  173.           outnam );
  174.       fgets( buf, 80, inpfile );
  175.       if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
  176.           sscanf( buf, "%s", outnam );
  177.       outnam[ min( 8, (int)strcspn( outnam, ". " ) ) ] = '\0';
  178.       if( outnam[0] == '\0' )
  179.           strcpy( outnam, "ochypg" );
  180.       strcat( outnam, ".csv" );
  181.       printf(  "\n\tThe OCHypg primary Output file is to be: %s\n",
  182.               outnam );
  183.       if( ( csvfile = fopen( outnam, "w" ) ) == NULL ) {
  184.           printf(  "\tCannot write to Output filename : %s\n", outnam );
  185.           if( ( csvfile = fopen( "ochypg.csv", "w" ) ) == NULL )
  186.                   printf(  "\tCannot write to Output filename : ochypg.csv\n" );
  187.           else {
  188.                   printf(  "\t...using default Outfile name : ochypg.csv\n" );
  189.                   strcpy( outnam, "ochypg.csv" );
  190.                   }
  191.           }
  192.  
  193.       printf("\n\nOCHypg uses the hypergeometric distribution to calculate values on" );
  194.       printf("\nthe Operating Characteristics curve for sampling without replacement" );
  195.       printf("\nfrom a finite population of 2 million or fewer items.");
  196.       
  197.       pn = 500;
  198.       sn = 100;
  199.       nc =   0;
  200.       fraction = 0.05;
  201.  
  202.       while( 1 ) {
  203.  
  204.           printf("\n\nWhat is the Population Size? [N=%ld] : ", pn );
  205.           fgets( buf, 80, inpfile );
  206.           if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
  207.               sscanf( buf, "%ld", &pn );
  208.           if( pn < 2 )
  209.               pn = 2;
  210.           if( pn > 2000000000 )
  211.               pn = 2000000000;
  212.           printf("\n\tPopulation Size: N = %ld", pn );
  213.           if( csvfile != NULL )
  214.               fprintf( csvfile, "\n\nPopulation Size, N = %ld,,", pn );
  215.  
  216.           printf("\n\nWhat is the Sample Size ? [n=%ld] : ", sn );
  217.           fgets( buf, 80, inpfile );
  218.           if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
  219.               sscanf( buf, "%ld", &sn );
  220.           if( sn < 1 )
  221.               sn = 1;
  222.           if( sn > pn - 1 )
  223.               sn = pn - 1;
  224.           printf("\n\tSample Size: n = %ld", sn);
  225.           if( csvfile != NULL )
  226.               fprintf( csvfile, "\nSample Size, n = %ld,,", sn);
  227.  
  228.           printf("\n\nWhat is the Acceptance Number ? [c=%ld] : ", nc );
  229.           fgets( buf, 80, inpfile );
  230.           if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
  231.               sscanf( buf, "%ld", &nc );
  232.           if( nc >= sn )
  233.               nc = sn - 1;
  234.           if( nc < 0 )
  235.               nc = 0;
  236.           printf("\n\tAcceptance Number: c = %ld", nc );
  237.           if( csvfile != NULL ) {
  238.               fprintf( csvfile, "\nAcceptance Number, c = %ld,,", nc );
  239.               fprintf( csvfile, "\n\nFraction Nonconforming,Probability of Acceptance,Nonconforms,Population Size" );
  240.               }
  241.  
  242.           while( 1 ) {
  243.  
  244.               printf("\n\nTrue Fraction Nonconforming ? [p=%7.5lf] : ",
  245.                   fraction );
  246.               fgets( buf, 80, inpfile );
  247.               if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
  248.                   sscanf( buf, "%lf", &fraction );
  249.               if( fraction > 1.0 )
  250.                   fraction = 1.0;
  251.               if( fraction < 0.0 )
  252.                   fraction = 0.0;
  253.  
  254.               xn = (long)( fraction * (double)pn + 0.5);
  255.               if( xn < (long)1 )
  256.                   xn = (long)1;
  257.               if( xn == pn )
  258.                   xn = pn - (long)1;
  259.               fraction = (double)xn / (double)pn;
  260.               printf("\n\tNumber of Nonconforming Items in Population = %ld ", xn );
  261.               printf("\n\tTrue Fraction Nonconforming: p=%7.5lf ", fraction );
  262.               prob = 0.0;
  263.               for( x = 0; x <= nc; x++ ) {
  264.                   term = hypgprob( pn, xn, sn, x );
  265.                   if( term < 0.0 || term > 1.0 )
  266.                       break;        /* error */
  267.                   prob += term;
  268.                   }
  269.               printf( "\n\tProbability of Acceptance = %7.5lf.", prob );
  270.  
  271.               if( csvfile != NULL )
  272.                   fprintf( csvfile, "\n%7.5lf,%7.5lf,%ld,%ld", fraction, prob, xn, pn );
  273.  
  274.               yesno = 'y';
  275.               printf("\n\nAdditional values of Incoming Fraction Defective [Y|n] ? : ");
  276.               if( kb == 'B' )
  277.                   fgets( buf, 80, inpfile );
  278.               else
  279.                   gets( buf );
  280.               if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
  281.                   sscanf( buf, "%c", &yesno );
  282.               if( yesno == 'n' || yesno == 'N' )
  283.                   break;
  284.               }
  285.  
  286.           yesno = 'y';
  287.           printf("\n\nAdditional values of Sample Size and/or Accept Number [Y|n] ? : ");
  288.           if( kb == 'B' )
  289.               fgets( buf, 80, inpfile );
  290.           else
  291.               gets( buf );
  292.           if( buf[0] != '\0' && buf[0] != '\r' && buf[0] != '\n' )
  293.               sscanf( buf, "%c", &yesno );
  294.           if( yesno == 'n' || yesno == 'N' )
  295.               break;
  296.           }
  297.  
  298.       printf( "\n\nExiting OCHypg...\n\n" );
  299.       }
  300.