/******************************************************************************
* FizzBuzz
*
* Purpose:
* Program for testing division by 3, 5, or 15 and printing the message
* "Fizz", "Buzz", or "FizzBuzz", respectively. If no factoring, prints
* the number.
*
* Usage:
* FizzBuzz [options]
*
* Options:
* -l<number> specifies the starting number(default 1);
* -h<number> specifies the final number (default 100);
* -f<text> division by 3 text (default "Fizz");
* -b<text> division by 5 text (default "Buzz");
* -v verbose. prints out numbers of fizz, buzz and fizzbuzz
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int verbose = 0; //verbose counter set to false(0)
int low = 1; //minimum number. default 1
int high = 100; //maximum number. default 100
char *Fizz = "Fizz"; //Fizz string. Set to "Fizz!"
char *Buzz = "Buzz"; //Buzz string. Set to "Buzz!"
int i; //counter for cycling through sequence
int fizz_count = 0; //counter for Fizz
int buzz_count = 0; //counter for Buzz
int fizzbuzz_count = 0; //counter for FizzBuzz
char *program_name; //name of program (for error messages)
/****************************************************************************
* FizzBuzz -- the actual FizzBuzz function that checks for divsions.
*
* Also, increments the counters for the FinalCount function.
*
* Parameters
* num -- the number to be checked for division
***************************************************************************/
void FizzBuzz(int num)
{
if(num % 15 == 0) //check for 15 factor
{
printf("%s%s\n", Fizz, Buzz);
fizzbuzz_count++;
}
else if(num % 3 == 0) //check for 3 factor
{
printf("%s\n", Fizz);
fizz_count++;
}
else if(num % 5 == 0) //check for 5 factor
{
printf("%s\n", Buzz);
buzz_count++;
}
else //print number in other cases
printf("%i\n", num);
}
1/2
Name:
Anonymous2013-02-27 23:36
/****************************************************************************
* Usage -- Tells the user how to use the program, and then exts.
***************************************************************************/
void usage(void)
{
fprintf(stderr, "Usage is %s [options]\n", program_name);
fprintf(stderr, "Options:\n");
fprintf(stderr, "\t-v verbose. prints out number of fizzes and buzzes\n");
fprintf(stderr, "\t-l<number> specifies a number to begin the FizzBuzz sequence at\n\tThe default is 1.\n");
fprintf(stderr, "\t-h<number> specifies a number to end the FizzBuzz sequence at.\n\tThe default is 100.\n");
fprintf(stderr, "\t-f<text> specifies text for use for numbers divisble by three\n\tThe default is \"Fizz!\"\n");
fprintf(stderr, "\t-b<text> specifies text for use for numbers divisble by five\n\tThe default is \"Buzz!\"\n");
exit(8);
}
/*****************************************************************************
* FinalCount -- if verbose is flagged, prints out the final numbers.
*****************************************************************************/
void FinalCount(void)
{
printf("For the range of %i to %i:\n", low, high);
printf("%s occured %i times.\n", Fizz, fizz_count);
printf("%s occured %i times.\n", Buzz, buzz_count);
printf("%s%s occured %i times.\n", Fizz, Buzz, fizzbuzz_count);
}
int main(int argc, char *argv[])
{
//save the program name for error handling.
program_name = argv[0];
/*
*loop for each option
* Stop if arguments are used up
* or we run into an improper flag
*/
while((argc>1) && (argv[1][0] == '-'))
{
switch(argv[1][1])
{
//verbose switch
case 'v':
verbose = 1;
break;
//low number of sequence
case 'l':
low = atoi(&argv[1][2]);
break;
//high number of sequence
case 'h':
high = atoi(&argv[1][2]);
break;
//Fizz text
case 'f':
Fizz=&argv[1][2];
break;
case 'b':
Buzz=&argv[1][2];
break;
default:
fprintf(stderr, "Bad option %s\n", argv[1]);
usage();
}
//cyle through the arguments
++argv;
--argc;
}
//now that everything is set up, run the actual program!
if(low > high)
{
fprintf(stderr, "Your low value is higher than the high value!\n");
exit(4);
}
for( i = low; i <= high ; i++)
FizzBuzz(i);
//check for the verbose switch
if(verbose)
FinalCount();