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

[ENTERPRISE] FizzBuzz (part MAXINT) [CODING]

Name: Anonymous 2012-04-08 10:41

Is this ENTERPRISE QUALITY?

#include <stdbool.h>
#include <stdio.h>

struct rule {
  bool (*pred)(int);
  char *word;
};

void count(struct rule rules[], int min, int max) {
  int num, i;
  bool found;

  for (num = min; num <= max; num++) {
    found = false;

    /* search for words */
    for (i = 0; rules[i].pred != NULL; i++) {
      if ((*rules[i].pred)(num)) {
        printf("%s", rules[i].word);
        found = true;
      }
    }

    /* else just print the number */
    if (!found) printf("%d", num);
    putchar('\n');
  }
}

bool Fizz(int x) { return (x % 3 == 0); }
bool Buzz(int x) { return (x % 5 == 0); }

struct rule fizzbuzz_rules[] = {
  {&Fizz, "Fizz"},
  {&Buzz, "Buzz"},
  {NULL, NULL},
};

int main() {
  count(fizzbuzz_rules, 1, 100);
  return 0;
}

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