fizz buzz
1
Name:
Anonymous
2012-05-28 0:33
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
class FizzBuzz
{
private:
std::vector<std::string> container;
static const int Mod3=0x01;
static const int Mod5=0x02;
int upto;
public:
template<typename T> static inline std::string Stringify(T&);
FizzBuzz(int upto)
{this->upto=upto;}
void Print();
private:
inline void Process(unsigned int&, int&);
std::vector<std::string> * Check();
};
template<typename T>
inline std::string FizzBuzz::Stringify(T& parm)
{
std::stringstream out;
out << parm;
return out.str();
}
inline void FizzBuzz::Process(unsigned int& c, int& i)
{
if(!(i%3))
c |= Mod3;
if(!(i%5))
c |= Mod5;
}
std::vector<std::string> * FizzBuzz::Check()
{
unsigned int cond;
for(int i=0; i<upto; ++i, cond=0x0)
{
Process(cond, i);
switch(cond)
{
case (Mod3 ^ Mod5):
container.push_back("FizzBuzz");
break;
case Mod3:
container.push_back("Fizz");
break;
case Mod5:
container.push_back("Buzz");
break;
case (Mod3 & Mod5):
container.push_back(Stringify(i));
break;
}
}
return &this->container;
}
void FizzBuzz::Print()
{
std::vector<std::string> * results=Check(); //execute
//print results
for(auto iter=results->begin();
iter!=results->end(); ++iter)
{
std::cout << *iter << std::endl;
}
}
int main()
{
FizzBuzz * demo=new FizzBuzz(100);
demo->Print();
delete demo;
return EXIT_SUCCESS;
}
2
Name:
Anonymous
2012-05-28 1:17
template <int n, int m3 = n % 3, int m5 = n % 5>
struct FizzBuzz {
static int value() {return n;}
};
template <int n, int m5>
struct FizzBuzz<n, 0, m5> {
static char const* value() {return "Fizz";}
};
template <int n, int m3>
struct FizzBuzz<n, m3, 0> {
static char const* value() {return "Buzz";}
};
template <int n>
struct FizzBuzz<n, 0, 0> {
static char const* value() {return "FizzBuzz";}
};
3
Name:
Anonymous
2012-05-28 6:25
Setting the bar pretty low, aren't you?
4
Name:
Anonymous
2012-05-28 7:04
#include <stdio.h>
int main(void)
{
int fizz = FIZZ;
int buzz = BUZZ;
int i;
for (i = 1; i <= 100; i++)
switch (i == fizz | (i == buzz) << 1) {
case 0:
printf("%d\n", i);
break;
case 1:
puts("fizz");
fizz += FIZZ;
break;
case 2:
puts("buzz");
buzz += BUZZ;
break;
case 3:
puts("fizz-buzz");
fizz += FIZZ;
buzz += BUZZ;
break;
default:
;
}
return 0;
}
5
Name:
Anonymous
2012-05-28 9:06
>>4
stdio.h
U MENA IOSTREAM ??????
6
Name:
Anonymous
2012-05-28 10:15
>>5
EEYO EEEYO EEEEYO EEEYO EEEEYO EEEYO
EEEYEN EEYEN EEEYEEN EEEEEEYEN
7
Name:
Anonymous
2012-05-28 10:23
>>4
[quote]default:[/quote]
why, god, why
8
Name:
Anonymous
2012-05-28 10:24
>>7
Goddamn fuck my bbcode habits
9
Name:
Anonymous
2012-05-28 10:26
10
Name:
Anonymous
2012-05-28 11:03
>>4
So what are FIZZ and BUZZ equal to?
11
Name:
Anonymous
2012-05-28 11:37
12
Name:
Anonymous
2012-05-28 12:02
(why) default
Lambda Calculus
So what are FIZZ and BUZZ equal to?
Lambda Calculus
FIZZ == default?
BUZZ == default?
13
Name:
Anonymous
2012-05-28 12:20
"Lambda calculus." != "Lambda Calculus."
Looks like you ain't read THE STANDARD .
14
Name:
Anonymous
2012-05-28 12:24
Just to clarify, the reason for the use of "default" is because of Lambda Calculus which FIZZ and BUZZ happen to be equal to.
Figuring out why this is so is left as an exercise for the reader.
15
Name:
Anonymous
2012-05-28 14:13
Now all we need is a Java version with a FizzBuzzAbstractFactoryFactoryFactory.
16
Name:
Anonymous
2012-05-28 15:41
abstracte bullshite
17
Name:
Anonymous
2012-05-28 15:43
fizzbuzz n = [ fb |
x <- [1 .. n],
fb <- [ f ++ b |
f <- [if x `mod` 3 == 0 then "fizz" else ""],
b <- [if x `mod` 5 == 0 then "buzz" else ""],
f <- [if (f == "") && (b == "") then show x else f]
]
]
18
Name:
bampu pantsu
2012-05-29 5:13
bampu pantsu
19
Name:
Anonymous
2014-03-11 6:39
>>2
pattern matching has never been more repulsive
20
Name:
Anonymous
2014-03-11 11:44
Thread is too old to post in! Make a new one.
Newer Posts