#include "MixedExpression.h"
#include<iostream>
#include<fstream>
using namespace std;
long GCD(long x, long y) // Get the greatest common divisor.
{
long remainder; // Holds the remainder.
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;
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 * c)/c) + (b/c)) + (((op.a * op.c)/op.c) + (op.b/op.c));
return res;
}
// Subtract two values and return the difference.
MixedExpression MixedExpression::subtract(MixedExpression op)
{
MixedExpression res;
res.a = (((a * c)/c) + (b/c)) - (((op.a * op.c)/op.c) + (op.b/op.c));
return res;
}
// Multiply two values and return the product.
MixedExpression MixedExpression::multiply(MixedExpression op)
{
MixedExpression res;
res.a = (((a * c)/c) + (b/c)) * (((op.a * op.c)/op.c) + (op.b/op.c));
return res;
}
// Divide two values and return the quotient.
MixedExpression MixedExpression::divide(MixedExpression op)
{
MixedExpression res;
res.a = (((a * c)/c) + (b/c)) / (((op.a * op.c)/op.c) + (op.b/op.c));
return res;
}
// Read each value while not at the end of the file.
void MixedExpression::ReadMixedExp(istream &in)
{
in >> a >> b >> c;
}
// Print the results.
void MixedExpression::printData(ostream &out)
{
out << "( " << a << " + " << b << " / " << c << " )" << endl;
}