Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C FizzBuzz!

Name: Anonymous 2013-02-27 23:35


/******************************************************************************
 * 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: Anonymous 2013-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();

        return 0;
}


2/2

Name: Anonymous 2013-02-27 23:43

my suspicions about /prog/ have been confirmed

Name: Anonymous 2013-02-27 23:44

>>3
What suspicions?

Name: Anonymous 2013-02-27 23:45

gnu quality :3

Name: Anonymous 2013-02-28 1:45

It's not ready for inclusion in the GNU project. There has to be a malloc somewhere in there, Unicode support, setjmp/longjmp, and a signal handler.

Name: Anonymous 2013-02-28 10:04

Way too documented  for GNU.

Name: Anonymous 2013-02-28 14:37


char *Fizz = "Fizz";            //Fizz string.  Set to "Fizz!"


Don't do this kind of code, we can see you set it to "fizz.

Name: Anonymous 2013-02-28 14:38

>>8

s/code/comment/

Name: Anonymous 2013-02-28 14:39

>>9,8
Stop mistyping and you won't have to litter the place with substitutions.
Also I'm pretty sure that's the joke.

Name: Anonymous 2013-02-28 15:05


char *Jizz = "Jizz";            //Jizz string.  Set to "Jizz!"

Name: Anonymous 2013-02-28 16:56

>char *Fizz = "Fizz";            //Fizz string.  Set to "Fizz!"

it's set to "fizz", baka, not to "fizz!"

Name: Anonymous 2013-02-28 21:24

>>11

Fizzbuzz -f jizz

Name: ya shoo vant kanesh a car 2013-03-01 4:01

CONIO H
STUDIO H

VOID MAIN

INT

INT

GETCH

GETCH


GETCH


GETCH

GETCH

KBHIT

KBHIT

KBHIT

KBHIT

GETCH

GETCH

GETCH

RETURN ZERO

REM BY COMPUTER SCIENTIST YA SHOO VANT KANESH A CAR
REM

REM

VOID FIZZ BUZZ INT MATRIX

INT MATRIX

RETURN

Name: Anonymous 2013-03-01 4:07

>>6
Are setjmp/longjmp recursive?

Don't change these.
Name: Email:
Entire Thread Thread List