By now you probably have a question or two, but the code should explain it:
Code: Select all
#include <vector>
struct F { // this should explain it self, numerator, denominetor...
long n, d;
};
class Fraction {
std::vector<unsigned short> P; // refer to constructor.
F Factorize(F f) { // for factorizing the fractions.
for(short i = 0; P[i] <= f.n && P[i] <= f.d; i++) {
if(!(f.n % P[i]) && !(f.d % P[i])) {
f.n /= P[i];
f.d /= P[i];
i = -1;
};
}
return f;
}
public:
Fraction() { // generating list of primes for factorization.
P.push_back(2);
for(unsigned short n = 3; n < 0x3fff; n += 2) {
for(unsigned short i = 1; P[i] * P[i] <= n; i++) {
if(!(n % P[i])) {
i = 0;
n += 2;
if(n > 0x3fff) break;
}
}
P.push_back(n);
}
}
F Add(F a, F b) { // From here it should be pretty self explaining.
a.n = (a.n*b.d)+(b.n*a.d);
a.d *= b.d;
return Factorize(a);
}
F Sub(F a, F b) {
a.n = (a.n*b.d)-(b.n*a.d);
a.d *= b.d;
return Factorize(a);
}
F Mul(F a, F b) {
a.n *= b.n;
a.d *= b.d;
return Factorize(a);
}
F Div(F a, F b) {
a.n *= b.d;
a.d *= b.n;
return Factorize(a);
}
};
I know of course that a lot of uptimization can be done, but though I also welcome such suggestions, this is not the apparent problem.
It just seems too complicated and messy.
Regards.
edit:
I just discovered a problem regarding natagive numbers. fx. 1/6 - 1/2 = -4/12 where the result should be -1 over 3. I can only imagine that (a % b) don't work as expected if negative numbers are involved. I have though and implimented a solution, but again it is very very ugly.
Code: Select all
F Factorize(F f) { // for factorizing the fractions.
bool sign[2] = {0};
if(f.n < 0) {
sign[0] = 1;
f.n *= -1;
}
if(f.d < 0) {
sign[1] = 1;
f.d *= -1;
}
for(short i = 0; P[i] <= f.n && P[i] <= f.d; i++) {
if(!(f.n % P[i]) && !(f.d % P[i])) {
f.n /= P[i];
f.d /= P[i];
i = -1;
};
}
if(sign[0]) f.n *=-1;
if(sign[1]) f.d *=-1;
return f;
}