(By the way this is c++)

#include "rational.h" int main() { Rational a(20,2); Rational b(4,2); if(a.less(B)/>) cout << "true" << endl; }

class Rational { public: int numerator; int denominator; Rational(int _numerator, int _denominator); void print(); Rational add(Rational &other); Rational sub(Rational &other); Rational mul(Rational &other); Rational div(Rational &other); bool less(Rational &other); int gcd(int x, int y); };

#include <iostream> #include "rational.h" using namespace std; Rational::Rational(int _numerator, int _denominator) { numerator = _numerator; denominator = _denominator; } void Rational::print() { cout << numerator << "/" << denominator << endl; } Rational Rational::add(Rational &other) { int new_numerator = (numerator*other.denominator) + (denominator *other.numerator); int new_denominator = (denominator * other.denominator); int gcd = Rational::gcd(new_numerator, new_denominator); return Rational(new_numerator/gcd, new_denominator/gcd); } Rational Rational::sub(Rational &other) { int new_numerator = (numerator*other.denominator) - (denominator *other.numerator); int new_denominator = (denominator * other.denominator); int gcd = Rational::gcd(new_numerator, new_denominator); return Rational(new_numerator/gcd, new_denominator/gcd); } Rational Rational::mul(Rational &other) { int new_numerator = (numerator * other.numerator); int new_denominator = (denominator * other.denominator); int gcd = Rational::gcd(new_numerator, new_denominator); return Rational(new_numerator/gcd, new_denominator/gcd); } Rational Rational::div(Rational &other) { int new_numerator = (numerator*other.denominator); int new_denominator = (denominator * other.numerator); int gcd = Rational::gcd(new_numerator, new_denominator); return Rational(new_numerator/gcd, new_denominator/gcd); } bool Rational::less(Rational &other) { if((numerator*other.denominator)<(other.numerator*denominator)) return true; else return false; } int Rational::gcd(int x, int y) { int g; if (x < 0) x = -x; if (y < 0) y = -y; if ((x+y) == 0) return 0; g = y; while(x > 0) { g = x; x = y % x; y = g; } return g; }

This post has been edited by **GDubz**: 11 February 2012 - 03:34 PM