bool operator<(Number l, Number r) { int lnum = l.numerator(); int lden = l.denominator(); int rnum = r.numerator(); int rden = r.denominator(); if (lnum < 0 && rnum >= 0) return true; if (rnum <= 0 && lnum >=0) return false; return operatorHelper(abs(lnum), abs(lden), abs(rnum), abs(rden)); }
Matrix Matrix::operator-() { Matrix negate(rows,cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols ; j++) { Number num = at(i).at(j); num.numerator( -1*(num.numerator()) ); } } return negate; }
Number operator*( Number l, Number r) { int lnum = l.numerator(); int lden = l.denominator(); int rnum = r.numerator(); int rden = r.denominator(); // cross factor int factor1 = gcdFunction(lnum, rden); int factor2 = gcdFunction(rnum, lden); lnum /= factor1; rden /= factor1; rnum /= factor2; lden /= factor2; Number prod(lnum*rnum, rden*lden); return prod; }
void Matrix::printRowAt(int row) { cout << "|"; for (int j = 0; j < at(row).size() ; j++) { Number num = at(row).at(j); // if positive if (num >= Number()) { cout << " "; } // if whole number if (num.denominator() == 1) { cout << " " << num.numerator() << " "; } else { cout << " " << num << " "; } } cout << "|"; }
Number operator/( Number l, Number r) { Number inverse(r.denominator(), r.numerator()); return l*inverse; }
bool operator==( Number l, Number r ) { return (l.numerator() == r.numerator() && l.denominator() == r.denominator()); }
// copy constructor Number::Number(const Number &c) : num(c.numerator()), denom(c.denominator()) {}