Skip to content

Instantly share code, notes, and snippets.

@RealKC
Last active December 24, 2018 12:58
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/4da292d0e153acdb2c7099adc85140ff to your computer and use it in GitHub Desktop.
Save RealKC/4da292d0e153acdb2c7099adc85140ff to your computer and use it in GitHub Desktop.
Fizzbuzz with a twist! Uses digraphs, class templates, inheritance, function pointers, and possibly more to overcomplicate the classic FIzzBuzz into something that Satan himself would want smitten.
/*
* THIS CODE IS ABSOLUTE CANCER
*/
%:include <algorithm>
%:include <cstring>
%:include <iostream>
%:include <iterator>
%:include <exception>
template<class T>
T moduloOperation(T a, T b)
<%
return a%b;
%>
template<class T>
class PropertyOfNumberThroughUsageOfModuloOperator
<%
public:
virtual bool operator()(T t, T numberToBeUsedWithModuloOperator) <%
return static_cast<bool>(!moduloOperation<T>(t, numberToBeUsedWithModuloOperator));
%>
%>;
template<class T>
class Fizz : public PropertyOfNumberThroughUsageOfModuloOperator<T>
<%
public:
virtual bool operator()(T t) final <%
return static_cast<bool>(!moduloOperation<T>(t, 3));
%>
%>;
template<class T>
class Buzz : public PropertyOfNumberThroughUsageOfModuloOperator<T>
<%
public:
virtual bool operator()(T t) final <%
return static_cast<bool>(!moduloOperation<T>(t, 5));
%>
%>;
template<class C>
bool call(C c, int x)
<%
return c(x);
%>
template<class T>
struct Range
<%
T x, y;
%>;
int sanitizedIntInput(std::string input)
<%
int ret;
for(char c : input) <%
if(isdigit(c)) <%
ret = ret * 10 + static_cast<int>(c - static_cast<char>(48));
%> else <%
char message<::> = <%
80, 108, 101, 97, 115, 101, 32, 105, 110, 112, 117, 116,
32, 97, 32, 110, 117, 109, 98, 101, 114, 46, 10, 000
%>;
throw std::invalid_argument(message);
%>
%>
return ret;
%>
Range<int> inputRangeForFizzBuzz()
<%
Range<int> range;
char lower<::> = <%
73, 110, 112, 117, 116, 32, 116, 104, 101, 32, 108, 111,
119, 101, 114, 32, 112, 97, 114, 116, 32, 111, 102, 32,
116, 104, 101, 32, 114, 97, 110, 103, 101, 58, 32, 000
%>, upper<::> = <%
73, 110, 112, 117, 116, 32, 116, 104, 101, 32, 117, 112,
112, 101, 114, 32, 112, 97, 114, 116, 32, 111, 102, 32,
116, 104, 101, 32, 114, 97, 110, 103, 101, 58, 32, 000
%>;
std::string input;
bool goodInputFinally = static_cast<bool>(0),
rangeXIsGood = static_cast<bool>(0),
rangeYIsGood = static_cast<bool>(0);
while(!goodInputFinally) <%
std::cout << lower; std::getline(std::cin, input);
rangeXIsGood = static_cast<bool>(0);
rangeYIsGood = static_cast<bool>(0);
try <%
range.x = sanitizedIntInput(input);
rangeXIsGood = static_cast<bool>(1);;
%> catch (std::invalid_argument const& e) <%
std::cout << e.what();
%>
if(rangeXIsGood) <%
std::cout << upper; std::getline(std::cin, input);
try <%
range.y = sanitizedIntInput(input);
rangeYIsGood = static_cast<bool>(1);;
%> catch (std::invalid_argument const& e) <%
std::cout << e.what();
%>
if(rangeXIsGood && rangeYIsGood) <%
goodInputFinally = static_cast<bool>(1);
%>
//Note that intended behaviour is outputting FizzBuzz if the users only inputed two newline characters.
%>
%>
if(range.y < range.x) <%
char message<::> = <%
84, 104, 101, 32, 117, 112, 112, 101, 114, 32, 112,
97, 114, 116, 32, 115, 104, 111, 117, 108, 100, 32,
98, 101, 32, 97, 32, 98, 105, 103, 103, 101, 114, 32,
110, 117, 109, 98, 101, 114, 32, 116, 104, 97, 110,
32, 116, 104, 101, 32, 108, 111, 119, 101, 114, 32,
112, 97, 114, 116, 33, 10, 000
%>;
throw std::invalid_argument(message);
%> else <% return range; %>
%>
void fizzbuzz() <%
Range<int> range;
bool goodRangeYet = false;
while(!goodRangeYet) <%
try <%
range = inputRangeForFizzBuzz();
goodRangeYet = true;
%> catch(std::invalid_argument const& e) <%
std::cout << e.what();
%>
%>
char s<:10:>,
f<::> = <%70, 105, 122, 122, 0%>,
b<::> = <%66, 117, 122, 122, 0%>;
for(int i = range.x; i <= range.y; ++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);
%>
%>
template<class T>
void loopFun(T t)
<%
char ask<::> = <%
87, 111, 117, 108, 100, 32, 121, 111, 117, 32, 108, 105, 107, 101, 32,
116, 111, 32, 99, 111, 110, 116, 105, 110, 117, 101, 63, 10, 89, 32, 105,
102, 32, 121, 101, 115, 44, 32, 78, 32, 105, 102, 32, 110, 111, 10, 000
%>, badChoice<::> = <%
66, 97, 100, 32, 99, 104, 111, 105, 99, 101, 44, 32, 116, 114, 121, 32,
97, 103, 97, 105, 110, 10, 000
%>;
bool quit = static_cast<bool>(0);
char choice;
while(!quit) <%
t();
tryagain:
std::cout << ask; std::cin >> choice;
switch(choice) <%
case 'Y': case 'y':
break;
case 'N':case 'n':
quit = static_cast<bool>(1);
break;
default:
std::cout << badChoice;
goto tryagain;
%>
%>
%>
void (*fizzbuzzPointer)() = fizzbuzz;
int main()
<%
loopFun(fizzbuzzPointer);
return 0;
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment