Last active
December 21, 2018 20:16
-
-
Save RealKC/0fe593f3cbb504777cd523fc7ed4be40 to your computer and use it in GitHub Desktop.
Needlessly complicated fizzbuzz using templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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