# To unbundle, sh this file echo Makefile 1>&2 cat >Makefile <<'End of Makefile' O_FILES = monty.o usage.o help.o CFLAGS = CC = gcc monty: $(O_FILES) $(CC) $(CFLAGS) -o monty $(O_FILES) End of Makefile echo help.c 1>&2 cat >help.c <<'End of help.c' /* * help - give program help */ #include help(argv) char** argv; { fprintf(stderr, "%s: HELP:\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, " monty - template filter\n"); fprintf(stderr, "\n"); fprintf(stderr, " >>> This is where to put some friendly help on the why and wherefore of\n"); fprintf(stderr, " this program. For example:\n"); fprintf(stderr, "\n"); fprintf(stderr, " The program parses its command line flags to read a Boolean, integer\n"); fprintf(stderr, " and float into global parameter variables. If the Debug flag is\n"); fprintf(stderr, " selected it writes the values of its parameters to standard error.\n"); fprintf(stderr, "\n"); fprintf(stderr, " It then reads a floating point number and an integer, both in\n"); fprintf(stderr, " ASCII text, from standard input and then writes them to standard output,\n"); fprintf(stderr, " as text preceded by variable names.\n"); fprintf(stderr, "\n"); } /* end of help */ End of help.c echo monty.c 1>&2 cat >monty.c <<'End of monty.c' /** ** monty - monty hall problem simulation ** ** CREATED: 2009.09.10 copied from t_filter ** **/ #include "monty.h" main(argc, argv) int argc; char** argv; { /* declare functions */ double atof(); /* declare local variables */ int n, m; int choice; int prize; int monty; int wins = 0; /* initialize global variables (including parameters) */ defaultParms(); parseArgs(argc, argv); /* initialize local variables */ /* compute & write */ for (n = 0; n < NumRuns; n++) { prize = rpick(NumDoors); choice = rpick(NumDoors); if (Switch) { if (choice == prize) { // if we already have the prize chosen we will switch it away do { monty = rpick(NumDoors); // this is the choice Monty offers } while (monty == choice); // do until monty != choice choice = monty; } else { // if we don't have the prize chosen we will switch to it choice = prize; } } // end if (Switch) if (choice == prize) wins++; } printf("you won %d out of %d runs, for an avearge of %f\n", wins, NumRuns, (double)wins/(double)NumRuns); } /* end main */ /* * defaultParms - initialize global variables */ int rpick(int choices) { return 1 + (int)((double)random()*(double)choices/(double)MAXRAND); } /* * defaultParms - initialize global variables */ defaultParms() { Debug = FALSE; NumDoors = 3; NumRuns = 1000; Switch = TRUE; } /* end defaultParms() */ /* * parseArgs - parse arguments into global variables */ parseArgs(argc, argv) int argc; char** argv; { int i; for (i = 0; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'D' : Debug = TRUE; break; case 'h' : help(argv); exit(0); break; case 'n' : NumRuns = atoi(&argv[++i][0]); break; case 'd' : NumDoors = atoi(&argv[++i][0]); break; case 'S' : Switch = TRUE; break; case 'u' : usage(argv); exit(0); break; default: fprintf(stderr, "%s: ERROR: illegal flag: %s\n", argv[0], argv[i]); usage(argv); exit(0); break; } /* end switch */ } /* end if */ } /* end for(i) */ } /* end parseArgs() */ End of monty.c echo monty.h 1>&2 cat >monty.h <<'End of monty.h' /* * t_filter.h -- include file for t_filter.c */ /* includes */ #include #include #include /* constants */ #define TRUE (1) #define FALSE (0) #define MAXRAND (2147483647) /* global variables */ /* flag-settable parameters (global variables) */ int Debug; /* Boolean: TRUE = debug writes */ int Switch; /* Boolean: TRUE = switch doors */ int NumDoors; int NumRuns; End of monty.h echo usage.c 1>&2 cat >usage.c <<'End of usage.c' /* * usage - give program usage */ #include usage(argv) char** argv; { fprintf(stderr, "%s: USAGE: %s [flags] < infile > outfile\n", argv[0], argv[0]); fprintf(stderr, "\n"); fprintf(stderr, " flag meaning default\n"); fprintf(stderr, " ------------- ---------------------- -------\n"); fprintf(stderr, " -D debug FALSE \n"); fprintf(stderr, " -h help on this program \n"); fprintf(stderr, " -d NumDoors 3 \n"); fprintf(stderr, " -n NumRuns 1000 \n"); fprintf(stderr, " -S switch doors always FALSE \n"); fprintf(stderr, " -u usage (this message) \n"); fprintf(stderr, "\n"); } /* end of usage */ End of usage.c