/////// Number.C /////// #include "Number.h" #include Number& Number::OP =(CS Number& n) // assignment { if (&n == this) return(*this); delete(ptr); ptr = (&n)->copy(); return(*this); } #define ABS(x) ((x) < 0 ? -(x) : (x)) static unsigned int gcd(int a, int b) { int t; a = ABS(a); b = ABS(b); if ( a == 0 ) return(b); // 0 is error value if ( b == 0 ) return(a); while ( b > 0 ) { t = a % b; a = b; b = t; } return (a); } Number::Number(int n, int d) { assert(d != 0); if ( d < 0 ) { n = -n; d = -d; } // make d > 0 int c = gcd(n,d); if ( c != 1 ) // remove gcd { n /= c; d /= c; } if ( d == 1 ) ptr = new Integer(n); else ptr = new Fraction(n,d); }