3 ms·
My "I'm going to hell" C++ version: https://gist.github.com/3838042 https://gist.github.com/3838042 Or, since github's being broken right now: #include <i
by LnxPrgr3 14y ago
My "I'm going to hell" C++ version: https://gist.github.com/3838042 https://gist.github.com/3838042
Or, since github's being broken right now:
#include <iostream>
#include <arpa/inet.h>
template <int multiples_of, uint32_t noise>
class Noisemaker {
private:
union {
uint32_t noise_as_int;
char noise_as_cstr[5];
};
public:
Noisemaker() {
noise_as_int = htonl(noise);
noise_as_cstr[4] = '\0';
}
bool operator()(std::ostream &out, int i) {
if(i % multiples_of == 0) {
out << noise_as_cstr;
return true;
}
return false;
}
};
template <typename Noise1, typename Noise2>
class NoisemakerPair {
private:
Noise1 noise1;
Noise2 noise2;
public:
bool operator()(std::ostream &out, int i) {
bool matched = false;
matched |= noise1(out, i);
matched |= noise2(out, i);
return matched;
}
};
int main(int argc, char *argv[]) {
NoisemakerPair<Noisemaker<3, 'Fizz'>, Noisemaker<5, 'Buzz'> > fizzbuzz;
for(int i=1;i<=100;++i) {
if(!fizzbuzz(std::cout, i))
std::cout << i;
std::cout << std::endl;
}
}
You can nest NoisemakerPairs to add more Noisemakers. But good luck with strings longer than 4 characters… for some absurd reason, string literals can't be template parameters. Go figure.