#include "MixedExpression.h"
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
long GCD(long x, long y) // Get the greatest common divisor.
{
long remainder; // Holds the remainder.
x = abs(x);
y = abs(y);
while(y != 0)
{
// Get the value of the remainder by dividing y by x.
remainder = x % y;
x = y;
y = remainder;
}
// Return the greatest common divisor.
return x;
}
// Convert a mixed expression to its normal form.
void reduce(void)
{
long a, b, c, x;
long gcd;
if(GCD(x,c) > 0)
{
x = a + (b/c);
gcd = GCD(x, c);
x /= gcd;
c /= gcd;
}
}
// Set three values in the default constructor to 0.
MixedExpression::MixedExpression()
{
a = 0;
b = 0;
c = 1;
}
MixedExpression::MixedExpression(long d, long e, long f)
{
a = d;
b = e;
c = f;
}
// Add two values and return the sum.
MixedExpression MixedExpression::add(MixedExpression op)
{
MixedExpression res;
res.a = (a * op.c) + (op.a * c);
res.b = (b/c) + (op.b/op.c);
res.c = (c * op.c);
return res;
}
// Subtract two values and return the difference.
MixedExpression MixedExpression::subtract(MixedExpression op)
{
MixedExpression res;
res.a = (a * op.c) - (op.a * c);
res.b = (b/c) - (op.b/op.c);
res.c = (c * op.c);
return res;
}
// Multiply two values and return the product.
MixedExpression MixedExpression::multiply(MixedExpression op)
{
MixedExpression res;
res.a = (a * op.c) * (op.a * c);
res.b = (b/c) * (op.b/op.c);
res.c = (c * op.c);
return res;
}
// Divide two values and return the quotient.
MixedExpression MixedExpression::divide(MixedExpression op)
{
MixedExpression res;
res.a = (a * op.c) / (op.a * c);
res.b = (b/c) / (op.b/op.c);
res.c = (c * op.c);
return res;
}
// Read each value.
void MixedExpression::ReadMixedExp(istream &in)
{
char remove;
in >> remove >> a >> remove >> b >> remove >> c >> remove;
}
// Print the results.
void MixedExpression::printData(ostream &out)
{
out << "( " << a << " + " << b << " / " << c << " )";
}