[Snark] [C] Terrible beers on terrible walls

Jeff Walter submissions at badcode.rocks
Fri Nov 9 01:21:01 UTC 2018


Attached is the C source for a horrendous method to count beers on walls in 
a bar where, I hope, people wipe the lips of every bottle passed to
them with bleach. As an added bonus, it passes the provided test.

== License ==

The following code was written entirely by me (Jeff Walter,
jeff at 404ster.com). It is being distributed under the CC-BY-SA license in the
hope that no one use it, ever. I'm serious. Don't. Just use normal numeric
types.

== bottles.c ==

#include <stdio.h> // For printf()
#include <stdlib.h> // For malloc()

#define BEERS_PER_RUN 99
#define ZERO 0x30

#ifndef NULL
#define NULL (void *)0
#endif

char *BEER_BOTTLES;

int equals (const char *a, const char *b) {
	for (;; a++, b++) {
		if (*a != *b) {
			return (0);
		}
		if (*a == 0) {
			return (1);
		}
	}
}
unsigned int pew (const unsigned int base, unsigned int exponent) {
	unsigned int result;

	for (result = 1; exponent > 0; exponent--) {
		result *= base;
	}

	return (result);
}

int beerRun (unsigned int number) {
	char places;

	for (places = 0; number >= pew (10, places); places++);

	BEER_BOTTLES = malloc (places + 1);
	if (BEER_BOTTLES == NULL) {
		return (-1);
	}
	*(BEER_BOTTLES + places--) = 0;

	for (; places >= 0;) {
		*(BEER_BOTTLES + places--) = ZERO + (number % 10);
		number /= 10;
	}

	return (number);
}

int takeOneDown () {
	char place, places;

	for (place = -1; *(BEER_BOTTLES + place + 1) != 0; place++);
	places = place;

	for (; place >= 0; place--) {
		*(BEER_BOTTLES + place) = *(BEER_BOTTLES + place) - 1;

		if (*(BEER_BOTTLES + place) < ZERO) {
			*(BEER_BOTTLES + place) = ZERO + 9;
		} else if (*(BEER_BOTTLES + place) > ZERO) {
			return (0);
		} else if ((*(BEER_BOTTLES + place) = ZERO) && place) {
			return (0);
		} else if ((*(BEER_BOTTLES + place) = ZERO) && ! place && (place != places)) {
			BEER_BOTTLES++;
			return (0);
		} else if ((*(BEER_BOTTLES + place) = ZERO) && ! place && (place == places)) {
			return (-1);
		}
	}

	return (0);
}

int main (const int argc, const char **argv) {
	if (beerRun (BEERS_PER_RUN) < 0) {
		fprintf (stderr, "Error: Beer-run failed\n");
		return (1);
	}

	BELLYUP:
	if (equals (BEER_BOTTLES, "1")) {
		printf ("%s bottle of beer on the wall, %s bottle of beer.\n", BEER_BOTTLES, BEER_BOTTLES);
	} else {
		printf ("%s bottles of beer on the wall, %s bottles of beer.\n", BEER_BOTTLES, BEER_BOTTLES);
	}
	if (takeOneDown () == 0) {
		if (equals (BEER_BOTTLES, "1")) {
			printf ("Take one down and pass it around, %s bottle of beer on the wall.\n\n", BEER_BOTTLES);
		} else {
			printf ("Take one down and pass it around, %s bottles of beer on the wall.\n\n", BEER_BOTTLES);
		}
		goto BELLYUP;
	} else {
		printf ("Take it down and pass it around, no more bottles of beer on the wall.\n\n");
	}

	printf ("No more bottles of beer on the wall, no more bottles of beer.\n");
	if (beerRun (BEERS_PER_RUN) < 0) {
		fprintf (stderr, "Error: Beer-run failed\n");
		return (1);
	}
	printf ("Go to the store and buy some more, %s bottles of beer on the wall.\n", BEER_BOTTLES);

	return (0);
}


More information about the Snark mailing list