DEV Community

loading...

Discussion on: Fizz Buzz in Every Language

Collapse
slurpsmadrips profile image
Isabella Muerte

There's several ways to do it in C++ :)

The Classic

#include <cstdio>
int main () {
  for (auto i = 1; i < 101; ++i) {
    if (i % 3 == 0) { std::printf("Fizz"); }
    if (i % 5 == 0) { std::printf("Buzz"); }
    std::printf(" %d\n", i);
  }
}

The "GOSH, MOM, IT'S GENERIC"

#include <cstdio>

struct number {
  number (int x) : x(x) { }
  auto operator * () const { return this->x; }
  bool operator == (int y) { return this->x == y; }
  bool operator != (int y) { return this->x != y; }
  number& operator ++ () { ++this->x; return *this; }
  number operator ++ (int) { return this->x++; }
  int x;
};


struct range {
    range (int start, int stop) :
      start(start),
      stop(stop)
    { }

    auto begin () const { return number(this->start); }
    auto end () const { return this->stop; }
private:
  int start;
  int stop;
};



int main () {
  for (auto i : range(1, 101)) {
    if (i % 3 == 0) { std::printf("Fizz"); }
    if (i % 5 == 0) { std::printf("Buzz"); }
    std::printf(" %d\n", i);
  }
}

The "Calculated At Compile Time (stare into madness edition)"

#include <type_traits>
#include <utility>
#include <cstdio>

template <char... Args>
struct as_string { const char data[sizeof... (Args)] = { Args... }; };

using fizz = as_string<'f', 'i', 'z', 'z'>;
using buzz = as_string<'b', 'u', 'z', 'z'>;
using newline = as_string<'\n'>;
using null = as_string<'\0'>;
struct fizzbuzz: fizz, buzz { };

template <class T> struct type_identity { using type = T; };

template <class T>
constexpr auto digits (T x) {
    auto digits = 0;
    while (x) {
        x /= 10;
        digits++;
    }
    return digits;
}

template <auto Size, auto X, char... Args>
struct to_string : to_string<Size - 1, X / 10, '0' + X % 10, Args...> { };

template <auto X, char... Args>
struct to_string<1, X, Args...> : type_identity<as_string<'0' + X, Args...>> { };

template <unsigned int X>
using int_to_string = typename to_string<digits(X), X>::type;

template <auto X, class Fizzable = std::bool_constant<X % 3 == 0>, class Buzzable = std::bool_constant<X % 5 == 0>> struct check;

template <auto X> struct check<X, std::false_type, std::false_type> : int_to_string<X> { };
template <auto X> struct check<X, std::true_type, std::false_type> : fizz, int_to_string<X> { };
template <auto X> struct check<X, std::false_type, std::true_type> : buzz, int_to_string<X> { };
template <auto X> struct check<X, std::true_type, std::true_type> : fizzbuzz, int_to_string<X> { };

template <auto X> struct calc : check<X>, newline { };
template <auto... X> struct print : calc<X>..., null { };

template <auto... Is>
print<(Is + 1)...> deduce (std::index_sequence<Is...>);

template <auto N>
using result = decltype(deduce(std::make_index_sequence<N>()));

static constexpr result<100> fb{};

int main() {
    std::puts(reinterpret_cast<const char *>(&fb));
    return 0;
}
Collapse
cjbrooks12 profile image
Casey Brooks

number 3 is the Cthulu of FizzBuzz

Collapse
renegadecoder94 profile image
Jeremy Grifski Author

That last one scares me. Haha I’m not sure I’d know where to start reading it.

Thanks for jumping into the challenge with some great additions. 😃

Collapse
nepeckman profile image
nepeckman

I raise you the compile time calculated fizzbuzz in Nim ;)

proc fizzbuzz(): seq[string] =
  result = @[]
  for i in 1..100:
    result.add(
      if i mod 15 == 0: "FizzBuzz"
      elif i mod 5 == 0: "Buzz"
      elif i mod 3 == 0: "Fizz"
      else: $i
    )

const compileTimeValue = fizzbuzz()

echo compileTimeValue
Collapse
renegadecoder94 profile image
Jeremy Grifski Author

Interested in adding this to the collection? 😁

Collapse
slurpsmadrips profile image
Isabella Muerte

C++20 isn't available yet, but we're a bit closer to this approach. :)

Collapse
theodesp profile image