Skip to content

Instantly share code, notes, and snippets.

@imaami
Created September 9, 2024 16:36
Show Gist options
  • Save imaami/d48680fc64a4b5833f7f502d0907d3b1 to your computer and use it in GitHub Desktop.
Save imaami/d48680fc64a4b5833f7f502d0907d3b1 to your computer and use it in GitHub Desktop.
FizzBuzz Enterprise Edition by claude-3.5-sonnet
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FIZZBUZZ_LENGTH 8
#define MAX_NUMBER_LENGTH 10
typedef struct {
int (*evaluate)(int);
char *(*transform)(void);
} FizzBuzzRule;
typedef struct {
FizzBuzzRule **rules;
int ruleCount;
} FizzBuzzStrategy;
typedef struct {
FizzBuzzStrategy *(*createStrategy)(void);
char *(*executeFizzBuzz)(FizzBuzzStrategy *, int);
} FizzBuzzFactory;
typedef struct {
FizzBuzzFactory *factory;
} FizzBuzzFacade;
typedef struct {
FizzBuzzFacade *facade;
} FizzBuzzContext;
int
evaluateFizz (int num)
{
return num % 3 == 0;
}
int
evaluateBuzz (int num)
{
return num % 5 == 0;
}
char *
transformFizz (void)
{
return "Fizz";
}
char *
transformBuzz (void)
{
return "Buzz";
}
FizzBuzzRule *
createRule (int (*evaluate)(int), char *(*transform)(void))
{
FizzBuzzRule *rule = (FizzBuzzRule *)malloc(sizeof(FizzBuzzRule));
rule->evaluate = evaluate;
rule->transform = transform;
return rule;
}
FizzBuzzStrategy *
createFizzBuzzStrategy (void)
{
FizzBuzzStrategy *strategy = (FizzBuzzStrategy *)malloc(sizeof(FizzBuzzStrategy));
strategy->rules = (FizzBuzzRule **)malloc(2 * sizeof(FizzBuzzRule *));
strategy->ruleCount = 2;
strategy->rules[0] = createRule(evaluateFizz, transformFizz);
strategy->rules[1] = createRule(evaluateBuzz, transformBuzz);
return strategy;
}
char *
executeFizzBuzzAlgorithm (FizzBuzzStrategy *strategy, int num)
{
char *result = (char *)malloc(MAX_FIZZBUZZ_LENGTH * sizeof(char));
result[0] = '\0';
for (int i = 0; i < strategy->ruleCount; i++) {
if (strategy->rules[i]->evaluate(num)) {
strcat(result, strategy->rules[i]->transform());
}
}
if (strlen(result) == 0) {
snprintf(result, MAX_NUMBER_LENGTH, "%d", num);
}
return result;
}
FizzBuzzFactory *
createFizzBuzzFactory (void)
{
FizzBuzzFactory *factory = (FizzBuzzFactory *)malloc(sizeof(FizzBuzzFactory));
factory->createStrategy = createFizzBuzzStrategy;
factory->executeFizzBuzz = executeFizzBuzzAlgorithm;
return factory;
}
FizzBuzzFacade *
createFizzBuzzFacade (FizzBuzzFactory *factory)
{
FizzBuzzFacade *facade = (FizzBuzzFacade *)malloc(sizeof(FizzBuzzFacade));
facade->factory = factory;
return facade;
}
FizzBuzzContext *
createFizzBuzzContext (FizzBuzzFacade *facade)
{
FizzBuzzContext *context = (FizzBuzzContext *)malloc(sizeof(FizzBuzzContext));
context->facade = facade;
return context;
}
void
destroyFizzBuzzContext (FizzBuzzContext *context)
{
free(context->facade->factory->createStrategy()->rules[0]);
free(context->facade->factory->createStrategy()->rules[1]);
free(context->facade->factory->createStrategy()->rules);
free(context->facade->factory->createStrategy());
free(context->facade->factory);
free(context->facade);
free(context);
}
int
main ()
{
FizzBuzzFactory *factory = createFizzBuzzFactory();
FizzBuzzFacade *facade = createFizzBuzzFacade(factory);
FizzBuzzContext *context = createFizzBuzzContext(facade);
FizzBuzzStrategy *strategy = context->facade->factory->createStrategy();
for (int i = 1; i <= 100; i++) {
char *result = context->facade->factory->executeFizzBuzz(strategy, i);
printf("%s\n", result);
free(result);
}
destroyFizzBuzzContext(context);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment