Skip to content

Instantly share code, notes, and snippets.

@RealKC
Last active December 21, 2018 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RealKC/0fe593f3cbb504777cd523fc7ed4be40 to your computer and use it in GitHub Desktop.
Save RealKC/0fe593f3cbb504777cd523fc7ed4be40 to your computer and use it in GitHub Desktop.
Needlessly complicated fizzbuzz using templates
#include <algorithm>
#include <cstring>
#include <iostream>
#include <iterator>
template<class T>
class Fizz
{
public:
bool operator()(T t) {
return static_cast<bool>(!(t%3));
}
};
template<class T>
class Buzz
{
public:
bool operator()(T t) {
return static_cast<bool>(!(t%5));
}
};
template<class C>
bool call(C c, int x) {
return c(x);
}
void fizzbuzz() {
char s[10],
f[] = {70, 105, 122, 122, 0},
b[] = {66, 117, 122, 122, 0};
for(int i=0; i<=100; ++i) {
std::fill(std::begin(s), std::end(s), 0);
if(call(Fizz<int>(), i)) { strcat(s, f); }
if(call(Buzz<int>(), i)) { strcat(s, b); }
if(0[s] == 0 ) snprintf(s, sizeof(s), "%i", i);
std::cout << s << static_cast<char>(10);
}
}
int main()
{
fizzbuzz();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment