1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include <iostream>
#import "Rational.h"
using namespace std;
int getNumerator(){
return numerator;
} //end getNumerator
void setNumerator(int n){
numerator = n;
} //end setNumerator
int getDenominator(){
return denominator;
} //end getDenominator
void setDenominator(int n){
denominator = n;
} //end setDenominator
friend Rational operator+ (Rational &a, Rational &b){
//a/b + c/d = (a * d + b * c) / (b * d)
Rational RationalSum;
RationalSum.setNumerator(a.getNumerator() * b.getDenominator() +
a.getDenominator() * b.getNumerato());
RationalSum.setDenominator (a.getDenominator() * b.getDenominator());
return RationalSum;
} // end operator+
friend Rational operator- (Rational &a, Rational &b){
//a/b - c/d = (a * d - b * c) / (b * d)
Rational RationalSubt;
RationalSubt.setNumerator(a.getNumerator() * b.getDenominator() -
a.getDenominator() * b.getNumerator());
RationalSubt.setDenominator (a.getDenominator() * b.getDenominator());
return RationalSubt;
} // end operator-
friend Rational operator* (Rational &a, Rational &b){
//(a/b) * (c/d) = (a * c) / (b * d)
Rational RationalMult;
RationalMult.setNumerator(a.getNumerator() * b.getNumerator());
RationalMult.setDenominator (a.getDenominator() * b.getDenominator());
return RationalMult;
} // end operator*
friend Rational operator/ (Rational &a, Rational &b){
//(a/b) / (c/d) = (a * d) / (c * b)
Rational RationalDiv;
RationalDiv.setNumerator(a.getNumerator() * b.getDenominator());
RationalDiv.setDenominator(a.getDenominator() * b.getNumerator());
return RationalDiv;
} //end operator/
friend bool operator== (Rational &a, Rational &b){
//(a/b) == (c/d) means (a * d) == (c * b)
Rational RationalEqu;
RationalEqu.setNumerator(a.getNumerator() * b.getDenominator());
RationalEqu.setDenominator(b.getNumerator() * a.getDenominator());
return RationalEqu;
} //end operator==
friend bool operator< (Rational &a, Rational &b){
//(a/b) < (c/d) means (a * d) < (c * b)
Rational RationalLess;
RationalLess.setNumerator(a.getNumerator() * b.getDenominator());
RationalLess.setDenominator(b.getNumerator() * a.getDenominator());
return RationalLess;
} // end operator<
friend bool operator<= (Rational &a, Rational &b){
Rational RationalLorE;
RationalLorE.setNumerator(a.getNumerator() * b.getDenominator());
RationalLorE.setDenominator(b.getNumerator() * a.getDenominator());
return RationalLorE;
} // end operator<=
friend bool operator> (Rational &a, Rational &b){
//(a/b) > (c/d) means (a * d) > (c * b)
Rational RationalGreat;
RationalGreat.setNumerator(a.getNumerator() * b.getDenominator());
RationalGreat.setDenominator(b.getNumerator() * a.getDenominator());
return RationalGreat;
} // end operator>
friend bool operator>= (Rational &a, Rational &b){
Rational RationalGorE;
RationalGorE.setNumerator(a.getNumerator() * b.getDenominator());
RationalGorE.setDenominator(b.getNumerator() * a.getDenominator());
return RationalGorE;
} // end operator>=
ostream& operator<<(ostream& fout, const Rational& r){
if (r.denominator== 0)
out << "undefined" ;
else
out << r.numerator << "/" << r.denominator;
return fout;
} //end ostream
istream& operator>>(istream& fin, Rational& r){
in >> r.numerator >> r.denominator;
return fin;
} // end istream
Rational::Rational(int a, int b){
numerator = a;
denominator = b;
} //end Rational
Rational::Rational(){
numerator = 5;
denominator = 10;
} //end Rational
Rational::Rational(){
num = 0;
dem = 1;
} // end Rational
Rational::Rational(int whole){
num = whole;
dem = 1;
} // end Rational
int main(){
//Rational rational;
//Rational a;
//Rational b(1,2);
//Rational bad(1,0);
//Rational c(10);
//cout << "Enter your number: ";
//cin >> rational;
//cout << "You entered the number " << rational << ".\n";
return 0;
} // end main function
| |