aaaaaa123456789 icon

Fizzbuzz without constants

aaaaaa123456789 | PRO | 01/05/15 11:07:21 PM UTC | 0 ⭐ | 5541 👁️ | Never ⏰ | []
C |

1.7 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int one, two, three, five, ten, onehundred;
int digit_base;
char * fizz;
char * buzz;
char * fizzbuzz;
char ** strings;
 
void prepare_strings(void);
void show(int);
void free_stuff(void);
 
int main (void) {
  if (one) one /= one; else one ++;
  two = one + one, three = one + two, five = two + three; // yay maths
  ten = five * two, onehundred = ten * ten;
  digit_base = ten * five - two;
  prepare_strings();
  int x;
  for (x = one; x <= onehundred; x ++) show(x);
  free_stuff();
  return one - one; // can't do return 0!
}
 
void show (int number) {
  int matched;
  if (matched) matched = !matched;
  if (!(number % three)) matched ++;
  if (!(number % five)) matched += two;
  if (matched)
    puts(strings[matched]);
  else {
    if (number >= ten) putchar(digit_base + number / ten);
    putchar(digit_base + number % ten);
    putchar(ten); // newline
  }
}
 
void free_stuff (void) {
  free(fizz);
  free(buzz);
  free(fizzbuzz);
  free(strings);
}
 
void prepare_strings (void) {
  fizz = calloc(five, one);
  buzz = calloc(five, one);
  fizzbuzz = calloc(five, two);
  char letter_base = onehundred - five + one;
  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;
  *fizz = f, fizz[one] = i, fizz[two] = fizz[three] = z;
  *buzz = b, buzz[one] = u, buzz[two] = buzz[three] = z;
  strcpy(fizzbuzz, fizz);
  strcat(fizzbuzz, buzz);
  strings = malloc(onehundred); // let's say that sizeof(char *) is a constant, so I won't use it
  strings[one] = fizz, strings[two] = buzz, strings[three] = fizzbuzz;
}

Comments