bool lessThan(fraction x, fraction y) // non-member function returning a bool
{                                     // indicating if x < y
    int xDen = x.getDenom();
    int yDen = y.getDenom();
    int lcm = LCM(xDen, yDen);           // same code as add, mostly
    int factor1 = lcm / xDen;
    int factor2 = lcm / yDen;
    return ((x.getNumer() * factor1) < (y.getNumer() * factor2));
}
fraction add(fraction x, fraction y) // non-member function to return a new
{                                    // fraction containing the sum of two others
    int xDen = x.getDenom();
    int yDen = y.getDenom();
    int lcm = LCM(xDen, yDen);          // it would be trivial to implement
    int factor1 = lcm / xDen;           // reduceMe here if desired.
    int factor2 = lcm / yDen;
    int num = (x.getNumer() * factor1) + (y.getNumer() * factor2);
    fraction z = fraction(num, lcm);
    return z;
}
Example #3
0
bool operator<(const fraction &a, const fraction &b) {
  double ax, ay;
  int as;
  getFrSpec(a.getNumer(), a.getDenom(), &ax, &ay, &as);
  if(as == -1) { ax =- ax; ay =-ay; }

  double bx, by;
  int bs;
  getFrSpec(b.getNumer(), b.getDenom(), &bx, &by, &bs);
  if(bs == -1) { bx =- bx; by =-by; }
  return (ax < bx || (ax == bx && ay < by));
}
Example #4
0
const fraction operator*(const fraction &a, int64_t b) {
  if(b < a.getInterimMultOverflowPt()) {
    //   b < interimMultOverflowPt
    return fraction(b * a.getNumer(), a.getDenom());
  } else if(b <= a.getFinalMultOverflowPt()) {
    //   interimMultOverflowPt <= b <= finalMultInterimPt
    cerr << "fraction::operator*- an interim overflow has occurred\n";
    return fraction(I64_MAX);
  } else {  //  finalMultInterimPt < b
    cerr << "fraction::operator*- a final overflow has occurred\n";
    return fraction(I64_MAX);
  }
}
Example #5
0
fraction fraction::operator*(fraction& fr2)
{
	int num2,den2,numH,denH;
	fraction frH;
	
	fr2.getnd(num2,den2);
	numH = num*num2/gcd(num*num2,den*den2);
	denH = den*den2/gcd(num*num2,den*den2);
	frH.setnd(numH,denH);
	
	return frH;
}
Example #6
0
void mixedNumber::copy(const fraction &other)
{
    setFraction(other.getNum(), other.getDenom());
}
Example #7
0
int main()
{
    fraction f1(9,8);
    fraction f2(2,3);
    fraction result;
    
    cout << "The result starts off at ";
    result.print();
    cout << endl;
    
    cout << "The product of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.multipliedBy(f2);
    result.print();
    cout << endl;

    cout << "The quotient of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.dividedBy(f2);
    result.print();
    cout << endl;
    
    cout << "The sum of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.addedTo(f2);
    result.print();
    cout << endl;
    
    cout << "The difference of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.subtract(f2);
    result.print();
    cout << endl;
    
    if (f1.isEqualTo(f2)){
        cout << "The two fractions are equal." << endl;
    } else {
        cout << "The two fractions are not equal." << endl;
    }

    const fraction f3(12, 8);
    const fraction f4(202, 303);
    result = f3.multipliedBy(f4);
    cout << "The product of ";
    f3.print();
    cout << " and ";
    f4.print();
    cout << " is ";
    result.print();
    cout << endl;
}
Example #8
0
mixedNumbersV2::mixedNumbersV2(const fraction &other)
{
    numerator=other.getNumerator();
    denominator=other.getDenominator();
}
Example #9
0
bool fraction::operator>(const fraction &other) {
	return this->getReal() > other.getReal();
}