lucky.c
/* File: lucky.c * Author: Margot (mmckin1@umbc.edu) * Date: 20 February 2001 * SSN: None of Your Business! * Section: Mine * * This file contains the function definitions for the following * functions: * GetValidBDay() * InValidDate() * SeedRand() * PrintNRandNums() * PrintGreeting() * These functions are generic functions which can be used to * do many things. See their descriptions below. * * This may or may not conform to your class coding standards. * See the main course webpage to be sure what those * standards are. */ #include <stdio.h> #include "lucky.h" /* Function: PrintGreeting() * * This function prints a little synopsis of the program * out to the screen for the user. */ void PrintGreeting (void) { printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); printf("A recent discovery has been made connecting your birthday to\n"); printf("your winning Lottery numbers. Here is a program to get your\n"); printf("personal lucky numbers. Enter in your Birthday as a 6 digit\n"); printf("number (this program assumes that you can handle this in an\n"); printf("error-free style). Enter 0 will quit the program.\n"); printf("\n\t\t\tHave fun!!!\n\n"); printf("WARNING: You may get repeats! Other people have to have\n"); printf(" some numbers to be lucky with too!!\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); return; } /* Function: GetValidBDay * * GetValidBDay gets a (hopefully) 6 digit number from * the user. This function calls InValidDate to be sure * that the date the user entered was correct and gets * a new number from them otherwise. */ int GetValidBDay (void) { int date; printf("\nPlease enter your Birthday (mmddyy) or 0 to Quit: "); scanf("%d", &date); while( date != 0 && InValidBDay(date) ) { printf("You don't know your own Birth Date?!?!?!?!?!\n"); printf("\nPlease enter your Birthday (mmddyy) or 0 to Quit: "); scanf("%d", &date); } return(date); } /* Function: InValidBDay * * InValidBDay is a predicate function which returns * true (1) if the date is not within the specified * limits and false (0) otherwise. */ int InValidBDay (int date) { int month, day; /*Strips the year off... they are all valid. */ date /= 100; /* Take advantage of truncation in integer division to * get the first two digits of the total date which will * represent the month. */ month = date / 100; if( month < 1 || month > 12 ) { return 1; } /* Mod the date by 100 to strip out the last two digits * is th remaining date which represent the day. check * to be sure the day is within the proper scale of days * available per month. */ day = date % 100; if( day <= 0 ) { return 1; } switch(month) { case 1: case 3: case 5: case 7: case 9: case 10: case 12: if( day > 31 ) { return 1; } case 8: case 4: case 6: case 11: if( day > 30 ) { return 1; } case 2: /* We are assuming that February */ if( day > 28 ) { /* is not affected by leap years */ return 1; /* I write it; I can do that. */ } default: return 0; } } /* Function: SeedRand * * This function seeds the random number generator with * the integer passed and returns nothing. */ void SeedRand (int seed) { srand(seed); return; } /* Function: PrintNRandNums * * This function prints the n (being the arguement passed) * random numbers to the screen in 5 beautiful columns. * If the user has chosen for either 0 or a negative * number of integers to be printed they are publicly * humiliated. */ void PrintNRandNums (int n) { int i, curr; if( n <= 0 ) { printf("\nNo Lucky Numbers for You!!!\n"); return; } for( i = 0; i < n; i++ ) { /* Print a new line after 5 entries have been printed * creating 5 columns. */ if( i % 5 == 0 ) { printf("\n"); } curr = rand() % 100; printf("\t%d", curr); } printf("\n"); return; }