Typical.
The entry I submitted used a seperate template to simplify the fraction because I couldnt do it with just the struct presented in the original question........and as you cant define any new public variables I was stumped
Sitting on the train this morning, I figured out how to get rid of that spare template and hold it only with numerator & denominator defined...so I'm sitting here in work wishing I noticed this earlier.....Still, probably not the best method (a lot longer than some people @ cpphome have claimed to do it in) and I know I have too many brackets (I could dump a few, but its too late to worry now).....but I was trying to do this for ages and now its too late as the contest has finished. Ah well!
If anyones interested, this is as far as I have got so far
Code:
#include <iostream>
#include <cassert>
//Main struct interface
template <int n1, int d1, int n2, int d2>
struct add_fraction
{
static const int numerator;
static const int denominator;
};
//Specialisation 1: Subvert functionality
template <int n1, int d1>
struct add_fraction<n1,d1,0,0>
{
static const int denominator = add_fraction<d1,n1%d1,0,0>::denominator;
};
//Specialisation 2: Assign value
template <int n1>
struct add_fraction<n1,0,0,0>
{
static const int denominator = n1;
};
//The Numerator
template <int n1, int d1, int n2, int d2>
const int add_fraction<n1,d1,n2,d2>::numerator =
((((d2*d1)/d1)*n1)+(((d2*d1)/d2)*n2)) /
add_fraction<((((d2*d1)/d1)*n1)+(((d2*d1)/d2)*n2)),
((d2*d1)%((((d2*d1)/d1)*n1)+(((d2*d1)/d2)*n2))),0,0>::denominator;
//The Denominator
template <int n1, int d1, int n2, int d2>
const int add_fraction<n1,d1,n2,d2>::denominator = (d1*d2)/
add_fraction<((((d2*d1)/d1)*n1)+(((d2*d1)/d2)*n2)),
((d2*d1)%((((d2*d1)/d1)*n1)+(((d2*d1)/d2)*n2))),0,0>::denominator;
int main() {
typedef add_fraction<2,3,1,12> three_fourths;
assert(three_fourths::numerator==3);
assert(three_fourths::denominator==4);
int n = three_fourths::numerator;
int d = three_fourths::denominator;
std::cout << n;
std::cout << std::endl;
std::cout << d;
}