Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 4aae951

Browse files
committed
Add rest of cpp programs
Signed-off-by: tsolakou <[email protected]>
1 parent ee6b1c4 commit 4aae951

14 files changed

+539
-0
lines changed

fixtures/accumulator_factory.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
3+
class Acc
4+
{
5+
public:
6+
Acc(int init)
7+
: _type(intType)
8+
, _intVal(init)
9+
{}
10+
11+
Acc(float init)
12+
: _type(floatType)
13+
, _floatVal(init)
14+
{}
15+
16+
int operator()(int x)
17+
{
18+
if( _type == intType )
19+
{
20+
_intVal += x;
21+
return _intVal;
22+
}
23+
else
24+
{
25+
_floatVal += x;
26+
return static_cast<int>(_floatVal);
27+
}
28+
}
29+
30+
float operator()(float x)
31+
{
32+
if( _type == intType )
33+
{
34+
_floatVal = _intVal + x;
35+
_type = floatType;
36+
return _floatVal;
37+
}
38+
else
39+
{
40+
_floatVal += x;
41+
return _floatVal;
42+
}
43+
}
44+
private:
45+
enum {floatType, intType} _type;
46+
float _floatVal;
47+
int _intVal;
48+
};
49+
50+
int main()
51+
{
52+
Acc a(1);
53+
a(5);
54+
Acc(3);
55+
std::cout << a(2.3f);
56+
return 0;
57+
}

fixtures/bench_hundred_doors.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
3+
int main()
4+
{
5+
bool is_open[100] = { false };
6+
7+
// do the 100 passes
8+
for (int pass = 0; pass < 100; ++pass)
9+
for (int door = pass; door < 100; door += pass+1)
10+
is_open[door] = !is_open[door];
11+
12+
// output the result
13+
for (int door = 0; door < 100; ++door)
14+
std::cout << "door #" << door+1 << (is_open[door]? " is open." : " is closed.") << std::endl;
15+
return 0;
16+
}
17+

fixtures/binary_search.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
template <class T>
2+
int binSearch(const T arr[], int len, T what) {
3+
int low = 0;
4+
int high = len - 1;
5+
while (low <= high) {
6+
int mid = (low + high) / 2;
7+
if (arr[mid] > what)
8+
high = mid - 1;
9+
else if (arr[mid] < what)
10+
low = mid + 1;
11+
else
12+
return mid;
13+
}
14+
return -1; // indicate not found
15+
}

fixtures/fibonacci.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
3+
int main()
4+
{
5+
unsigned int a = 1, b = 1;
6+
unsigned int target = 48;
7+
for(unsigned int n = 3; n <= target; ++n)
8+
{
9+
unsigned int fib = a + b;
10+
std::cout << "F("<< n << ") = " << fib << std::endl;
11+
a = b;
12+
b = fib;
13+
}
14+
15+
return 0;
16+
}

fixtures/fizzbuzz.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
for (int i = 0; i <= 100; ++i)
7+
{
8+
bool fizz = (i % 3) == 0;
9+
bool buzz = (i % 5) == 0;
10+
if (fizz)
11+
cout << "Fizz";
12+
if (buzz)
13+
cout << "Buzz";
14+
if (!fizz && !buzz)
15+
cout << i;
16+
cout << "\n";
17+
}
18+
return 0;
19+
}

fixtures/gcd.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
static void Main(string[] args)
2+
{
3+
Console.WriteLine("GCD of {0} and {1} is {2}", 1, 1, gcd(1, 1));
4+
Console.WriteLine("GCD of {0} and {1} is {2}", 1, 10, gcd(1, 10));
5+
Console.WriteLine("GCD of {0} and {1} is {2}", 10, 100, gcd(10, 100));
6+
Console.WriteLine("GCD of {0} and {1} is {2}", 5, 50, gcd(5, 50));
7+
Console.WriteLine("GCD of {0} and {1} is {2}", 8, 24, gcd(8, 24));
8+
Console.WriteLine("GCD of {0} and {1} is {2}", 36, 17, gcd(36, 17));
9+
Console.WriteLine("GCD of {0} and {1} is {2}", 36, 18, gcd(36, 18));
10+
Console.WriteLine("GCD of {0} and {1} is {2}", 36, 19, gcd(36, 19));
11+
for (int x = 1; x < 36; x++)
12+
{
13+
Console.WriteLine("GCD of {0} and {1} is {2}", 36, x, gcd(36, x));
14+
}
15+
Console.Read();
16+
}
17+
18+
// Greatest Common Denominator using Euclidian Algorithm
19+
// Gist: https://gist.github.com/SecretDeveloper/6c426f8993873f1a05f7
20+
static int gcd(int a, int b)
21+
{
22+
return b==0 ? a : gcd(b, a % b);
23+
}
24+

fixtures/happy_numbers.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
unsigned int happy_iteration(unsigned int n)
2+
{
3+
unsigned int result = 0;
4+
while (n > 0)
5+
{
6+
unsigned int lastdig = n % 10;
7+
result += lastdig*lastdig;
8+
n /= 10;
9+
}
10+
return result;
11+
}
12+
13+
bool is_happy(unsigned int n)
14+
{
15+
unsigned int n2 = happy_iteration(n);
16+
while (n != n2)
17+
{
18+
n = happy_iteration(n);
19+
n2 = happy_iteration(happy_iteration(n2));
20+
}
21+
return n == 1;
22+
}
23+
24+
#include <iostream>
25+
26+
int main()
27+
{
28+
unsigned int current_number = 1;
29+
30+
unsigned int happy_count = 0;
31+
while (happy_count != 8)
32+
{
33+
if (is_happy(current_number))
34+
{
35+
std::cout << current_number << " ";
36+
++happy_count;
37+
}
38+
++current_number;
39+
}
40+
std::cout << std::endl;
41+
}

fixtures/is_prime.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <cmath>
2+
3+
bool is_prime(unsigned int n)
4+
{
5+
if (n <= 1)
6+
return false;
7+
if (n == 2)
8+
return true;
9+
for (unsigned int i = 2; i <= sqrt(n); ++i)
10+
if (n % i == 0)
11+
return false;
12+
return true;
13+
}

fixtures/mutual_recursion.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <iterator>
4+
5+
class Hofstadter
6+
{
7+
public:
8+
static int F(int n) {
9+
if ( n == 0 ) return 1;
10+
return n - M(F(n-1));
11+
}
12+
static int M(int n) {
13+
if ( n == 0 ) return 0;
14+
return n - F(M(n-1));
15+
}
16+
};
17+
18+
using namespace std;
19+
20+
int main()
21+
{
22+
int i;
23+
vector<int> ra, rb;
24+
25+
for(i=0; i < 20; i++) {
26+
ra.push_back(Hofstadter::F(i));
27+
rb.push_back(Hofstadter::M(i));
28+
}
29+
copy(ra.begin(), ra.end(),
30+
ostream_iterator<int>(cout, " "));
31+
cout << endl;
32+
copy(rb.begin(), rb.end(),
33+
ostream_iterator<int>(cout, " "));
34+
cout << endl;
35+
return 0;
36+
}

0 commit comments

Comments
 (0)