Advertisement
aaaaaa123456789

Fizzbuzz without constants

Jan 5th, 2015
5,210
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int one, two, three, five, ten, onehundred;
  6. int digit_base;
  7. char * fizz;
  8. char * buzz;
  9. char * fizzbuzz;
  10. char ** strings;
  11.  
  12. void prepare_strings(void);
  13. void show(int);
  14. void free_stuff(void);
  15.  
  16. int main (void) {
  17.   if (one) one /= one; else one ++;
  18.   two = one + one, three = one + two, five = two + three; // yay maths
  19.   ten = five * two, onehundred = ten * ten;
  20.   digit_base = ten * five - two;
  21.   prepare_strings();
  22.   int x;
  23.   for (x = one; x <= onehundred; x ++) show(x);
  24.   free_stuff();
  25.   return one - one; // can't do return 0!
  26. }
  27.  
  28. void show (int number) {
  29.   int matched;
  30.   if (matched) matched = !matched;
  31.   if (!(number % three)) matched ++;
  32.   if (!(number % five)) matched += two;
  33.   if (matched)
  34.     puts(strings[matched]);
  35.   else {
  36.     if (number >= ten) putchar(digit_base + number / ten);
  37.     putchar(digit_base + number % ten);
  38.     putchar(ten); // newline
  39.   }
  40. }
  41.  
  42. void free_stuff (void) {
  43.   free(fizz);
  44.   free(buzz);
  45.   free(fizzbuzz);
  46.   free(strings);
  47. }
  48.  
  49. void prepare_strings (void) {
  50.   fizz = calloc(five, one);
  51.   buzz = calloc(five, one);
  52.   fizzbuzz = calloc(five, two);
  53.   char letter_base = onehundred - five + one;
  54.   char b = letter_base + two, f = letter_base + five + one, i = letter_base + ten - one, u = letter_base + ten * two + one, z = letter_base + five * five + one;
  55.   *fizz = f, fizz[one] = i, fizz[two] = fizz[three] = z;
  56.   *buzz = b, buzz[one] = u, buzz[two] = buzz[three] = z;
  57.   strcpy(fizzbuzz, fizz);
  58.   strcat(fizzbuzz, buzz);
  59.   strings = malloc(onehundred); // let's say that sizeof(char *) is a constant, so I won't use it
  60.   strings[one] = fizz, strings[two] = buzz, strings[three] = fizzbuzz;
  61. }
Advertisement
RAW Paste Data Copied
Advertisement